mounted() {
this.$nextTick(this.init);
},
methods: {
init() {
let imgDomList = document.getElementsByTagName('img');
Array.prototype.slice.call(imgDomList,0).forEach((img, index) => {
......................
})
})
}
}
问题出现了:打印imgDomList的值没问题,但是imgDomList.length 为0,imgDomList[0]为undefined,实际上是有值的,当然转为数组也是空数组[]。
最后不得已用了setTimeout(this.init, 1000)
是因为this.$nextTick(this.init);放错地方了,正确代码如下:
mounted() {
this.docEntry = this.$route.query.docEntry;
this.classify = this.$route.query.classify;
let formdata = new FormData();
formdata.append("action", "ReceiveDetail");
formdata.append("docEntry", this.docEntry);
formdata.append("classify", this.classify);
formdata.append("userCode", localStorage.getItem("UserCode"));
this.$store
.dispatch("receiveDetail", formdata)
.then(res => {
this.htmls = res[0].U_ContentHtml;
this.title = res[0].U_Title;
this.$nextTick(this.init) //必须放在then里
})
.catch(err => this.$toast(err, "error"));
// setTimeout(this.init, 1000);
// this.$nextTick(this.init); //之前放在这里是错误的
},