简体   繁体   English

vue js 安装的生命周期挂钩内的 getElementById 导致 null

[英]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语句,这导致input1nulltestStatusDropDown 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.

相关问题 每个 vue.js 生命周期挂钩的用例是什么? - What is the use case for each vue.js life cycle hooks? 什么时候在 Vue 的自定义指令中调用 bind 和 unbind 以及它们与生命周期函数的关系(挂载/销毁) - When does bind and unbind called in custom directives of Vue and how are they related to life-cycle functions (mounted / destroyed) V-斗篷的Vue.js生命周期挂钩 - Vue.js life-cycle hook for v-cloak 如何修复 [Vue 警告]:挂载钩子中的错误:“TypeError: document.getElementById(...) is null”(在<root> )?</root> - How to fix [Vue warn]: Error in mounted hook: “TypeError: document.getElementById(…) is null” (found in <Root>)? getElementById返回null? (使用Vue) - getElementById returns null? (with Vue) 如何在已安装的vue js中使用数据值 - how to use data value inside mounted vue js Vuex this。$ store在“已安装”生命周期挂钩中未定义 - Vuex this.$store undefined in “mounted” life cycle hook 使用Vue在生命周期中传递不同的组件 - Passing different components with life cycle using vue Vue 生命钩子中的先前和当前数据 - Previous and current data in Vue life hooks 如果不使用 created() 生命周期钩子或展开运算符 (...),则 vue.js 中的空白屏幕 - Blank screen in vue.js if not using created() life cycle hook or spread out operator (...)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM