lijh
2022-01-13 ad2b70777b866a9f4848a89fd0e343f991f8a604
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
// 子组件 childB
 
<template>
    <div id="childB">
        <h1>我是B组件</h1>
        <button @click="transform">点我让A组件接收到数据</button>
        <p>因为你点了A,所以我的信息发生了变化:{{AMessage}}</p>
    </div>
</template>
 
<script>
    export default {
        data() {
            return {
                BMessage: 'Hello,A组件,我是B组件'
            }
        },
        computed: {
            AMessage() {
                // 这里存储从store里获取的A组件的数据
                return this.$store.state.AMsg
            }
        },
        methods: {
            transform() {
                // 触发receiveBMsg,将B组件的数据存放到store里去
                this.$store.commit('receiveBMsg', {
                    BMsg: this.BMessage
                })
            }
        }
    }
</script>