当使用vue3.0搭配ElementPlus搭建项目时,报有关ts的错
解决方案如下:
1.下载typescript和ts-loader
npm install typescript ts-loader --save-dev
2.配置vue.config.js文件,添加下面的代码
configureWebpack: {
resolve: { extensions: [".ts", ".tsx", ".js", ".json"] },
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
options: {
appendTsSuffixTo: [/\.vue$/],
}
}
]
}
}
添加好后,vue.config.js 内容如下:
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
transpileDependencies: true,
configureWebpack: {
resolve: { extensions: [".ts", ".tsx", ".js", ".json"] },
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
options: {
appendTsSuffixTo: [/\.vue$/],
}
}
]
}
}
})
3.步骤三:新建tsconfig.json文件放在项目根目录,并添加如下内容
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"strict": true,
"strictNullChecks": true,
"esModuleInterop": true,
"experimentalDecorators": true
}
}
4.在src根目录下新建vue-shim.d.ts文件,并添加如下内容;( 这个文件可以让vue识别ts文件,不加会报错)
declare module "*.vue" {
import Vue from "vue";
export default Vue;
}
5.重启项目,成功运行
也是我遇到问题然后看到的,确实解决了我的问题,此文章只用于记录,如有冒犯联系删除。
附上链接来源