简体   繁体   English

vue.js中相同级别的组件之间的双向绑定

[英]Two way binding between components at same level in vue.js

While using the typeahead https://github.com/pespantelis/vue-typeahead can I do two binding between search term of two typeahead, so basically it should not matter into which search box the user is typing but both should return data but from different endpoints? 在使用typeahead https://github.com/pespantelis/vue-typeahead时,我可以在两个typeahead的搜索词之间进行两次绑定,因此基本上用户输入的搜索框无关紧要,但是两者都应从中返回数据,不同的端点?

<typeahead ep="http://localhost:8080/ep1"></typeahead>
<typeahead ep="http://localhost:8080/ep2"></typeahead>

Yes. 是。 Just pass the same value to both components. 只需将相同的值传递给两个组件。

<typeahead ep="foo" v-model="someValue"></typeahead>
<typeahead ep="bar" v-model="someValue"></typeahead>

Then in your component when you this.$emit the value you pass back to the parent both will update. 然后,当您执行this.$emit时,在您的组件中this.$emit传递回父级的值都将更新。

Here's a very minimal example: 这是一个非常简单的示例:

Vue.component('typeahead', {
  template: '<div><input v-model="displayValue"> on {{ endpoint }}</div>',
  props: {
    value: String,
    endpoint: String,
  },
  computed: {
    displayValue: {
      get () {
        return this.value
      },
      set (value) {
        this.$emit('input', value)
      },
    },
  },
})

and here it is on a fiddle with some common scenarios like changing the value from parent, making sure the parent data updates as you change the child, etc: https://jsfiddle.net/crswll/tLmszrp2/ 在这里它摆弄着一些常见的场景,例如从父级更改值,确保在更改子级时父级数据更新等: https//jsfiddle.net/crswll/tLmszrp2/

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

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