简体   繁体   English

在Typescript Vue项目中使用Javascript模块

[英]Using Javascript module in Typescript Vue project

The problem问题

I'm trying to use https://github.com/moonwave99/fretboard.js in my Vue project.我正在尝试在我的 Vue 项目中使用https://github.com/moonwave99/fretboard.js I try to import the module in a component as follows:我尝试按如下方式在组件中导入模块:

<template>
  <div>
    <div id="fretboard" />
  </div>
</template>
<script lang="ts">
import { Fretboard } from '@moonwave99/fretboard.js';
const fretboard = new Fretboard({
  el: '#fretboard',
  fretColor: 'blue',
  dotFill: 'red'
});
fretboard.render([
  {
    string: 5,
    fret: 3
  },
  {
    string: 4,
    fret: 2
  },
  {
    string: 2,
    fret: 1
  }
]);
</script>

This results in the following error:这会导致以下错误:

Could not find a declaration file for module '@moonwave99/fretboard.js'. '/Users/xxx/guitar-apps/node_modules/@moonwave99/fretboard.js/dist/fretboard.cjs.js' implicitly has an 'any' type.
  Try `npm install @types/moonwave99__fretboard.js` if it exists or add a new declaration (.d.ts) file containing `declare module '@moonwave99/fretboard.js';`Vetur(7016)

I've tried runing the npm install @types/moonwave99__fretboard.js command but that also resulted in an error, saying '@types/moonwave99__fretboard.js@latest' is not in the npm registry.我试过运行npm install @types/moonwave99__fretboard.js命令,但这也导致了一个错误,说'@types/moonwave99__fretboard.js@latest' is not in the npm registry. . .

I've been looking all over the place, but couldn't find a solution that worked for my project.我一直在到处寻找,但找不到适合我的项目的解决方案。 Anybody out there who has a solution to my problem?有谁能解决我的问题吗?

Things I've tried我试过的事情

  1. Creating a declaration file containing declare module '@moonwave99/fretboard.js in the root of my project directory在我的项目目录的根目录中创建一个包含declare module '@moonwave99/fretboard.js的声明文件
  2. Adding "allowJs": true to my tsconfig.ts file添加"allowJs": true到我的 tsconfig.ts 文件

Additional information附加信息

My tsconfig looks like:我的 tsconfig 看起来像:

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "allowJs": true,
    "importHelpers": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowSyntheticDefaultImports": true,
    "sourceMap": true,
    "baseUrl": ".",
    "types": [
      "webpack-env",
      "jest"
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "include": [
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx"
  ],
  "exclude": [
    "node_modules"
  ]
}

The .d.ts file should be in one of the include paths configured in tsconfig.json , and according to the tsconfig.json you've posted, that's src or tests . .d.ts文件应该在tsconfig.json配置的include路径之一中,根据您发布的tsconfig.json ,这是srctests These typings are typically stored in src root (you'll see src/shims.vue.d.ts in a Vue CLI-generated project).这些类型通常存储在src根目录中(您将在 Vue CLI 生成的项目中看到src/shims.vue.d.ts )。

You could add a src/fretboard.d.ts file, containing declare module '@moonwave99/fretboard.js' .您可以添加一个src/fretboard.d.ts文件,其中包含declare module '@moonwave99/fretboard.js' Then, you'd have to restart VS Code , so that the typings are indexed properly, which would resolve the error.然后,您必须重新启动 VS Code ,以便正确索引类型,这将解决错误。

Solved with on file src/.env.d.ts add declare module 'vue-image-zoomer' .通过在文件src/.env.d.ts添加declare module 'vue-image-zoomer'

All done.全部做完。 Vanished the error of module not found.消失了模块未找到的错误。

Happy coding.快乐的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM