在h5项目中,经常遇到不同组件需要不同背景色,通过vue的生命周期来解决。
这组件创建前设置我的样式,销毁之前移除我设置的样式。
用以下方式给body添加样式:
<script>
export default {
beforeCreate: function() {
document.getElementsByTagName("body")[0].className="bg";
},
beforeDestroy: function() {
document.body.removeAttribute("class","bg");
}
}
</script>
<style>
.bg{
background: #000;
}
</style>