vue在个组件中给body加样式,或者给父级组件元素加样式
在h5项目中,经常遇到不同组件需要不同背景色,通过vue的生命周期来解决。这组件创建前设置我的样式,销毁之前移除我设置的样式。用以下方式给body添加样式:<script>export default {beforeCreate: function() {document.getElementsByTagName("body")[0].class...
·
在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>
更多推荐
已为社区贡献26条内容
所有评论(0)