Vue的状态管理

从零打造简单状态管理

状态管理

如果你有一处需要被多个实例间共享的状态,你可以使用一个 reactive 方法让对象作为响应式对象

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>状态管理</title>
    <script src="https://unpkg.com/vue@next"></script>
</head>
<body>
    <div id="app-a">App A: {{ message }}</div>

    <div id="app-b">App B: {{ message }}</div>

    
    <script>
        const {createApp,reactive} =Vue
        const sourceOfTruth=reactive({
            message:"Hello"
        })

        const appA=createApp({
            data(){
                return sourceOfTruth
            },

        }).mount("#app-a")

        const appB=createApp({
            data(){
                return sourceOfTruth
            },
            mounted(){
                sourceOfTruth.message='GoodBye'
            }
        }).mount("#app-b")
    </script>
</body>
</html>

如何能更好的解决这个问题可以引入store模式,状态都放置在 store 自身的 action 中去管理

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>引入store</title>
    <script src="https://unpkg.com/vue@next"></script>
</head>

<body>
    <div id="app-a">{{sharedState.message}}</div>

    <div id="app-b">{{sharedState.message}}
        <button @click="clearState"></button>
    </div>
    <script>
        const {createApp,reactive} =Vue
        const store = {
            debug: true,
            state: reactive({
                message: 'Hello!'
            }),
            setMessageAction(newValue) {
                if (this.debug) {
                    console.log('setMessageAction triggered with', newValue)
                }
                this.state.message = newValue
            },
            clearMessageAction() {
                if (this.debug) {
                    console.log('clearMessageAction triggered')
                }
                this.state.message = ''
            }
        }

        const appA = createApp({
            data() {
                return {
                    privateState: {},
                    sharedState: store.state
                }
            },
            mounted() {
                store.setMessageAction('Goodbye!')
            }
        }).mount('#app-a')

        const appB = createApp({
            data() {
                return {
                    privateState: {},
                    sharedState: store.state
                }
            },
            methods:{
                clearState(){
                    console.log("准备清除数据");
                    store.clearMessageAction()
                    console.log("打印清清除后的结果",store.state.message);
                    store.setMessageAction("这是测试数据")
                    console.log("设置了新值",store.state.message);
                }
            }
        }).mount('#app-b')

    </script>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值