最近写东西遇到了莫名其妙的问题,用ref属性原以为它会获得一个数组
// html
<ul>
<li ref="refCon">1</li>
<li ref="refCon">2</li>
</ul>
// js
created: function() {
this.$nextTick(() => {
console.log(this.$refs.refCon)
// 打印结果:<li>2</li> 本以为会获得一个数组
})
}
后来去看了下文档才搞明白orz。(没仔细看文档的锅

这下就明白了
// html
<ul>
<li v-for="item in people" ref="refContent">{{item}}</li>
</ul>
// js
data: {
people:['三姑','四婶','五叔','六姨','七舅姥爷']
},
created: function() {
this.$nextTick(() => {
console.log(this.$refs.refContent)
})
}

以及一定要注意
// js
data: {
people:['三姑','四婶','五叔','六姨','七舅姥爷']
},
created: function() {
console.log(this.$refs.refContent)
// undefined
}
网友评论