在Vue中,可以通过$refs来访问组件中的DOM元素或组件实例。因此,可以在组件中使用$refs来
调用
另一个方法。
代码示例:
<tem
pl
ate>
<button @click="testCallback">Test Callback</button>
</tem
pl
ate>
<s
cr
i
pt
>
export default {
methods: {
testMethod() {
console.log("testMethod called");
testCallback() {
setTimeout(() => {
this.$refs.testMethodRef();
}, 1000);
mounted() {
console.log(this.$refs.testMethodRef);
</s
cr
i
pt
>
在这个示例中,我们在组件中定义了两个方法:testMethod和testCallback。testCallback方法使用setTimeout
函数
来模拟异步操作,并在1秒后
调用
另一个方法testMethod。在testCallback方法中,我们通过this.$refs来访问testMethod方法,并在它后面加上Ref,这是因为在tem
pl
ate模板中我们使用v-ref指令将testMethod绑定到ref上。最后,在mounted钩子
函数
中,我们可以访问DOM元素或组件实例的引用。