【问题标题】:Vue - Proper way to modify slots before rendering - Goal: add transition group key programaticallyVue - 在渲染之前修改插槽的正确方法 - 目标:以编程方式添加转换组键
【发布时间】:2019-03-06 17:06:06
【问题描述】:

我有一个包装器组件,它只是一个 <transition-group> 组件,通过其默认插槽接受内容。通过默认slot传递的内容是一系列二级vue组件。

由于过渡组中的项目需要键控,我将如何将键添加到插槽项目?由于它们是开槽而不是在 v-for 循环中呈现,我认为需要以编程方式添加键。

下面是一个可以玩的 sn-p。您将在 created() 挂钩中看到我的方法,该方法似乎适用于初始页面加载/渲染,但是当我的开发环境中的热重载刷新时,密钥丢失并且有关转换组子项需要密钥的错误又回来了.

有没有更好的方法来实现这一点,让热重载快乐?也许我不应该担心热重载,因为它只是一个开发功能,但我的想法是,如果热重载不喜欢我的方法,那么我可能做错了,可能有更好的方法。

想法?

我也只是好奇在生命周期中何时是对插槽节点进行修改的正确时间。此外, node.key 是应用唯一键的正确位置吗?插槽节点中的哪个属性是要编辑的正确属性? (即,当在 v-for 循环中设置键时,组件数据属性中还设置了一个“键”)

非常感谢您提供的任何见解!

Vue.component('wrapper-component', {
  render(h) {
    return h('transition-group', this.$slots.default)
  },
  
  // my attempt at providing a unique key for transition children is in created() hook below
  // this works on itital page load/rendering - but breaks when hot-reload refreshes my development site
  // uncomment created() hook to see my approach in action - works here on page reload, but does not hold up with hot-reload (i.e. once hot-reload refreshes, the transition items no longer have their unique keys)
  
/*  
  created() {
    this.$slots.default = this.$slots.default.map((node, index) => {
      node.key = `${node.tag}-${index}`
      return node
    })
  }
*/
  
});

Vue.component('child-item', {
  render(h) {
    return h('div', this.$slots.default)
  }
});

new Vue({
  el: "#app"
});
div {
 display: block;
 width: 100%;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>

<div id="app">
  <wrapper-component>
    <child-item>My Child 1</child-item>
    <child-item>My Child 2</child-item>
    <child-item>My Child 3</child-item>
    <child-item>My Child 4</child-item>
  </wrapper-component>
</div>

【问题讨论】:

    标签: javascript vue.js


    【解决方案1】:

    尝试添加beforeUpdate钩子:

      beforeUpdate() {
        this.$slots.default = this.$slots.default.map((node, index) => {
          node.key = `${node.tag}-${index}`
          return node
        })
      }
    

    【讨论】:

    • 我想就是这么简单。我需要做更多的测试才能确定,并且 created() 和 beforeUpdate() 钩子中需要逻辑,但到目前为止看起来不错!似乎编辑 node.key 正在工作,所以我想我会相信这是完成我需要的一个好方法。有趣的是,我的头脑如何期望事情变得复杂,而事实并非如此!非常感谢您的洞察力!
    猜你喜欢
    • 2021-11-24
    • 2016-10-20
    • 1970-01-01
    • 2013-01-04
    • 2020-08-12
    • 2021-05-16
    • 2018-04-03
    • 2012-10-03
    • 1970-01-01
    相关资源
    最近更新 更多