refInFor这个属性是vue原生自带的还是elementUI里面的属性?

阅读 7.1k
2 个回答
// assign the `ref` is used on elements/components with v-for
refInFor

这是一个 createElement 函数中的数据对象属性,当通过循环例如map生成一个 vNode 数组时,开启 refInFor后,在父组件中通过this.$refs.xxx 获取到的值是一个数组。

如果不开启,获取到的是最后一个元素或者子组件。

// 子组件
Vue.component("todo", {
  props: ["todo"],
  // createElement 可简写 h
  render(h) {
    console.log("todo");
    console.log(this.todo);
    return h("li", this.todo);
  }
});
Vue.component("my-todo", {
  props: ["items"],
  render(h) {
    console.log("my-todo");
    if (this.items.length) {
      const todo = this.items.map((item) => {
        return h("todo", {
          props: { todo: item.do },
          ref: "ref-li",
          refInFor: true // 使用map生成一个vNode数组,开启 refInFor: true,this.$refs.["ref-li"] 是一个数组
        });
      });
      return h("ol", todo);
    } else {
      return h("p", "no todo");
    }
  },
  mounted() {
    // 在父组件中获取注册了 ref 特性的子组件的
    console.log(this.$refs["ref-li"]);//数组
  }
});

在数据对象中开启该选项,相当于在模板中使用 v-forref 结合生成子组件:

<todo ref="ref-li" v-for="(do,index) in todoList" :key="index">{{do}}<todo>

demo:

codepen查看效果

请采纳。思否的编辑器真是一坨xxx

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题