解決vue創(chuàng)建項(xiàng)目使用vue-router和vuex報錯Object(...)is not a function
vue創(chuàng)建項(xiàng)目使用vue-router和vuex報錯Object(...)is not a function
之前創(chuàng)建項(xiàng)目使用還好好的,這次想新建個項(xiàng)目練練手,才用了vue-router就報錯了,記錄下自己修改了一個下午的bug,淚目了(其實(shí)是版本問題,小小劇透)
報錯:
Object(...) is not a fuction

創(chuàng)建項(xiàng)目時用的是vue-cli3命令:vue create projectName
選擇vue2:

main.js
import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
Vue.config.productionTip = false
new Vue({
render: h => h(App),
router,
store
}).$mount('#app')
router文件夾下的index.js
import Vue from 'vue'
import VueRouter from 'vue-router'
const testA = () => import('../views/testA.vue')
const testB = () => import('../views/testB.vue')
const testC = () => import('../views/testC.vue')
//1.安裝插件
Vue.use(VueRouter);
//2.創(chuàng)建路由對象
const routes = [
{
path: '',
redirect: '/testa'
},
{
path: '/testa',
component: testA
},
{
path: '/testb',
component: testB
},
{
path: '/testc',
component: testC
}
]
const router = new VueRouter({
routes,
mode: 'history'
})
export default router
store文件夾下的index.js
import Vue from 'vue';
import Vuex from 'vuex'
Vue.use(Vuex);
const store = new Vuex.Store({
state: {
},
mutations: {
},
actions: {
}
})
export default store;
找bug
一開始報錯,百度了下,既然報錯Object(…) is not a fuction,那肯定是那個函數(shù)或者啥寫錯拼錯了,檢查了一遍,就是沒有錯。
又有的文章說是import引入沒有加{},我給全部加上了,報錯
Cannot read properties of undefined (reading ‘use‘)
百思不得其解以為是創(chuàng)項(xiàng)目時使用的腳手架和語法有沖突,又重新創(chuàng)了幾次項(xiàng)目。。。。
真正的原因是vue-router,vuex和vue的版本不匹配。
解決方法
卸載vue-router和vuex npm uninstall vue-router和npm uninstall vuex
安裝匹配版本,我這里可以運(yùn)行的版本是vue-router: “^3.5.2”, vuex: “^3.6.2”。
輸入命令npm install vue-router@3.5.2和npm install vuex@3.6.2
當(dāng)然不是重新安裝就完事兒了,重點(diǎn)是再輸入npm install
然后bug就完美解決啦~
附上版本

總結(jié)
以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
element-ui使用el-date-picker日期組件常見場景分析
最近一直在使用 element-ui中的日期組件,所以想對日期組件常用的做一個簡單的總結(jié),對element-ui el-date-picker日期組件使用場景分析感興趣的朋友一起看看吧2024-05-05
vue3.x?的shallowReactive?與?shallowRef?使用場景分析
在Vue3.x中,`shallowReactive`和`shallowRef`是用于創(chuàng)建淺層響應(yīng)式數(shù)據(jù)的API,它們與`reactive`和`ref`類似,本文介紹vue3.x??shallowReactive?與?shallowRef的使用場景,感興趣的朋友一起看看吧2025-02-02
自定義Vue中的v-module雙向綁定的實(shí)現(xiàn)
這篇文章主要介紹了自定義Vue中的v-module雙向綁定的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04
Vue from-validate 表單驗(yàn)證的示例代碼
本篇文章主要介紹了Vue from-validate 表單驗(yàn)證的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-09-09
vue-cli3使用 DllPlugin 實(shí)現(xiàn)預(yù)編譯提升構(gòu)建速度
這篇文章主要介紹了vue-cli3使用 DllPlugin 實(shí)現(xiàn)預(yù)編譯提升構(gòu)建速度 ,需要的朋友可以參考下2019-04-04

