vue:ref的作用和实例
warning:
这篇文章距离上次修改已过247天,其中的内容可能已经有所变动。
ref
在 Vue 中主要用来访问 DOM 元素或组件实例。
- 当
ref
用于 DOM 元素时,ref
引用的是真实 DOM 元素实例,可以通过this.$refs.refName
来访问。 - 当
ref
用于组件实例时,ref
引用的是组件实例,可以通过this.$refs.refName
来访问组件的方法和数据。
实例代码:
在这个例子中,focusInput
方法通过 this.$refs.inputRef.focus()
使文本输入框获得焦点,通过 this.$refs.myComponentRef.someMethod()
调用了子组件 my-component
的 someMethod
方法。
评论已关闭