vue.js - 传入一个有引用值(value)的slot?

标签 vue.js vuejs2

在我的组件中,我有这个作用域插槽:

<slot name="test">
    <input ref="inputTest">
</slot>

在 parent 中我这样做:

<div slot="test">
    <input ref="inputTest">
</div>

但是当我稍后尝试在我的组件中访问 ref 时:

console.log(this.$refs.inputTest);

我不确定。

如何传入一个有引用的槽?

最佳答案

您不能从父组件到子组件访问 refs

您可以使用 scoped slot在它们之间传递数据。

<!-- pass ref as props -->
<slot name="test" :ref="inputTest">
    <input ref="inputTest">
</slot>


<!-- receive ref props -->
<template slot-scope="ref">
    <!-- bind ref to $refs -->
    <input ref="ref">
</div>

这显然会令人困惑。因此,我建议为 props 使用任何其他合适的名称而不是 ref。

关于vue.js - 传入一个有引用值(value)的slot?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49492389/

相关文章:

json - Vue 获取 response.json promise 不会创建响应式(Reactive) setter/getter

javascript - vue @click 函数 on::after css element - close btn

forms - 如何在 VeeValidate 中自定义错误消息(字段名称)?

vue.js - 浏览器控制台中的错误,对/sockjs-node/info的请求?t = 1555629946494

node.js - 尽管使用 CORS,对 expressjs 的 POST 请求仍无法正常工作

javascript - 属性或方法 "sendResetMail"未在实例上定义,但在渲染期间引用

javascript - 在不同的屏幕尺寸上重新排序 DOM 元素

javascript - 如何修复 "the requested module does not provide an export named ' default'"?

javascript - 如果在初始化根中的对象时未显式声明,则组件 v for 无法识别 prop

vue.js - Vuejs - 如何在点击时进行 v-for 循环