讲起这个主要是在一些微服务框架中可以进行状态管理的再封装
1.首先在man.js里边(当然也可以单独拿出来)
//最小化的跨组件状态管理器,一处变化全局改变
const obser = Vue.observable({
count: 0,
name:"qc",
})
Vue.prototype.$obser =obser;
2.在具体组件中
1.template
<button
@click="setCount"
style="margin-top:50upx;width:100%"
class="cu-btn bg-blue lg">
count自增:{{count}}
</button>
<button
@click="goTest"
style="margin-top:50upx;width:100%"
class="cu-btn bg-blue lg">
go test
</button>
2.js
computed: {
count(){
return this.$obser.count
}
},
methods:{
setCount(){
this.$obser.count++;
},
goTest(){
uni.navigateTo({ url: '/pages/test/index' })
},
}
在其它组件中使用方法一样,状态会在多组件之间共享