1、defineConstants
引入全局变量报错:Uncaught SyntaxError: missing ) after argument list,一定先加双引号,再加单引号
defineConstants: {
BASE_URL:'"/api"',
},
2、import { getWindowHeight } from "@/utils/utils"
报错:找不到模块“@/utils/global”或其相应的类型声明
在config文件夹下的index.js文件的alias设置别名
const path = require("path"); //顶部引入,不要忘记了
alias: {
"@/components": path.resolve(__dirname, "..", "src/components"),
"@/services": path.resolve(__dirname, "..", "src/services"),
"@/utils": path.resolve(__dirname, "..", "src/utils"),
"@/pages": path.resolve(__dirname, "..", "src/pages"),
"@/icon": path.resolve(__dirname, "..", "src/icon"),
},
在tsconfig.json文件下修改
"paths": {
"@/component/*": ["src/component/*"],
"@/services/*": ["src/services/*"],
"@/utils/*": ["src/utils/*"],
"@/pages/*": ["src/pages/*"],
"@/icon/*": ["src/icon/*"],
},
// ts 管理的文件
"include": [
"src/**/*.ts",
"src/**/*.tsx",
"src/**/*.vue",
],
3、报错:Property “value” was accessed during render but is not defined on instance.
原因:data里面定义value报错,是因为忘记return了
data() {
return {
value: "",
};
},
4、import时ts不识别.vue文件,在src根目录下,新建一个.d.ts
后缀的文件,放入以下代码。
declare module '*.vue' {
import { defineComponent } from 'vue';
const Component: ReturnType<typeof defineComponent>;
export default Component;
}
5、 Invalid prop: type check failed for prop “index”. Expected Number with value 12, got String with value “12”.
原因:for循环得到的index是String
类型,在子组件定义类型定义成了Number
类型。
props: {
index: {
type: String, //原Number类型报错需要改为String
default: 0,
},
}
6、Type ‘String’ is not assignable to type ‘string’,‘string’ is a primitive, but ‘String’ is a wrapper object. Prefer using ‘string’ when possible.
原因:在ts后缀的文件用String
,在vue后缀的文件用string