vue3引用ElementPlus出错|如何在vue中引用TypeScript

版权声明:本文为CSDN博主「今天又有什么bug」的原创文章
原文链接:https://blog.csdn.net/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' 

最后,重新编译运行,组件正常显示并且功能正常!问题解决啦!制作不易,欢迎点赞收藏哦!

 

 

 

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值