美文网首页
vue使用observable组件传值

vue使用observable组件传值

作者: _undefined | 来源:发表于2019-09-26 13:22 被阅读0次

demo目录

Parent.vue
Child.vue
store.js

store.js

import Vue from 'vue'

export const store = Vue.observable({
    count: 0
})

export const mutations = {
    addCount () {
        store.count = store.count + 1
    },
    minusCount () {
        store.count = store.count - 1
    }
}

Parent.vue

<template>
    <div>
        <p>parent: {{ store.count }}</p>
        <button @click.prevent="handleAdd">父组件 - 加</button>
        <button @click.prevent="handleMinus">父组件 - 减</button>
        <Child></Child>
    </div>
</template>

<script>
import { store, mutations } from './store'
import Child from './Child'
export default {
    name: 'ParentPage',

    components: {
        Child
    },

    data () {
        return {
            store: store
        }
    },

    methods: {
        handleAdd () {
            mutations.addCount()
        },
        handleMinus () {
            mutations.minusCount()
        }
    }
}
</script>

Child.vue

<template>
    <div>
        <p>child: {{ store.count }}</p>
        <button @click.prevent="handleAdd">子组件 - 加</button>
        <button @click.prevent="handleMinus">子组件 - 减</button>
    </div>
</template>

<script>
import { store, mutations } from './store'
export default {
    name: 'ChildPage',

    data () {
        return {
            store: store
        }
    },

    methods: {
        handleAdd () {
            mutations.addCount()
        },
        handleMinus () {
            mutations.minusCount()
        }
    }
}
</script>

Vue-observable

相关文章

  • vue使用observable组件传值

    demo目录 store.js Parent.vue Child.vue Vue-observable

  • 组件通信

    vue传值可分为父子之间传值、兄弟组件之间传值、跨代组件之间传值 1.父子之间传值:可以使用$emit/props...

  • vue初步学习---组件弹出框的实现

    当你使用终端命令建立vue项目时候,我们可能需要父组件向孙组件传值,或者非父子组件传值,或子组件向父组件传值的时候...

  • vue通信方式

    vue组件通信方式 使用props父传子 定义child组件 父组件传值