简体   繁体   English

Vue.js 中父组件如何与子组件通信?

[英]How can a parent component communicate with a child component in Vue.js?

This is what I have:这就是我所拥有的:

<div id='vnav-container'>
    <input type="text" v-model="searchTerm" v-on:keyup="search" class="vnav-input">
    <menu :items="menu"></menu>
</div>

The outer component contains a search-input and a menu component. outer组件包含一个search-input和一个menu组件。

When the user performs a search on the outer component, I need to call a method on the menu component, or emit an event, or whatever, as long as I can communicate to the menu component saying it should filter itself based on the new criteria.当用户在outer组件上执行搜索时,我需要在menu组件上调用一个方法,或者发出一个事件,或者其他什么,只要我可以与menu组件通信,说它应该根据新标准过滤自己.

I've read somewhere that calling methods on child components is discouraged and that I should use events.我在某处读到不鼓励在子组件上调用方法,我应该使用事件。 I'm looking at the docs right now, but I can only see an example of a child talking to a parent, not the other way around.我现在正在查看文档,但我只能看到一个孩子与父母交谈的例子,而不是相反。

How can I communicate to the menu component as the search criteria changes?当搜索条件发生变化时,我如何与菜单组件进行通信?

EDIT编辑

According to some blog posts, there used to be a $broadcast method intended to talk to child components but the documentation about that just vanished.根据一些博客文章,曾经有一个$broadcast方法旨在与子组件对话,但关于它的文档刚刚消失。 This used to be the URL: http://vuejs.org/api/#vm-broadcast这曾经是 URL: http : //vuejs.org/api/#vm-broadcast

The convention is " props down, events up ".惯例是“道具向下,事件向上”。 Data flows from parents to child components via props, so you could add a prop to the menu, maybe:数据通过 props 从父组件流向子组件,所以你可以在菜单中添加一个 prop,也许:

<menu :items="menu" :searchTerm="searchTerm"></menu>

The filtering system (I'm guessing it's a computed?) would be based on searchTerm , and would update whenever it changed.过滤系统(我猜它是计算出来的?)将基于searchTerm ,并且会在它发生变化时更新。

When a system of components becomes large, passing the data through many layers of components can be cumbersome, and some sort of central store is generally used.当组件系统变大时,通过多层组件传递数据可能会很麻烦,通常会使用某种中央存储。

Yes, $broadcast was deprecated in 2.x.是的, $broadcast在 2.x 中已被弃用。 See the Migration guide for some ideas on replacing the functionality (which includes event hubs or Vuex ).有关替换功能(包括事件中心或Vuex )的一些想法,请参阅迁移指南

Or you can create the kind of simple store for that.或者您可以为此创建一种简单的商店。

First off, let's create the new file called searchStore.js it would just VanillaJS Object首先,让我们创建一个名为searchStore.js的新文件,它只是 VanillaJS 对象

export default {

    searchStore: { 
        searchTerm: '' 
    } 


}

And then in files where you are using this store you have to import it然后在您使用此商店的文件中,您必须导入它

import Store from '../storedir/searchStore'

And then in your component, where you want to filter data, you should, create new data object然后在您要过滤数据的组件中,您应该创建新的数据对象

data() {
   return {
      shared: Store.searchStore
   }
}

About methods - you could put method in your store, like this关于方法 - 你可以把方法放在你的商店里,就像这样

doFilter(param) {
  // Do some logic here
}

And then again in your component, you can call it like this然后再次在您的组件中,您可以像这样调用它

methods: {
   search() {
      Store.doFilter(param)
   }
}

And you are right $broadcast and $dispatch are deprecated in VueJS 2.0你是对的 $broadcast 和 $dispatch 在 VueJS 2.0 中被弃用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM