简体   繁体   English

vuejs 插槽对从父级传递的动态数据没有反应

[英]vuejs slot are not reactive to dynamic data being passed from parent

I am passing a variable (from parent component) to slot of a child component.我正在将一个变量(从父组件)传递给子组件的插槽。 During initial rendering this variable get correctly displayed.在初始渲染期间,此变量会正确显示。

But when the variable being passed changes in parent, this change is not reflected in child component, below is my code但是当传递的变量在父组件中发生变化时,这种变化不会反映在子组件中,下面是我的代码

<template>
  <div>
    <v-client-table
     :columns="columns"
     v-model="data"
     :options="options"
     :class="{ 'ready' : isReady }"
    >
    <div slot="h__select">
      <input type="checkbox" @change="selectAll($event)" 
      :checked="allSelected" />
    </div>
 ....

The variable is "allSelected".变量是“allSelected”。 How can I bind the "checked" attribute to the value of "allSelected" variable.如何将“checked”属性绑定到“allSelected”变量的值。 I have searched extensively but couldn't find anything similar.我进行了广泛的搜索,但找不到类似的东西。 Please help.请帮忙。

Use the following technique:使用以下技术:

<template>
<!-- This is the higher-order component V-CLIENT-TABLE -->
  <table>
    <thead>
      <tr>
        <th>
          <slot name="h__select" :allSelected="all" />
        </th>
        <!-- Produce the header here -->
      </tr>
    </thead>
    <tbody>
      <tr v-for="(item,index) in items" :key="index">
        <slot :item="item" />
      </tr>
    </tbody>
  </table>
</template>

<script>
export default
{
  name: 'VClientTable',
  props:
  {
    items:
    {
      type: Array,
      default: () => []
    },
    columns:
    {
      type: Array,
      default: () => []
    },
  },
  data()
  {
    return {
      all: false,
    };
  },
  watch:
  {
    all(newValue, oldValue)
    {
      // update checkboxes for all rows
    }
  }
}
</script>
<template>
<!-- This is the component where you use V-CLIENT-TABLE -->
  <div>
    <v-client-table
     :columns="columns"
     v-model="data"
     :options="options"
     :class="{ 'ready' : isReady }"
    >
    <div slot="h__select" slot-scope="data">
      <input type="checkbox" @change="data.allSelected = !data.allSelected" 
      :checked="data.allSelected" />
    </div>
  </div>
</template>

<script>
export default
{
  name: 'ChildComponent',
}
</script>

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

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