自学内容网 自学内容网

this.$set

用于改数组,但页面没有更新
例如:
this.originPageList[i].screenType = “tall”; 不行
就用 this.$set(this.originPageList[i], ‘screenType’, ‘wide’);

    for (let i in this.originPageList) {
        const img = this.originPageList[i].coverImgUrl;
        // 创建一个 Image 对象
        const imageObj = new Image();

        // 设置图片的 src 属性为当前显示图片的 src
        imageObj.src = img;

        // 当图片加载完成后获取宽度和高度
        imageObj.onload = () => {
          const width = imageObj.width;
          const height = imageObj.height;

          // 判断宽高比例,根据实际需求调整比例阈值
          const ratio = width / height;

          if (ratio > 1) {
            // this.imageRatios[index] = "wide"; // 宽长
            // this.originPageList[i].screenType = "wide"; // 宽长
            this.$set(this.originPageList[i], 'screenType', 'wide');
          } else if (ratio < 1) {
            // this.imageRatios[index] = "tall"; // 高长
            // this.originPageList[i].screenType = "tall"; // 宽长
            this.$set(this.originPageList[i], 'screenType', 'tall');

          } else {
            // this.imageRatios[index] = "square"; // 正方形
            // this.originPageList[i].screenType = "square"; // 宽长
            this.$set(this.originPageList[i], 'screenType', 'square');

          }

原文地址:https://blog.csdn.net/weixin_45148022/article/details/136043675

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!