如何从子组件更新父组件中的属性

时间:2019-01-08 21:31:55

标签: vuejs2

我有一个具有数据属性(布尔型)的父组件,如下所示:

<script>
import PmhnEntryAll from './components/PmhnEntryAll'
import {store} from './store.js'


export default {
  name: 'PmhnApp',
  components: {
    PmhnEntryAll,
  },
  data: function () {
    return {
      vPmhnEntryAll: store.componentVisibility.vPmhnEntryAll,
    }
  },
}
</script>

,在子组件中,我要将vPmhnEntryAll设置为“ true”。我该怎么办?

1 个答案:

答案 0 :(得分:2)

通常使用父级的v-on:和子级的$emit

查看https://vuejs.org/v2/guide/components-custom-events.html中有关事件发生的文档

您还可以更进一步,v-bind是事件侦听的一种形式,但是它只能用于什么应用程序。 https://vuejs.org/v2/guide/components-custom-events.html#Binding-Native-Events-to-Components

您还可以传递一个函数来更新变量,但这被认为是反模式,不建议这样做。

根据项目的结构,您可以使用总线或vuex来管理全局范围。

相关问题