使用Elemen加上lang=“ts“后編譯報錯
部分代碼:
<template> <el-input v-model="input" placeholder="Please input" /> </template> <script lang="ts" setup> import { ref } from 'vue' const input = ref('') </script>
瀏覽器報錯:
在這里搞了幾個小時,后面發(fā)現(xiàn)是加了 lang=ts 的原因
1.下載typescript和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$/], ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ] ? ? ? ? ? } ? ? ? ? }
添加好后如下:
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; } ?
在第四步出現(xiàn)這個錯誤不影響允許,看錯誤提示是因為不符合ESLint規(guī)范,我也不知道怎么改。
但是這看著就很不舒服,可以把ESLint檢測關閉(按一下步驟操作就行)
這就舒服多了
成功展示。
到此這篇關于使用Elemen加上lang=“ts“后編譯報錯的文章就介紹到這了,更多相關Elemen lang=“ts“報錯內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
vue 如何添加全局函數(shù)或全局變量以及單頁面的title設置總結
本篇文章主要介紹了vue 如何添加全局函數(shù)或全局變量以及單頁面的title設置總結,非常具有實用價值,需要的朋友可以參考下2017-06-06Vue中圖片上傳組件封裝-antd的a-upload二次封裝的實例
這篇文章主要介紹了Vue中圖片上傳組件封裝-antd的a-upload二次封裝的實例,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09