使用计算属性连接vuex的变量,在用watch去监听,变相的实现监听vuex内部state变化

<template>
    <div class="page2">
        <button @click="togglepage2">点击切换</button>
    </div>
</template>

<script>
    export default {
        name: 'page2',
        data() {
            return {}
        },
        computed: {
            listenshowpage1() {
                return this.$store.state.showpage2;
            }
        },
        watch: {
            listenshowpage1: function(a, b) {
                console.log("修改前卫:" + a);
                console.log("修改后为:" + b);
            }
        },
        methods:{
            togglepage2:function(){
                this.$store.commit({
                    type:"togglepage2"
                })
            }
        }
    }
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>