在vue中created、mounted等方法使用小结
created:html加载完成之前,执行。执行顺序:父组件-子组件
mounted:html加载完成后执行。执行顺序:子组件-父组件
methods:事件方法执行
watch:watch是去监听一个值的变化,然后执行相对应的函数。
computed:computed是计算属性,也就是依赖其它的属性计算所得出最后的值
exportdefault{
name:"draw",
data(){//定义变量source
return{
source:newol.source.Vector({wrapX:false}),
}
},
props:{//接收父组件传递过来的参数
map:{
//type:String
},
},
mounted(){//页面初始化方法
if(map==map){
}
varvector=newol.layer.Vector({
source:this.source
});
this.map.addLayer(vector);
},
watch:{//监听值变化:map值
map:function(){
console.log('3333'+this.map);
//returnthis.map
console.log('444444'+this.map);
varvector=newol.layer.Vector({
source:this.source
});
this.map.addLayer(vector);
}
},
methods:{//监听方法click事件等,执行drawFeatures方法
drawFeatures:function(drawType){}
}
补充知识:vue中在mounted中window.onresize不生效
在vue开发中,因为引用的父组件和子组件都使用了window.onresize以至于一个window.onresize失效。
解决方案==>可以采用下面的方式
window.onresize=()=>this.screenWidth=window.innerWidth
//改为以下写法
window.addEventListener('resize',()=>this.screenWidth=window.innerWidth,false)
以上这篇在vue中created、mounted等方法使用小结就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持毛票票。