【问题标题】:How to get data from a component in VueJSVueJS如何从组件中获取数据
【发布时间】:2017-07-29 19:30:53
【问题描述】:

我有以下组件:

组件

<template>
<div>
  <label class="typo__label">Single selecter</label>
  <multiselect v-model="value" :options="options" :searchable="false" :close-on-select="false" :show-labels="false" placeholder="Pick a value"></multiselect>
</div>
</template>

<script>
import Multiselect from 'vue-multiselect'

export default {
  components: {
    Multiselect
  },
  data () {
    return {
      value: '',
      options: ['Select option', 'options', 'selected', 'mulitple', 'label', 'searchable', 'clearOnSelect', 'hideSelected', 'maxHeight', 'allowEmpty', 'showLabels', 'onChange', 'touched']
    }
  }
}
</script>

我在我的页面中这样使用它:

<p id="app-two">
  <dropdown></dropdown>
  @{{ value }}
  @{{ message }}
</p>

<script>
    new Vue({
    el: '#app',
    data: {
        message: 'Test message'
    }
});
</script>

当我运行页面时,@{{ message }} 显示“测试消息”,但 @{{ value }} 为空白。 我可以看到下拉组件的值在 VueJS 开发工具中得到更新,但它没有显示在页面上。如何访问我的 vue 元素中的组件数据?类似@{{ dropdown.value }}

【问题讨论】:

    标签: vuejs2 components


    【解决方案1】:

    这对我来说是最好和最干净的方式。

    父组件

    <template>
        <ChildComponent v-model="selected" />
    </template>
    

    子组件

    <template>
        <Multiselect
            :value="value"
            :options="options"
            :show-labels="false"
            :multiple="true"
            :close-on-select="false"
            :clear-on-select="false"
            :searchable="false"
            :limit="1"
            :limit-text="(count) => $t('app.multiselect.and_n_more', { n: count })"
            :placeholder="$t('app.select')"
            label="name"
            track-by="name"
            @input="$emit('input', $event)"
        />
    </template>
    
    <script>    
    export default {
        props: {
            value: {
                type: Array,
                default: () => []
            }
        },
        data() {
            return {
                options: [{id:1, name:'John'}, {id:2, name:'Tom'}]
            }
        }
    }
    </script>
    

    【讨论】:

      【解决方案2】:
      <template>
          <div>
            <label class="typo__label">Single selecter</label>
            <multiselect v-model="internalValue" :options="options" :searchable="false" :close-on-select="false" :show-labels="false" placeholder="Pick a value">  </multiselect>
          </div>
      </template>
      
      <script>
          import Multiselect from 'vue-multiselect'
      
          export default {
            components: {
              Multiselect
            },
            props: ['value'],
            data () {
              return {
                internalValue: this.value,
                options: ['Select option', 'options', 'selected', 'mulitple', 'label', 'searchable', 'clearOnSelect', 'hideSelected', 'maxHeight', 'allowEmpty', 'showLabels', 'onChange', 'touched']
              }
            },
            watch:{
               internalValue(v){
                   this.$emit('input', v);
               }
            }
          }
      </script>
      

      在你的页面中

      <p id="app-two">
        <dropdown v-model="selectedValue"></dropdown>
        @{{ selectedValue}}
        @{{ message }}
      </p>
      
      <script>
          new Vue({
          el: '#app',
          data: {
              selectedValue: null
              message: 'Test message'
          }
      });
      </script>
      

      Here is an example,不使用多选,而是实现对v-model的支持的自定义组件。

      【讨论】:

      • 嗨,没有从组件中获取数据。我想我需要使用道具,但我不知道它在这里是如何工作的。谢谢
      • @ClintonGreen 我误读了您的代码。看起来您正在将多选包装在您自己的自定义组件中?在这种情况下,您可能希望在组件中实现 v-model 支持,或者,您可以只发出值。
      • @ClintonGreen 更新了示例以实现自定义组件的 v-model。
      猜你喜欢
      • 1970-01-01
      • 2019-08-02
      • 2016-07-11
      • 1970-01-01
      • 2017-09-26
      • 2020-04-04
      • 2021-11-18
      • 2019-08-30
      • 2023-03-11
      相关资源
      最近更新 更多