解決vue創(chuàng)建項目使用vue-router和vuex報錯Object(...)is not a function
vue創(chuàng)建項目使用vue-router和vuex報錯Object(...)is not a function
之前創(chuàng)建項目使用還好好的,這次想新建個項目練練手,才用了vue-router就報錯了,記錄下自己修改了一個下午的bug,淚目了(其實是版本問題,小小劇透)
報錯:
Object(...) is not a fuction
創(chuà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)項目時使用的腳手架和語法有沖突,又重新創(chuàng)了幾次項目。。。。
真正的原因是vue-router,vuex和vue的版本不匹配。
解決方法
卸載vue-router和vuex npm uninstall vue-router
和npm uninstall vuex
安裝匹配版本,我這里可以運行的版本是vue-router: “^3.5.2”, vuex: “^3.6.2”。
輸入命令npm install vue-router@3.5.2
和npm install vuex@3.6.2
當然不是重新安裝就完事兒了,重點是再輸入npm install
然后bug就完美解決啦~
附上版本
總結(jié)
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
element-ui使用el-date-picker日期組件常見場景分析
最近一直在使用 element-ui中的日期組件,所以想對日期組件常用的做一個簡單的總結(jié),對element-ui el-date-picker日期組件使用場景分析感興趣的朋友一起看看吧2024-05-05vue3.x?的shallowReactive?與?shallowRef?使用場景分析
在Vue3.x中,`shallowReactive`和`shallowRef`是用于創(chuàng)建淺層響應(yīng)式數(shù)據(jù)的API,它們與`reactive`和`ref`類似,本文介紹vue3.x??shallowReactive?與?shallowRef的使用場景,感興趣的朋友一起看看吧2025-02-02vue-cli3使用 DllPlugin 實現(xiàn)預(yù)編譯提升構(gòu)建速度
這篇文章主要介紹了vue-cli3使用 DllPlugin 實現(xiàn)預(yù)編譯提升構(gòu)建速度 ,需要的朋友可以參考下2019-04-04