1. 程式人生 > 其它 >vue-router使用出現Uncaught TypeError:Cannot read property ‘use‘ of undefined

vue-router使用出現Uncaught TypeError:Cannot read property ‘use‘ of undefined

問題

Uncaught TypeError:Cannot read property 'use' of undefined //vue.use()沒有定義
Uncaught TypeError:vue_router__WEBPACK_IMPORTED_MODULE_6__.default is not a constructor  // 說vue-router預設的不是構造器

然後我檢查了一下終端輸出的內容,好傢伙,說我匯入的模組沒有找到

"export 'default'(imported as 'Vue') was not found in 'vue'
"export 'default'
(imported as 'VueRouter') was not found in 'vue-router'

在我搜索瀏覽很多csdn之後,發現好像沒人碰到這個問題,耗費了一個多小時,**然後我去看了Vue 3x 文件發現在宣告一個Vue例項使用的不再是new Vue()了,而是createApp()。**是的,然後我發現我在vue3x裡面用了new Vue(),所以出錯是理所當然的。我也終於明白為啥沒人碰到這個問題了,應該沒有人會向我一樣去改腳手架自動生成的模板吧…
在這裡插入圖片描述
這是Vue 2x的main.js

在這裡插入圖片描述
這Vue 3x 的main.js

所以之後,我又重新建立了一個檔案

在這裡插入圖片描述

所以,如果自己很想用new Vue()的話,就使用vue 2x

如果想了解new Vue() 和 createApp()的區別的話,指路:new Vue() vs createApp()