当前位置: 首页 > 知识库问答 >
问题:

导入vue类型脚本类型定义

李疏珂
2023-03-14

我试图在我的主文件条目中引用vue的typescript定义。ts

entry.ts位于src/js/entry, ts,vue的类型定义位于src/js/lib/baul/vue/type/index.d.ts

/// <reference path='./lib/typings/jquery.d.ts' />
/// <reference path='./lib/bower/vue/types/index.d.ts' />

let data: Object = {},
    app: Vue = new Vue({
        data: data,
        el: '#app'
    });
console.log(app);

class Test {
    private id: string;
    constructor(id: string) {
        this.id = id;
    }
    public getElement(): any {
        return $(this.id);
    }
}
console.log(new Test('asdf').getElement());

编译此文件时,输出如下。顺便说一句,typescript的目标是es6和es6模块。

[steventheevil@Steven-PC webdev-starter]$ tsc
src/js/entry.ts(5,10): error TS2304: Cannot find name 'Vue'.
src/js/entry.ts(5,20): error TS2304: Cannot find name 'Vue'.

JQuery工作正常(看起来像),所以我用导入替换了对vue类型定义的引用。

/// <reference path='./lib/typings/jquery.d.ts' />
import * as Vue from './lib/bower/vue/types/index';

let data: Object = {},
    app: Vue = new Vue({
        data: data,
        el: '#app'
    });
console.log(app);

class Test {
    private id: string;
    constructor(id: string) {
        this.id = id;
    }
    public getElement(): any {
        return $(this.id);
    }
}
console.log(new Test('asdf').getElement());

它编译得很好,下面是输出:

import * as Vue from '../../src/js/lib/bower/vue/types/index';
let data = {}, app = new Vue({
    data: data,
    el: '#app'
});
console.log(app);
class Test {
    constructor(id) {
        this.id = id;
    }
    getElement() {
        return $(this.id);
    }
}
console.log(new Test('asdf').getElement());

问题是没有删除类型定义的import语句。当我在babel中使用rollup时,这会导致一个错误(我不为ts使用rollup插件,因为我需要操作rollup和ts之间的文件)。如何告诉typescript编译器删除类型定义(.d.ts文件)的导入?

这是vue的类型定义(src/jslb/baul/vue/type/index.d.ts)

import * as V from "./vue";
import * as Options from "./options";
import * as Plugin from "./plugin";
import * as VNode from "./vnode";

// `Vue` in `export = Vue` must be a namespace
// All available types are exported via this namespace
declare namespace Vue {
  export type CreateElement = V.CreateElement;

  export type Component = Options.Component;
  export type AsyncComponent = Options.AsyncComponent;
  export type ComponentOptions<V extends Vue> = Options.ComponentOptions<V>;
  export type FunctionalComponentOptions = Options.FunctionalComponentOptions;
  export type RenderContext = Options.RenderContext;
  export type PropOptions = Options.PropOptions;
  export type ComputedOptions<V extends Vue> = Options.ComputedOptions<V>;
  export type WatchHandler<V extends Vue> = Options.WatchHandler<V>;
  export type WatchOptions = Options.WatchOptions;
  export type DirectiveFunction = Options.DirectiveFunction;
  export type DirectiveOptions = Options.DirectiveOptions;

  export type PluginFunction<T> = Plugin.PluginFunction<T>;
  export type PluginObject<T> = Plugin.PluginObject<T>;

  export type VNodeChildren = VNode.VNodeChildren;
  export type VNodeChildrenArrayContents = VNode.VNodeChildrenArrayContents;
  export type VNode = VNode.VNode;
  export type VNodeComponentOptions = VNode.VNodeComponentOptions;
  export type VNodeData = VNode.VNodeData;
  export type VNodeDirective = VNode.VNodeDirective;
}

// TS cannot merge imported class with namespace, declare a subclass to bypass
declare class Vue extends V.Vue {}

export = Vue;

感谢您的帮助。

共有1个答案

秦毅
2023-03-14

当我试图弄清楚如何从我的打字脚本代码中导入VNode时,我偶然发现了这个问题。

也许我的回答会帮助那些面临同样问题的人。

类型定义见vue/types。从那里导入所需的接口或类。

VNode为例:

// main.ts
import Vue from 'vue';
import { VNode } from 'vue/types';
import App from './App.vue';

new Vue({
  render: (h): VNode => h(App),
}).$mount('#app');

使用的Vue版本:2.6.10。

 类似资料:
  • 我在许多组件中使用了几个大型对象,因此我为每个大型对象创建了一个proptypes文件,如下所示: 其中包含从"prop类型"导入PropType; 我在组件中使用对象,如下所示: 它给我一个警告,Prop类型“PropLargeObject”无效,它必须是一个函数,通常来自React。道具类型。我做错了什么?

  • 当我将接口的任何属性设置为可选时,我会在将其成员分配给其他变量时遇到如下错误: 我如何绕过这个错误?

  • 我需要帮助来找出如何解决活动中两个相互冲突的导入的问题,即: null 取决于哪一个先来。

  • npm包允许我们在TypeScript应用程序中使用反应。我们将组件定义为 在这里,我们必须声明组件道具和状态的类型(在类型变量中)。 在我们声明了这些类型之后,TypeScript使用这些类型来验证组件的使用(传递给它的道具的形状)。 我想围绕这样一个组件创建一个容器。容器将重用组件的道具。但是为了用相同的道具创建另一个组件,我必须重新声明道具的类型。或者从原始零部件文件中导出它们并导入到容器中

  • 问题是如何使用对象类型发送Header,而不是HTTPClient声明中提供的HttpHeaders。 我在VS代码中得到的错误信息如下所示 “{headers:GetLoggedInUserHeaderRequestParam;}”类型的[ts]参数不可分配给“{headers?:HttpHeaders{[Header:string]:string string[];};};”类型“的参数;”观

  • 在我的项目中,我希望有一个包含所有类型脚本定义的文件,如下所示: type_defs.tsx: 问题是,当我尝试导出typedefs时,如下所示: 我得到了错误: 正如我在网络中发现的那样,只能对typedef文件进行默认导出,这意味着我必须为每个类型定义准备一个文件,这绝对是荒谬的! 在我看来,不允许typedefs具有多个非默认导出的约束完全是无稽之谈,并且使编码变得多余且难以重用。拥有一个包