基于vue框架
在mounted()函数中,初始化某一个元素的css样式,发现无法成功。
解决办法:使用nextTick
this.$nextTick()函数作用是等页面的数据更新完成以后,它再执行内部回调函数中的逻辑
参考如下:
<div>
<span ref="red">我需要初始化颜色</span>
</div>
new Vue({
el: '#app',
mounted: function () {
this.init()
},
methods: {
init() {
this.$nextTick(() => {
this.$refs.red.style.color = 'red'
})
}
}
})