[英]getElementById inside the vue js mounted life cycle hooks results in null
I've been getting null
when I try to see what's inside an id with document.getElementById
.当我尝试使用document.getElementById
查看 id 中的内容时,我得到了null
。 Part of the code is below:部分代码如下:
<input-layout v-if="edit" label="Status" class="grayout">
<u-select v-model='test.status' :options="testStatus" tabindex="14" class="grayout" id="testStatusDropDown"/>
</input-layout>
<input id="input1" type="checkbox">
Note that, v-if="edit"
results in true.请注意, v-if="edit"
结果为真。
In the mounted
life cycle hook I have:在已mounted
的生命周期挂钩中,我有:
mounted () {
this.$nextTick(function (){
console.log(document.getElementById("input1"))
console.log(document.getElementById("testStatusDropDown"))
})
},
Originally I didn't have the console.log
statements in the mounted method which resulted in null
for both input1
and testStatusDropDown
.最初我在安装的方法中没有console.log
语句,这导致input1
和null
的testStatusDropDown
。 I understand that because both elements do not exist yet when that part of the code runs.我理解这一点,因为当这部分代码运行时,这两个元素都不存在。 So then I moved them into the mounted method, now, I can see <input id="input1" type="checkbox">
but for the second log statement I get null
.所以我把它们移到了挂载的方法中,现在,我可以看到<input id="input1" type="checkbox">
但是对于第二个日志语句,我得到null
。
I went to the vue js mounted api docs, which can be found here: https://v2.vuejs.org/v2/api/#mounted .我去了 vue js 安装的 api 文档,可以在这里找到: https://v2.vuejs.org/v2/api/#mounted 。 From what I can see is that mounted
does not guarantee all child components have been mounted so I need to use this.$nextTic
inside the mounted
method.据我所知, mounted
并不能保证所有子组件都已安装,因此我需要在mounted
方法中使用this.$nextTic
。 However, even after adding this.$nextTick
I'm still having the same issue where document.getElementById("testStatusDropDown")
results in null
when I expect it to be <u-select v-model='test.status':options="testStatus" tabindex="14" class="grayout" id="testStatusDropDown"/>
.但是,即使在添加this.$nextTick
之后,我仍然遇到相同的问题,即document.getElementById("testStatusDropDown")
导致null
当我期望它是<u-select v-model='test.status':options="testStatus" tabindex="14" class="grayout" id="testStatusDropDown"/>
。
Is my expectation incorrect?我的期望不正确吗? What do I need to change such that document.getElementById("testStatusDropDown")
does not return null
?我需要更改什么以使document.getElementById("testStatusDropDown")
不返回null
?
Use ref to reference an element in vuejs.使用 ref 引用 vuejs 中的元素。
<input id="input1" ref="input1" type="checkbox">
and in mounted access it using:并在安装访问它使用:
this.$refs.input1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.