简体   繁体   English

vuejs 中不存在引用

[英]refs nonexistent in vuejs

I want to trigger a button click programmatically in vuejs with a javascript method and I have the following line to reference it via the ref tag.我想使用 javascript 方法在 vuejs 中以编程方式触发按钮单击,我有以下行通过 ref 标记引用它。 below is my javascript下面是我的 javascript

this.$refs.action.click();

Below is the html for the button下面是按钮的 html

<button v-show="false" ref="action" type="button" data-toggle="modal" data-target="#MonitorModal" />

Is there a reason this isn't working?这是行不通的原因吗? I believe it's all declared fine and when I run a console.log on this.$refs I see the action as anfield, but when I try to reference it, I get undefined我相信这一切都被声明为正常,当我在 this.$refs 上运行 console.log 时,我将操作视为 anfield,但是当我尝试引用它时,我得到了 undefined

this.$refs 的图片

the elements are only available after the initial mount of the component if you are using vue 2 try running your code on mounted life cycle hook.如果您使用的是 vue 2,则元素仅在组件初始安装后可用,请尝试在已mounted的生命周期挂钩上运行您的代码。

mounted() {
  this.$refs.action.click();
}

in vue 3 use onMounted life cycle hook.在 vue 3 中使用onMounted生命周期钩子。

const action = ref()

onMounted(()=>{
action.value.click()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM