我正在努力得到Vue.js 2.0打字在Visual Studio中使用TypeScript.之前,我使用了these typings from DefinitelyTyped,但它们是用于Vue的.js 1.0,因此不匹配.然而,它们确实工作得很好,让我使用Vue型.

此后,我开始使用Vue附带的键入文件.js发布(here).我已经将它们包含在我的~/Scripts/typings/vue文件夹中的项目中,但我的项目不理解它们.

据我所知,这些打字文件可能是通过导入/导出来使用的?我使用的其他打字文件都不是这样工作的,所以我不确定如何正确地实现打字,使它们在我的项目中全局可用.

我有一个示例解决方案,它展示了我try 过的一个例子——download here from my github repo.

以下是我的Scripts文件夹的 struct :

enter image description here

_references.d.ts个目录

/// <reference path="typings/vue/index.d.ts" /> 

vue_test.ts个目录

namespace Test
{
    export class MyClass
    {
        public initialize()
        {
            var component = this.getComponent();
        }

        private getComponent(): Vue.Component
        {
            return Vue.component("test", {
                template: "<div></div>",
                props: ["test"],
                methods: {
                    onClick: () =>
                    {
                    }
                }
            });
        }
    }
}

我希望我能够访问Vue.Component个名称空间和typings/vue/index.d.ts中声明的其他名称空间,但情况似乎并非如此.

我try 将导出的类导入到global中,如下所示:

import * as _Vue from "./index";

declare global
{
    export class Vue extends _Vue
    {

    }
}

然而,这只允许我访问根Vue类,因此我无法执行诸如将Vue.Component指定为类型或Vue之外的任何其他名称空间之类的操作.

Other information:

UPDATE after suggestions from @unional

没有更多的参考资料.d、 ts,使用tsconfig.而是json.Tconfig.json文件包含以下内容:

{
    "compilerOptions": {
        "sourceMap": true
    },
    "include": [
        "../../**/*.ts"
    ]
}

以上都是进口商品.项目中的ts文件.~/Scripts/typings/custom-typings/vue.d.ts文件包含以下内容:

export * from "vue"
export as namespace Vue

VisualStudio告诉我Cannot find module 'vue',所以我的打字仍然不起作用,尽管Tconfig.json文件可以工作(我添加了jQuery类型以验证这一点).

下面是更新解决方案的链接,显示了新问题:[link]

推荐答案

我可以使用@unional comments 中的信息,但有一点变化:

我将此添加到自定义vue中.d、 ts文件:

import * as _Vue from 'vue';
export as namespace Vue;
export = _Vue; 

此外,我还必须创建一个包含以下内容的package.json文件:

{
    "dependencies": {
        "vue": "^2.2.1"
    }
}

最后,我需要在与tsconfig.json文件相同的范围内添加一个node_modules文件夹.它有以下特点:

+-- node_modules
|   +-- vue
|   |   +-- package.json
|   |   +-- types
|   |   |   +-- index.d.ts
|   |   |   +-- options.d.ts
|   |   |   +-- plugin.d.ts
|   |   |   +-- vnode.d.ts
|   |   |   +-- vue.d.ts

package.json简单包含:

{
  "typings": "types/index.d.ts"
}

现在一切正常

Edit

import * as _Vue from "../vue/index";

Vue.js相关问答推荐

绑定事件在渲染函数中不起作用

vue 3 中范围样式和深度样式的问题

如何在Vue js 3中根据API响应替换react 值

vue router/:param 斜杠的参数

两个div元素之间的vue2过渡

Vuetify 复选框:如何停止点击事件?

如何使用 vue 或 javascript 向服务器发送密码?

VueJS - 将单独的组件加载到 vue 实例中

使用数组元素的计算(computed)属性

使用 vuex 更新数据

Vue:限制文本区域输入中的字符,截断过滤器?

如何将 vuetify 2.0 beta 安装到新的 vue cli 项目中?

Vuejs模板继承

如何更改 Vuetify 日历日期格式

Select 下拉列表中的复选框后如何清除 v-autocomplete(多个)搜索输入?

如何从 v-for 创建的对象中绑定多个类?

在 VueJS 中观察元素的高度

Vue 2.0 设置路由 - 不要使用new来产生副作用

Vue - 重用方法的最佳方式

理解 Vue 中的this关键字