vue-select : 从 VueJs 中的祖父组件传递选项插槽

vue-select : Passing option slot from grandparent component in VueJs

我正在使用自定义下拉组件 'vue-select',它有一个插槽选项,我们可以通过它自定义选项视图,如本文档所示 -> https://vue-select.org/guide/slots.html

我想通过从祖父组件传递一个插槽来实现类似的事情。 这是我尝试过的。

App.vue(祖父母成分)

<template>
  <div id="app">
    <v-select-wrapper v-model="selectedData" :options-data="[{
        id:1,
        label: 'New York'
      }, {
        id:2,
        label : 'London'
      }]">
      <template v-slot:option-data="option">
        {{option.id}} -
        {{ option.label }}
      </template>
    </v-select-wrapper>
  </div>
</template>

VSelectWrapper.vue(Parent分量)

<template>
  <v-select :options="optionsData" :value="value" @input="inputChanged">

    <template v-slot:option="option">
      <slot name="option-data"/>
    </template>
  </v-select>
</template>

<script>
  import vSelect from "vue-select";

  export default {
    name: "VSelectWrapper",
    components: {
      vSelect
    },
    props: {
      optionsData: {type: Array},
      value: {}
    },
    methods: {
      inputChanged(val) {
        this.$emit("input", val);
      }
    }
  };
</script>

我收到的输出只是下拉选项中的“-”(连字符)字符。数据未通过插槽传递。

如何实现?

你的插槽道具没有被传下来的原因是你没有在插槽上绑定任何东西以供 children 从中拾取。为此,您只需添加 v-bind="option",其中 optionvue-select 组件本身的插槽 属性:

VSelectWrapper.vue

<v-select
  :options="optionsData"
  :value="value"
  @input="inputChanged">
  <template v-slot:option="option">
    <slot name="option-data" v-bind="option"></slot>
  </template>
</v-select>