简体   繁体   English

如何访问创建的钩子中的插槽道具?

[英]How to access slot props in the created hook?

I'm trying to access properties I'm passing on to my slot.我正在尝试访问要传递给我的插槽的属性。 But my slotProps are undefined.但是我的slotProps是未定义的。

As I'm still new to Vue and I've read their docs I still can't seem to figure out why I can't access the props data.由于我还是 Vue 的新手并且我已经阅读了他们的文档,我似乎仍然无法弄清楚为什么我无法访问道具数据。

Problem问题

I'm trying to access the slotProps in my child components created , but it's undefined我正在尝试访问子组件中的slotProps created ,但它是未定义的

emphasized text强调文本

<template>
  <div>
    <slot :data="data" :loading="loading"></slot>
  </div>
</template>

Child孩子

<template v-slot:default="slotProps">
  <div >

  </div>
</template>

<script>
export default {
  name: "child"
  created: function() {
    console.log("slotProps", slotProps);
  }
};
</script>

You can use this object to follow with your child property您可以使用object 跟随您的子属性

demo: https://stackblitz.com/edit/vue-kkhwzc?file=src%2Fcomponents%2FHelloWorld.vue演示: https://stackblitz.com/edit/vue-kkhwzc?file=src%2Fcomponents%2FHelloWorld.vue

Updated code Child更新代码

<template v-slot:default="slotProps">
  <div >

  </div>
</template>

<script>
export default {
  name: "child"
  created: function() {
    console.log("slotProps", this.slotProps);
  }
};
</script>

You do not need the created() life cycle hook to achieve what you want.你不需要created()生命周期钩子来实现你想要的。 There are few things to clear up:有几点需要澄清:

  • What you are using is actually called scoped slots .您使用的实际上称为scoped slots They are useful because, unlike when using the default and named slots, the parent component can not access the data of its child component(s).它们很有用,因为与使用默认插槽和命名插槽不同,父组件无法访问其子组件的数据。

  • What you call a Child is actually the parent component.您所说的Child实际上是父组件。

Child.vue component should be something like this: Child.vue组件应该是这样的:

<template>
  <div>
    <main>
      <slot :data="data1" :loading="loading1" />
    </main>
  </div>
</template>

<script>
export default {
  name: 'Page',
  data () {
    return {
      data1: 'foo',
      loading1: 'bar'
    }
  }
}
</script>

In a Parent.vue component, you can access the data of the above component as follows:在一个Parent.vue组件中,您可以通过如下方式访问上述组件的数据:

<template>
  <child>
    <template v-slot="slotProps">
      {{ slotProps.data }},
      {{ slotProps.loading }}
    </template>
  </child>
</template>

<script>
import Child from '@/components/Child.vue'

export default {
  components: { Child }
}
</script>

Or you can also destruct the objects on the fly as follows:或者您也可以按如下方式动态销毁对象:

<template>
  <child>
    <template v-slot="{data, loading }">
      {{ data }},
      {{ loading }}
    </template>
  </child>
</template>

<script>
import Child from '@/components/Child.vue'

export default {
  components: { Child }
}
</script>

This is the clean way to access data of a child component from the parent using scoped slots.这是使用作用域插槽从父组件访问子组件数据的干净方式。

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

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