export function getRgb() {
let r = Math.floor(Math.random() * 256);
let g = Math.floor(Math.random() * 256);
let b = Math.floor(Math.random() * 256);
// return `rgb(${r},${g},${b})`;
return 'rgb('+r+','+g+','+b+')'
}
// Math.random是生成0-1之间的随机数 *256 的范围就变成0.xx-255.7
// Math.floor 向下取整就变成 0-255
changeBg() {
let rgb = getRgb();
this.$store.commit("changeBg", rgb);
},
methods: 在methods定义方法传到vuex
changeBg(state, val) {
state.bgcolor = val;
},
在vuex中接收传过来的方法并赋值
<div class="header" :style="`background-color:${$store.state.bgcolor}`">></div>
在需要改变的地方引入