当前位置: 代码网 > it编程>前端脚本>Vue.js > vue3+ElementPlus使用lang="ts"报Unexpected token错误的解决办法

vue3+ElementPlus使用lang="ts"报Unexpected token错误的解决办法

2024年06月01日 Vue.js 我要评论
问题背景在做vue3+elementplus项目时,复制粘贴elementplus官网的代码到项目中,结果会报这样的错:eslint parsing error: unexpected token明明

问题背景

在做vue3+elementplus项目时,复制粘贴elementplus官网的代码到项目中,结果会报这样的错:

eslint parsing error: unexpected token

明明就是按照官网的代码原封不动的粘贴过来,为什么会报错呢?经过排查,原来是< script>标签中加了“lang = ts”,也就是使用了typescript语法糖。导致出现这个错误

问题解决

步骤一:下载typescript和ts-loader

npm install typescript ts-loader --save-dev

步骤二:配置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$/],
          }
        }
      ]
    }
  }
})

步骤三:新建tsconfig.json文件放在项目根目录,并添加如下内容

{
    "compileroptions": {
      "target": "es5",
      "module": "commonjs",
      "strict": true,
      "strictnullchecks": true,
      "esmoduleinterop": true,
      "experimentaldecorators": true
    }
}

步骤四:在src根目录下新建vue-shim.d.ts文件,并添加如下内容;( 这个文件可以让vue识别ts文件,不加会报错)

declare module "*.vue" {
    import vue from "vue";
    export default vue;
}  

步骤五:重启项目,成功运行

本文主要参考如下文章:https://blog.csdn.net/qq_61672548/article/details/125506231

总结

到此这篇关于vue3+elementplus使用lang="ts"报unexpected token错误解决的文章就介绍到这了,更多相关vue3 elementplus报unexpected token内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com