[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
<>this.$refs获取不到子组件
很多时候我们通过this.$refs.xxx去获取一个子组件的时候,会发现是undefined,有以下几个原因,也只可能是以下几个原因:
1.在mounted中调用
调用时机,this.$refs的使用需要在mounted中使用且最好加上this. $nextick来获取子组件。
原因:该子组件在mounted中可能还没渲染完成,加nextick可以保证已经渲染完成了
2.你需要获取的组件身上或者父元素身上带有v-if,需要把v-if更换成v-show(这个是最容易忘记的,也是本次做笔记的原因)
例如:
<div class="tree_box" v-show="treeShow"> <div class="tree"> <el-tree :data="
treeData" :props="defaultProps" @node-click="(data, node, cur) =>
nodeClick(data, node, cur, item.prop)" default-expand-all show-checkbox node-key
="id" ref="inputTree" :expand-on-click-node="false" > <span class="
custom-tree-node" slot-scope="{ node }"> <span class="em-tree-text" :title="
node.label">{{ node.label }}</span> </span> </el-tree> </div> <div class="
close_btn"> <el-button type="primary" @click="closeBox">关闭</el-button> </div> </
div>
我是想去获取inputTree这个子组件,但是因为之前我一直在
.tree_box身上写的v-if(且v-if的值为false,页面上就没有这个元素),导致了我想拿这个子组件一直获取不到。
3.this.$refs.xxx获取到的是一个数组。
这种情况是因为你要获取的子组件身处在一个v-for的循环中,你只需要利用数组的下标取出就行
总结,如果你注意到了第一点,那么最大的可能就是因为v-if的原因(即你的子组件没渲染到,你就去拿了)!