版权声明:本文为优快云博主「今天又有什么bug」的原创文章
原文链接:https://blog.youkuaiyun.com/weixin_46019681/article/details/124953663
具体错误:直接套用elementplus官方文档里的模版,报错:
Module parse failed: Unexpected token……You may need an additional loader to handle the result of these loaders.
————————————————
简单来说就是elementplus我能用,用不了的是它写了ts语法的地方,这个很关键,如果你连基本的elementplus都用不了,那么这篇博客不适合你哦
解决办法
1.配置Vue-Loader
npm install -D vue-loader vue-template-compiler
2.引入Typescript包
npm install --save-dev typescript
npm install --save-dev ts-loader
3.修改vue.config.js
vue页面该咋写还是咋写,一点不用改。
const { defineConfig } = require('@vue/cli-service')
module.exports = defineConfig({
transpileDependencies: true
})
module.exports = { configureWebpack: {
resolve: {
extensions: [".ts", ".tsx", ".js", ".json"],
alias: {}
},
module: {
rules: [
{
test: /\.tsx?$/,
loader: 'ts-loader',
exclude: /node_modules/,
options: {
appendTsSuffixTo: [/\.vue$/],
}
}
]
}
}
}
4.根目录下创建tsconfig.json
代码如下:
{
"compilerOptions": {
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"lib": ["dom","es2016"],
"target": "es5"
},
"include": ["./src/**/*"]
}
5.创建ts文件
src目录下创建一个ts文件,如test.ts,该文件里面什么都不用写。Why?
因为有了tsconfig.json后,VScode会自动在include和exclude包含的范围中查找ts文件,如果找不到ts文件就会报错,当在include和exclude范围中添加了ts文件,VScode就不会报错了。
没写报错:[tsl] ERROR TS18003: No inputs were found in config file 'tsconfig.json'. Specified 'include'
最后,重新编译运行,组件正常显示并且功能正常!问题解决啦!制作不易,欢迎点赞收藏哦!