inleft
2022-08-26 fa1bd95d533444d7360d1ada127b7a3279a3901f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// 子组件childA
 
<template>
    <div id="childA">
        <h1>我是A组件</h1>
        <button @click="transform">点我让B组件接收到数据</button>
        <p>因为你点了B,所以我的信息发生了变化:{{BMessage}}</p>
    </div>
</template>
 
<script>
    export default {
        data() {
            return {
                AMessage: 'Hello,B组件,我是A组件'
            }
        },
        computed: {
            BMessage() {
                // 这里存储从store里获取的B组件的数据
                return this.$store.state.BMsg
            }
        },
        methods: {
            transform() {
                // 触发receiveAMsg,将A组件的数据存放到store里去
                this.$store.commit('receiveAMsg', {
                    AMsg: this.AMessage
                })
            }
        }
    }
</script>