找不到模块“vue-xxx”的声明文件

Xav*_*eña 14 javascript node.js typescript vue.js

我尝试添加到我的项目中的任何 3rd 方 Vue.js 库都会引发以下错误:

Could not find a declaration file for module 'vue-xxx'
Run Code Online (Sandbox Code Playgroud)

我试过 'vue-treeselect', 'vue-select' , 'vue-multiselect'或多或少的结果相同(*那些库不允许import Modulename from,但需要const Modulename = require)。

"dependencies": {package.json.

我正在使用 dotnet core 提供的 Vue.js SPA 模板。


'tree-select' 的完整示例:

import Treeselect from 'vue-treeselect';

/* ... */

value = null;
source = [
    {
        id: 'node-1',
        label: 'Node 1',
        children: [
            {
                id: 'node-1-a',
                label: 'Node 1-A',
            }
      ]
    },
    {
        id: 'node-2',
        label: 'Node 2',
    }
  ];

/* ... */

<treeselect v-model="value"
                :multiple="true"
                :options="source" />
Run Code Online (Sandbox Code Playgroud)

错误:

in [at-loader] ./ClientApp/components/mycomponent/mycomponent.ts:10:24 
    TS7016: Could not find a declaration file for module 'vue-treeselect'. 'path/to/project/node_modules/vue-treeselect/dist/vue-treeselect.js' implicitly has an 'any' type.
Run Code Online (Sandbox Code Playgroud)

包.json

{
  "name": "MyProject",
  "private": true,
  "version": "0.0.0",
  "dependencies": {
    "dygraphs": "^2.1.0",
    "ol3-layerswitcher": "^1.1.2",
    "vue-treeselect": "^1.0.6"
  },
  "devDependencies": {
    "@types/dygraphs": "^1.1.6",
    "@types/webpack-env": "^1.13.0",
    "@types/vue": "^2.0.0",
    "aspnet-webpack": "^2.0.1",
    "awesome-typescript-loader": "^3.0.0",
    "bootstrap": "^3.3.6",
    "css-loader": "^0.25.0",
    "event-source-polyfill": "^0.0.7",
    "extract-text-webpack-plugin": "^2.0.0-rc",
    "file-loader": "^0.9.0",
    "isomorphic-fetch": "^2.2.1",
    "jquery": "^3.1.1",
    "style-loader": "^0.13.1",
    "typescript": "^2.2.1",
    "url-loader": "^0.5.7",
    "vue": "^2.5.13",
    "vue-loader": "^14.2.1",
    "vue-property-decorator": "^6.0.0",
    "vue-router": "^3.0.1",
    "vue-template-compiler": "^2.5.13",
    "webpack": "^2.2.0",
    "webpack-hot-middleware": "^2.12.2"    
  }
}
Run Code Online (Sandbox Code Playgroud)

fat*_*thy 18

如果@types/vue-treeselect不存在,那么您可以使用环境定义手动声明模块。

.d.ts在您的项目中的某个地方创建一个(警告:它不应该使用importexport)并定义您的模块。如果您不想对其进行类型检查(使导入的所有内容都被推断为any):

declare module 'vue-treeselect'
Run Code Online (Sandbox Code Playgroud)

如果你想输入检查它:

declare module 'vue-treeselect' {
  export function something(): void
}
Run Code Online (Sandbox Code Playgroud)


Moh*_*han 18

发生此错误是因为 vue-xxxxxx 已在 JavaScript 中生成。

方法 1: 为了避免这个问题,我只是用标准的require()替换它:

const vue-xxxxxx = require('vue-xxxxxx');
// import { vue-xxxxxx } from 'vue-xxxxxx'
Run Code Online (Sandbox Code Playgroud)

方法 2: 为了避免这个问题,我只是在tsconfig.json
"noImplicitAny": false,
"allowJs": true,

现在你可以使用 import 语句如下:
import { vue-xxxxxx } from 'vue-xxxxxx'

享受 :)

  • 支持方法 2 (2认同)
  • 在我的实例中,我只需要 tsconfig.json 文件中的 `"noImplicitAny": false` 。它解决了由于“找不到模块‘vue-xxx’的声明文件”而导致构建失败的问题 (2认同)

luk*_*ups 6

因为任何给定的解决方案本身都没有帮助我,所以我不得不在我的项目根文件夹中创建一个专用的vendor.d.tsshims-vue.d.ts不起作用)文件,然后将以下行放在那里:

declare module 'my-module-name-here';
Run Code Online (Sandbox Code Playgroud)

来源


Don*_*nMB 5

tsconfig.json文件中只需添加即可"allowJs": true,为我解决它