[英]How to get to HTML element of component from another component Vue
here is the situation. 这是情况。 Have a component(cmp1) that inside of it uses search bar component (cmp2).
有一个内部使用搜索栏组件(cmp2)的组件(cmp1)。 My goal is to dig into
cmp2
and grab input
element to set focus on it when cmp1
renders. 我的目标是深入研究
cmp2
并在cmp1
渲染时抓取input
元素以将焦点放在该元素上。 How do I achieve it with Vue
? 如何使用
Vue
实现它? What have I tried so far that worked was to set ref
on cmp2
, than through this.$refs.search.$el.firstChild[0].focus()
but I realize that this is not an option. 到目前为止,我尝试过的工作是在
cmp2
上设置ref
,而不是通过this.$refs.search.$el.firstChild[0].focus()
但是我意识到这不是一个选择。 So, perhaps you'd help me out with this one. 所以,也许您会帮我解决这个问题。 some screenshots attached.
附有一些屏幕截图。 Many thanks!
非常感谢!
You don't need to manually set these by accessing the child html elements. 您无需通过访问子html元素来手动设置它们。
If I understand correctly, you want to execute a function of the child triggered by the parent. 如果我理解正确,那么您想执行由父级触发的子级功能。
One way to do that is to have the parent have a loaded
state default to false. 一种方法是让父级具有默认为false的
loaded
状态。 Pass the state as a prop to the child, and then you can add a watch for this state to the child. 将状态作为道具传递给孩子,然后可以向孩子添加此状态的手表。 When the loaded changes to true, your watch should trigger the desired action.
当加载更改为true时,手表应触发所需的动作。
You can complicate things a bit by having an emit in the child pass a function or the input to the parent that you can manage from there. 您可以通过在子代中传递一个可以从那里管理的函数或输入给父代,来使事情复杂化。
But as mentioned in the comment, this doesn't need to be a complicated rective situation. 但是,正如评论中所提到的,这不必是一个复杂的叙述情况。 I think what may suffice is having a
isFocused
prop available to the parent. 我认为父母可以使用
isFocused
道具就足够了。
example for #1: #1的示例:
in parent: 在父母中:
<template>
<my-component :isFocused="focusedElement === 'item-1'"/>
</template>
<script>
data () {
focusedElement: null
},
mounted() {
this.focusedElement = 'item-1'
}
</script>
in child/component: 在子/组件中:
<script>
params: ['isFocused'],
watch: {
isFocused(val){
if (val === true) {
this.$refs.input.focus()
}
}
}
</script>
example for #2: #2的示例:
to emit a function, you could do this in the child 发出一个功能,你可以在孩子
methods: {
setFocus() { this.$refs.input.focus() }
}
mounted() {
this.$emit('onMounted', this.setFocus);
},
then, in the parent you would use the @onMounted
listener to register the function. 然后,在父级中,您将使用
@onMounted
侦听器注册该函数。 you would still need to store the function and manage when the function fires from the parent. 您仍然需要存储功能并管理何时从父级启动功能。 I think option one is a better choice
我认为选项一是更好的选择
Create method in search-payment-component
and ref input
element: 在
search-payment-component
和ref input
元素中创建方法:
methods: { ... focus() { this.$refs.input.focus() } ... }
And call this method in parent component with this.$refs.searchComponent.focus()
(yes, you should also ref child component to use it). 并使用
this.$refs.searchComponent.focus()
在父组件中调用此方法(是的,还应该引用子组件以使用它)。
Add prop focused
to search-payment-component
: 将
focused
添加到search-payment-component
:
props: ['focused'], ... watch: { focused(val) { val && this.$refs.input.focus() } }
But you should also provide events when focus is lost as best practice. 但是,您也应该提供失去焦点的事件作为最佳实践。
[UPDATED] OR if you want just focus input on initial rendering: [更新]或者,如果您只想将输入集中在初始渲染上:
props: { focused: Boolean },
...
mounted() {
focused && this.$refs.input.focus();
}
// in parent template
...
// if you just declared attribute(w/o value) it will be true
<search-payment-component focused ><search-payment-component>
...
Also if you want to work with child component in mounted
hook you should use it in this.$nextTick
method to be sure that child component was rendered. 另外,如果要在
mounted
挂钩中使用子组件,则应在this.$nextTick
方法中使用它,以确保已渲染子组件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.