Vue+ts项目导入Js文件方法

修改 tsconfig.json

{
  "compilerOptions": {
    "target": "esnext",
    "module": "esnext",
    "strict": true,
    "jsx": "preserve",
    "importHelpers": true,
    "moduleResolution": "node",
    "experimentalDecorators": true,
    "skipLibCheck": true,
    "esModuleInterop": true,
    "allowJs": true,                                      // 允许js
    "allowSyntheticDefaultImports": true,
    "noImplicitAny": false,                               // 不进行any语法检查
    "sourceMap": true,
    "baseUrl": ".",
    "types": [
      "webpack-env"
    ],
    "paths": {
      "@/*": [
        "src/*"
      ]
    },
    "lib": [
      "esnext",
      "dom",
      "dom.iterable",
      "scripthost"
    ]
  },
  "include": [
    "src/**/*.js",  // 导入js
    "src/**/*.ts",
    "src/**/*.tsx",
    "src/**/*.vue",
    "tests/**/*.ts",
    "tests/**/*.tsx",
    "public/config.js"  // 导入js
  ],
  "exclude": [
    "node_modules"
  ]
}

oImplicitAny编译器选项所做的,基本上是将TypeScript从可选类型语言转换为强制类型检验语言。

简单的:

function logMe(x) {
  console.log(x);
}
// error TS7006: Parameter 'x' implicitly has an 'any' type.

也将报错——你必须明确声明x的类型为any:

function logMe(x: any) {
  console.log(x);
}
 // OK

这意味着,如果你要把现有的JS代码库迁移到TS,那除了更改文件扩展名,你还得做一些较复杂的东西。这还意味着,在编写代码时,您需要更多地关注类型,如果不指定类型,编译器就总是会「抱怨」。

原文地址:https://www.cnblogs.com/zsg88/p/15621762.html