$nextTick() 获取的DOM类数组取值有问题(已解决)

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);  //之前放在这里是错误的
  },
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值