如何从 vue 3 中的 slot 获取 ref?
我需要将名称为 test1 的 ref 集中起来,设置一些值,该值放置在组件槽中(从外部)。有可能以某种方式做到这一点吗?我尝试从 $refs 或 $slots 获取,但失败了。
应用程序.vue
<template>
<div id="app">
<HelloWorld>
<input type="text" ref="test1" />
</HelloWorld>
</div>
</template>
```
<script>
import HelloWorld from './components/HelloWorld.vue';
export default {
name: 'App',
components: {
HelloWorld,
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
组件.vue
<template>
<slot></slot>
<hr />
<input type="text" ref="test2" />
</template>
<script>
export default {
name: 'HelloWorld',
mounted() {
this.$refs.test2.value = 'test 2 value';
// how get ref 'test1' ?
},
};
</script>
i need to to focus ref with name test1 a set some value which is placed in compontend slot (from outside). Is it possible to do it somehow? I tried to get from $refs or $slots, but failed.
App.vue
<template>
<div id="app">
<HelloWorld>
<input type="text" ref="test1" />
</HelloWorld>
</div>
</template>
```
<script>
import HelloWorld from './components/HelloWorld.vue';
export default {
name: 'App',
components: {
HelloWorld,
},
};
</script>
<style>
#app {
font-family: Avenir, Helvetica, Arial, sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
text-align: center;
color: #2c3e50;
margin-top: 60px;
}
</style>
Component.vue
<template>
<slot></slot>
<hr />
<input type="text" ref="test2" />
</template>
<script>
export default {
name: 'HelloWorld',
mounted() {
this.$refs.test2.value = 'test 2 value';
// how get ref 'test1' ?
},
};
</script>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以简单地通过事件或道具传递引用。这是使用您的示例的解决方案。
You can simply pass a ref through an event or a prop. Here's a solution using your example.
在 Vue3 中,您可以使用函数引用和作用域槽。
<一href="https://sfc.vuejs.org/#eNqtUs1OwzAMfhUrHLZJrBUcSzeBeAOQOOXSde7WkSZRko5D1XfHbtqOnhASN/9+/vzZnXixNrm2KDKR+9LVNoDH0Nq91HVjjQvQgcMKeqic aWBFpas59a5M+GgxpqRIUg4wmhRSS10a7QmOYm+EsGOc9WYOY+DqmFmj2sBuD53UMDUk10IR9g5QPUndS52nkSBRIydgY1URkDyAfGQyOOROSbgrz7U67qToeCAP66WIVTLIkB/aEI yGjKhR0UQjlrI3FcfyAezWnMZu5kMuB5akBjkiq0VK3Iso4LYpbHLxRpP6w+pyTHgpsi gGxwZBMzLOIVifpamvSlb54hPjTilZiWt1qBtM0DfbgzNfHh0B8xMANTY9jZxu87dL//s Vp06jX1VdfnLbrYlTRmGizGm9AKCBv3wA78FyWYfeQ/XwCMGAsaihgCNeUZHtKGaUj8eJDTmPAV00SPeP9xWQ0Vrb+BK3BaVIY8f4M88l86eScZP5VX4sMagQx82/snyF/ht/+DNb" rel="nofollow noreferrer">示例
In Vue3, you can use function refs and scoped slot.
example
我有!
链接:https://stackblitz.com/edit/vue-jynies?文件=src/App.vue
I have it!
Link: https://stackblitz.com/edit/vue-jynies?file=src/App.vue
您在 App.vue 中定义了
,因此您需要执行
this.$refs.test1.focus()
也在 App.vue 中。您可以使用 vue:mounted 监听并使用父组件的 Mounted 事件。App.js
Component.vue
You define
<input type="text" ref="test1" />
in App.vue so you need to executethis.$refs.test1.focus()
in the App.vue too. You can listen and use parent component's mounted event using vue:mounted.App.js
Component.vue