1. 程式人生 > 其它 >vue常見錯誤

vue常見錯誤

  • 刪除根元件app.vue中的預設程式碼後報錯:Module Error (from ./node_modules/eslint-loader/index.js):
  • 解決方案:關閉ESlint程式碼檢測,在專案根目錄建立vue.config.js,在檔案中新增
  module.exports = {
    lintOnSave: false
  }

參考

  • 專案啟動報錯:Trailing spaces not allowed no-trailing-spaces
  • 錯誤原因:ESLint檢查到了多餘的空格
  • 解決方案:通常是刪除了根元件App.vue中的預設程式碼後還有多餘空格,刪除多餘的空格即可;或者配置.eslintrc.js,找到rules,新增如下:
  rules: {
    'no-irregular-whitespace': 'off'
  }

參考

  • vite構建vue專案,使用vscode開啟後,出現紅色波浪線;應取消vue模板校驗:設定 -> 取消勾選'Vetur>Validation:template'

  • 根元件app.vue的template標籤中使用引入的子元件HelloWorld.vue時如果報錯,子元件中模板應使用單根元件形式,子元件必須使用閉合標籤

  • vue-cli構建的vue專案,執行依賴和開發依賴的區別:dependencies執行依賴是專案上線後需要用到的依賴,devDependencies是專案上線後不會用到的依賴

  • cli構建專案後啟動報錯:Syntax Error: TypeError: this.getOptions is not a function

# 如果專案中使用了sass,需安裝依賴sass-loader、node-sass;若專案中使用less,需安裝依賴less-loader、less
# 錯誤原因:sass-loader或less-loader版本太高
# 解決方案:解除安裝sass-loader或less-loader,重新安裝低版本
  npm uninstall --save sass-loader           # 終端解除安裝
  npm remove less-loader          # 終端解除安裝
  cnpm uninstall xxx --save         # 解除安裝
# 也可以使用cmd輸入vue ui開啟vue工作臺解除安裝
  cnpm i [email protected] --save        # 安裝指定版本 
  npm install [email protected]       # 安裝less-loader
  npm install [email protected]        # 安裝sass-loader
# 使用以上方式安裝會安裝成執行依賴,應安裝開發依賴;在專案的package.json檔案中dependencies表示執行依賴,devDependencies表示開發依賴
 "dependencies": {
    "axios": "^0.21.1",
    "core-js": "^3.6.5",
    "element-ui": "^2.4.5",
    "vue": "^2.6.11",
    "vue-router": "^3.2.0"
  },
  "devDependencies": {
    "@vue/cli-plugin-babel": "~4.5.0",
    "@vue/cli-plugin-eslint": "~4.5.0",
    "@vue/cli-plugin-router": "~4.5.0",
    "@vue/cli-service": "~4.5.0",
    "@vue/eslint-config-standard": "^5.1.2",
    "babel-eslint": "^10.1.0",
    "babel-plugin-component": "^1.1.1",
    "eslint": "^6.7.2",
    "eslint-plugin-import": "^2.20.2",
    "eslint-plugin-node": "^11.1.0",
    "eslint-plugin-promise": "^4.2.1",
    "eslint-plugin-standard": "^4.0.0",
    "eslint-plugin-vue": "^6.2.2",
    "less": "^4.1.1",
    "less-loader": "^5.0.0",
    "vue-cli-plugin-element": "^1.0.1",
    "vue-template-compiler": "^2.6.11"
  }
# 將dependencies中的那一行剪下到devDependencies

  • cnpm報錯:cnpm : 無法載入檔案 C:\Users\93457\AppData\Roaming\npm\cnpm.ps1

  • 解決方案:管理員的身份開啟powershell,輸入:set-ExecutionPolicy RemoteSigned 選擇A
    參考

  • 報錯:Node Sass version 6.0.1 is incompatible with ^4.0.0.

# 解決方案:
  node-sass npm uninstall node-sass         # 解除安裝之前的版本
  npm install [email protected]          # 解除安裝後安裝4.0.0版本 

參考

  • 啟動報錯:Node Sass could not find a binding for your current environment: Windows 64-bit with Node.js 12.x
  • 解決方案:解除安裝node sass後重新安裝
    參考