简体   繁体   English

从 VueJS 中的不同组件调用 v-model

[英]Call v-model from a different component in VueJS

How to call a v-model that is placed in a different component?如何调用放置在不同组件中的 v-model? The search text-field is placed in the App.vue and I need to trigger the :search='search' in the Datatable.vue搜索文本字段放置在App.vue中,我需要触发Datatable.vue中的:search='search'

App.vue code: App.vue 代码:

    <v-text-field
    v-model="search"
    class="mx-3"
    label="Suche"
    ></v-text-field>

Datatable.vue code:数据表.vue 代码:

<v-data-table
ref='dTable'
:items='products'
:search='search'
class='elevation-1'
>

There is a number of different ways you can share data between your components.您可以通过多种不同的方式在组件之间共享数据。

In your case, since you have a the v-text-field bound to your search -data, you can simply pass it along to your Datatable component.在您的情况下,由于您有一个绑定到您的search数据的v-text-field ,您可以简单地将它传递给您的Datatable组件。

<div id="app">
  <v-text-field v-model="search" ...  />
  <Datatable v-bind:search="search" ... />
</div>

Then you have to "catch" and use it in your Datatable component.然后你必须“捕捉”并在你的Datatable组件中使用它。


Other ways of sharing data:其他数据共享方式:

Service bus服务总线

One of the ways is creating a serviceBus , to emit changes to different parts of your application.其中一种方法是创建serviceBus ,以向应用程序的不同部分发出更改。 Take a look at this blog post .看看这篇博文

In your case, you would set up a listener for serviceBus.$on('searchInitiated', (query) => { ... }) in your data table component - and in the search component trigger the search by calling serviceBus.$emit('searchInitiated', this.search) ;在您的情况下,您将在数据表组件中为serviceBus.$on('searchInitiated', (query) => { ... })设置一个侦听器 - 并在搜索组件中通过调用serviceBus.$emit('searchInitiated', this.search)触发搜索serviceBus.$emit('searchInitiated', this.search)

This lets you share component state across different components, independent of hierarchy.这使您可以在不同的组件之间共享组件状态,而与层次结构无关。


Passing Data to Child Components with Props使用 Props 将数据传递给子组件

Another way of "sharing data", is letting a "high level component" keep track of your state - then pass the data on to child components . “共享数据”的另一种方式是让“高级组件”跟踪您的状态 - 然后将数据传递给子组件

This might get cumbersome, if you have a deeply nested component hierarchy - as you have to pass the prop from component to component.如果你有一个深度嵌套的组件层次结构,这可能会变得很麻烦——因为你必须将 prop 从一个组件传递到另一个组件。


Sending Messages to Parents with Events通过事件向父母发送消息

You can also emit values from a child component ( Search ) to the parent ( App ), then pass this data on to your DataTable .您还可以从子组件( Search ) 向父组件 ( App ) 发出值,然后将此数据传递给您的DataTable

A variation of the "Service bus" example, differing on that there is no "shared bus" - you have to specifically attach the callback to your child component. “服务总线”示例的变体,不同之处在于没有“共享总线” - 您必须专门将回调附加到您的子组件。


Share state via vuex通过vuex共享状态

You can also utilize vuex to share state between your components.您还可以利用vuex在组件之间共享状态。 Here is a great guide for getting started.是一个很好的入门指南。

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

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