简体   繁体   English

v-select同步修改器将ID为ID的文本从子级传递到父级

[英]v-select sync modifier pass text with id from child to parent

I have the following v-select in child component. 我在子组件中具有以下v-select。

<VSelect
                label="Attribute"
                :items="addOnLabelItems"
                :value="addOnLabelId"
                @input="
                  $emit('update:addOnLabelId', $event)
                "
                solo
                outline
                reverse
                type="text"
              />

in parent, I got .sync modifier for addOnLabelId . 在父级中,我为addOnLabelId获得了.sync修饰符。 Turns out that $event passed in $emit is just only id of the select user chose. 事实证明,传递给$emit $ event只是所选用户的ID。

Question 1): How can I also pass text with the id together? 问题1):如何将带有ID的文本一起传递?

Question 2) is it possible to include another sync modifier for v-select so that when select changes, it throws two $emits (one that throws id, and one that throws text) and in parent component, i can catch those separately? 问题2)是否可以为v-select包含另一个同步修饰符,以便在select更改时,它引发两个$emits (一个引发id,一个引发文本),并且在父组件中,我可以分别捕获它们?

You can pass in objects as items in a v-select , but you have to associate the value and the text by using item-text and item-state . 您可以在v-select中将对象作为项目传入,但必须使用item-textitem-state文本关联起来。

And if you want an object back, then you have to also add the return-object attribute to the v-select . 如果要返回对象,则还必须将return-object属性添加到v-select ( https://vuetifyjs.com/en/components/selects#customized-item-text-and-value ) https://vuetifyjs.com/en/components/selects#customized-item-text-and-value

return-object described in the Vuetify docs: Vuetify文档中描述的返回对象

Changes the selection behavior to return the object directly rather than the value specified with item-value 更改选择行为以直接返回对象,而不是使用item-value指定的值

HTML/Vue: HTML / VUE:

<div id="app">
  <v-container fluid grid-list-xl>
    <v-layout wrap align-center>
      <v-flex xs12 sm6 d-flex>
        <v-select
          :items="items"
          label="Attributes"
          item-text="state"
          item-value="abbr"
          @input="atInput($event)"
          @change="atInput($event)"
          return-object
        ></v-select>
      </v-flex>
    </v-layout>
  </v-container>
</div>

And the JS: 和JS:

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data: {
    items: [
      { state: 'Florida', abbr: 'FL' },
      { state: 'Georgia', abbr: 'GA' },
      { state: 'Nebraska', abbr: 'NE' },
      { state: 'California', abbr: 'CA' },
      { state: 'New York', abbr: 'NY' },
    ],
  },
  methods: {
    atInput(event) {
      console.log(event)
    }
  }
})

Here's a working Codepen: https://codepen.io/mukagergely/pen/wVJLyQ 这是一个有效的Codepen: https ://codepen.io/mukagergely/pen/wVJLyQ

If you look at the console in the Codepen, then it logs that the $event returned is an object . 如果您查看Codepen中的控制台,那么它将记录返回的$ event是一个object

I also added @input and @change to the v-select , but you don't need both. 我还在v-select中添加了@input@change ,但是您不需要两者。

I hope this helps you solve your problem. 我希望这可以帮助您解决问题。

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

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