Vue常見錯誤及解決方法:
1.[Vue-warn]: Missing required prop: "to"(found in component )
這個錯誤是少了個to或者是寫錯 ,正確寫法為:
并且路由在做字符串拼接的時候,to要作為一個屬性綁定
2.端口沖突錯誤:需要改端口,當(dāng)然現(xiàn)在vue2.0中的webpack 已經(jīng)自己會根據(jù)你的端口號進(jìn)行改正,從8080往后面進(jìn)
行遞增,不會發(fā)生端口號沖突的情況,在vue1.0中會經(jīng)常出現(xiàn)。
3.[Vue-warn]:Unknown custom element: - did you register the component correctiy?
錯誤1:引進(jìn)來的vue-router沒有use()
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter);
錯誤2:在生成路由實例之后,沒有將路由掛到我們的Vue實例上面
const router=new VueRouter({
mode:'history',//切換路徑模式,變成history模式,不然路徑為/#/home
scrollBehavior:()=>({ // 滾動條滾動的行為,不加這個默認(rèn)就會記憶原來滾動條的位置
y:0
}),
// 注意這里的名稱
routes
});
new Vue({
/* 4.最后掛到vue上 */
router,
el: '#app',
render: h => h(App)
});
4.Uncaught TypeError: _vuex2.default.store is not a constructor
這個報錯的是_vuex2.default.store 不是一個構(gòu)造函數(shù)
因為在我們用vuex的時候需要將用到的actions,mutations模塊最終導(dǎo)出,
在導(dǎo)出的時候new Vuex.Store中的Store小寫了,這里的一定要大寫,
就相當(dāng)于我們在使用構(gòu)造函數(shù)(類)的時候首字母要大寫
import mutations from './mutations.js'
import actions from './actions.js'
export default new Vuex.Store({ //Vue.Stroe()首字母大寫
modules:{ //這里注意mutations導(dǎo)出的是一個模塊
mutations
},
actions
});
5.Moudel not found:Error:Can't resolve "style" in 'D:\vue-demo'
在vue1.0中,在webpack.config.js中配置css文件時
module:{
loaders:[
{
test:/\.css$/,
loader:'style!css'
}
]
}
在vue2.0中,在webpack.config.js中配置css文件時,必須要寫全,不能和vue1.0一樣簡寫
module:{
rules:[ //這里改成了rules
{
test:/\.css$/,
loader:'style-loader!css-loader' //這里必須要寫全,不能和vue1.0一樣簡寫
}
]
}
6.組件之間的通信從1.0過渡到2.0時引發(fā)的錯誤:
vue1.0實現(xiàn)父子組件的通信 -->通過props屬性-->并且子組件可以更改父組件的數(shù)據(jù) 通過sync同步,
當(dāng)在vue2.0里面不允許直接給父級數(shù)據(jù)做更改,并且把這個方法.sync去掉了,
當(dāng)子組件再試圖更改父組件的數(shù)據(jù)時,就會報錯。
解決方法:
1.$emit()——單一事件管理
經(jīng)常遇到的問題是找不到$emit()或$on(),這時需要單獨準(zhǔn)備一個文件Store.js
在文件里面需要:var oEvent =new Vue();
這個這個文件里的數(shù)據(jù)一定要導(dǎo)出去才可以使用:export default oEvent
2.對象之間的引用:(推薦使用)
vue1.0傳數(shù)據(jù):msg:'welcome' -->傳給子級
vue2.0直接將數(shù)據(jù)定義成對象json的形式,這樣傳給子級的數(shù)據(jù)是對象的屬性,即msg.title
這樣子級修改父級的數(shù)據(jù),修改的也是這個對象的一個屬性msg.title
msg:{
title:'welcome'
}
msg.title
7.用vuex用來管理組件狀態(tài):(增加/減少,顯示/隱藏)
8.axios目前不可以use,因為axios里面沒有install這個方法
使用axios的時候,可以這樣來使用:
1.將axios導(dǎo)入文件
import axios from 'axios'
2.將axios放入到Vue實例上面,這樣在其他組件中,可以直接通過this.$https.get/post使用
在main.js中寫:Vue.prototype.$http = axios
其他組件可以直接使用:
this.$http.get('data.txt').then((res)=>{
console.log(res.data);
}).catch((err)=>{
console.log(err);
});
9.element.ui表頭點擊事件
使用element.ui之后 @click="" 無法對表頭等元素添加點擊事件,正確的寫法應(yīng)該是@click.native=""
10.webpack2.0
插件的配置需要放到 plugins里面進(jìn)行配置,不可放到rules里面進(jìn)行配置