Vue開發中遇到的問題與解決方案(一)
阿新 • • 發佈:2019-01-02
vue生命週期函式:http://www.zhimengzhe.com/Javascriptjiaocheng/236707.html
1、在watch或者created裡面操作dom,用this.$nextTick(function(){
xxxx
}) 2、class動態繫結 三元寫法: :class="[isShowTab?'showTab':'noShowTab']" 3、vue獲取後端資料應該在created還是mounted方法: 看情況了,一般放到created裡面就可以了,這樣可以及早發請求獲取資料,如果有依賴dom必須存在的情況,就放到mounted(){this.$nextTick(() => { /* code */ })}裡面
4、watch裡面函式寫法不同this指代不同
(1)
tabName:(newVal)=>{
console.log(this, 'this')
}
(2) tabName:function(newVal) { console.log(this, 'this')
} 上面的方法(1)不能用this.$nextTick,會報錯,方法(2)可以用this.$nextTick 5、 window.addEventListener('scroll',function(){ 在這個裡面使用this.data 提示未定義,請問如何訪問data(){return {}}的資料
試試 用 ()=> {}或者:
this.$data或者:
let vm =this ;window.addEventListener('scroll',function(vm ){})這樣 你就能 訪問 vue 的 this
6、render函式的使用 render:function(createElement){ return createElement( 'h'+this.level, { 'class':{ show:true, hide:false, }, style:{
width:'200px',
height:'400px',
background:'red',
},
attrs:{
name:'h-ex',
id:'h-id'
},
props:{
myprops:true,
},
on: {
click: function(event){
}) 2、class動態繫結 三元寫法: :class="[isShowTab?'showTab':'noShowTab']" 3、vue獲取後端資料應該在created還是mounted方法: 看情況了,一般放到created裡面就可以了,這樣可以及早發請求獲取資料,如果有依賴dom必須存在的情況,就放到mounted(){this.$nextTick(() => { /* code */ })}裡面
(2) tabName:function(newVal) { console.log(this, 'this')
} 上面的方法(1)不能用this.$nextTick,會報錯,方法(2)可以用this.$nextTick 5、 window.addEventListener('scroll',function(){ 在這個裡面使用this.data 提示未定義,請問如何訪問data(){return {}}的資料
6、render函式的使用 render:function(createElement){ return createElement( 'h'+this.level, { 'class':{ show:true, hide:false, }, style:{
alert(this.num) } }, nativeOn:{
click:function(event) {
alert(1111)}
}
}, [
this.$slots.myslot,createElement('div',{
domProps:{innerHTML:'holle render'}
})
]) }