常见问题解决 - 路由切换定时器问题,父组件传数据给轮播图子组件问题、异步请求执行顺序、关闭Eslint、devtools不能使用

news/2024/7/10 1:55:57 标签: Vue, 问题, 解决方案

文章目录

      • 1. 路由切换定时器问题 - 常见轮播图
      • 2. 父组件传数据给轮播图子组件问题
      • 3. 异步请求发送不按执行顺序解决
      • 3. 异步请求导致数组不一致解决方案
      • 4. 异步请求尽量放在created(){} 生命周期里面而不是mounted(){}
      • 5. 关闭ESLint语法检查 - 实在是太严格了
      • 6. Devtools不能在控制台使用的情况 - 解决方案

1. 路由切换定时器问题 - 常见轮播图

<script>
    
    // 将轮播图的定时器交给组件管理
    data() {
        return {
            timer: {}
        }
    },
    
    methods: {   
        play() {
            this.timer = setInterval(function() {
            }, 2000);
        }    
    },
    
    // 组件挂载时,播放轮播图
    mounted() {
        this.play();
    },
    
    // 路由切换时,自动会销毁组件 - 故执行停止定时器
    beforeDestory() {
        clearInterval( this.timer );
    }
    
</script>

2. 父组件传数据给轮播图子组件问题

AutoPhotoPlayer2.vue - 轮播图子组件

<template>
  <div class="photo-player">
    <ul class="photo-player-lists">
      <li v-for="(value, key) in photos" v-if="key==index" :key="key"><img :src="value"/></li>
    </ul>

    <ul class="photo-player-button">
      <li v-for="(value, key) in photos" :class="['white', {'active': key == index}]" @click="index = key"
          :key="key"></li>
    </ul>
  </div>
</template>


<script>
  export default {
    name: "AutoPhotoPlayer",
    data() {
      return {
        index: 0,
        length: 0,
        
        //用来停止轮播图定时器,重点
        timer: {}
      }
    },
    props: ["photos", "photosLength"],
    methods: {
      change() {

        let that = this;

        this.timer = setInterval(() => {
          that.index++;
          
          // 必须在内部写这个每时每刻都要从data获取长度 - that.photosLength
          if (that.index == that.photosLength) {
            that.index = 0;
          }

        }, 1000*2);


      }
    },
    created() {
      this.change();
    },
    beforeDestroy() {
      clearInterval(this.timer);
    }
  }
</script>

<style scoped>
  html, body {
    margin: 0;
    padding: 0;
  }

  .photo-player {
    width: 100%;
    height: 170px;
    overflow: hidden;
    position: relative;
  }

  .photo-player-lists {
    list-style-type: none;
    margin: 0;
    padding: 0;
    width: 100%;
    height: 100%;
  }

  .photo-player-lists li {
    list-style-type: none;
    width: 100%;
    height: 100%;
  }

  .photo-player-lists li img {
    width: 100%;
    height: 100%;
  }

  .photo-player-button {
    position: absolute;
    margin: 0;
    padding: 0;
    bottom: 30px;
    left: 198px;
    list-style-type: none;
  }

  .photo-player-button li {
    list-style-type: none;
    width: 10px;
    height: 10px;
    margin-left: 5px;
    margin-right: 5px;
    border-radius: 6px;
    float: left;
    cursor: pointer;
  }

  .white {
    background: #ffffff;
  }

  .active {
    background: #0000ff;
  }
</style>

home.vue - 父组件

<template>
  <div class="home" >
    <auto_photo_player :photos="autoPhotos" :photosLength="updatePhotosLength"/>
  </div>
</template>

<script>
	
	// 导入轮播图组件
  import AutoPhotoPlayer from "@/components/index/AutoPhotoPlayer2.vue"

  export default {
    name: 'Home',
    data() {
      return {
        autoPhotos: [],
      }
    },
    components: {
      auto_photo_player: AutoPhotoPlayer,
    },
    methods: {
      clearTime: function(){
        clearInterval();
      }
    },
    computed: {
        updatePhotosLength: {
          get: function() {
            return this.autoPhotos.length;
          }
        }
    },
    created: async function () {
		
		// 从后台请求轮播图的图片
      var {data: result} = await this.$http.get("http://localhost:8080/apiGateway/advService/advs");
      result.data.forEach((item, index) => {
        if (item.type_id == null) {
          this.autoPhotos.push(this.constantValue.nginxPrefix + item.img);
        }
      })

    }

  }
</script>


3. 异步请求发送不按执行顺序解决

添加setTimeout进行延迟发送

  if(token != null && token != undefined && fromPath == "/carts" && fromPath != targetPath ) {
    
    // 这里发送了3条异步请求更新数据
    $store.dispatch("aUpdateCartsList");
    if($store.state.CartUpdateFlag == true) {
        
        // 这里需要重新拉取服务器的数据。必须要延迟加载 - 0.5秒
        setTimeout(function() {
          console.log("======1秒后 - 符合重新向服务器拉取请求")
          $store.dispatch("aGetCartsList");
        }, 500)

    }
  }


3. 异步请求导致数组不一致解决方案

var $this = this;
if (index != -1) {
          $this.products.push(item);
          // 这里进行获取数组长度
          var length1 = this.products.length;
          var {data: result} = await $this.$http.get("http://localhost:8080/apiGateway/productService/products/" + item.product_id);

          setTimeout(function () {
           // 赋值给这里使用 - 避免数组不一致的情况
           var length = length1;
           $this.products[length-1] = result
        }
}

4. 异步请求尽量放在created(){} 生命周期里面而不是mounted(){}

5. 关闭ESLint语法检查 - 实在是太严格了

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-7lqetBYD-1587886927895)(en-resource://database/29970:1)]

6. Devtools不能在控制台使用的情况 - 解决方案

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-9rtydRtu-1587886927915)(en-resource://database/33202:1)]

原因:引用的js文件是生产压缩的vue.min.js文件
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Y7BeW89L-1587886928102)(en-resource://database/33204:1)]


使用源码vue.js文件即可

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-SjlNb43c-1587886928110)(en-resource://database/33206:1)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-ovrfwfYx-1587886928117)(en-resource://database/33208:1)]


http://www.niftyadmin.cn/n/1657865.html

相关文章

怎么在Linux中查看fork函数代码,Linux中fork()调用的源代码在哪里?

以x86平台和2.6.23 Linux内核为参考&#xff1a;>创建test-fork.c文件&#xff1a;#include>使用静态链接编译它&#xff1a;gcc -O0 -static -Wall test-fork.c -o test-fork>拆卸它&#xff1a;objdump -D -S test-fork>测试fork.dis>打开test-fork.dis文件并…

今晚去参加了ZTE IP产品线的活动

在城东的向阳渔港&#xff0c;因为是“外人”了&#xff0c;所以只是见了见老朋友&#xff0c;吃了几口奢侈的菜肴&#xff0c;就走了&#xff0c;呵呵。 虽然我离开了这个团体&#xff0c;但还是衷心希望IP产品能拨开迷雾&#xff0c;扬眉吐气。转载于:https://www.cnblogs.co…

Vue-Router路由学习

文章目录简单使用路由重定向嵌套路由动态路由命名路由编程式导航路由前置守卫//获取当前路由地址 this.$route.path//编程式路由 this.$router.push("路由地址");简单使用 <body><div id"app"><!--2. 添加路由链接--><router-link t…

linux使用yum安装ftp,linux配置本地yum安装--FTP yum

1&#xff1a;首先将ISO文件拷贝到服务器上2&#xff1a;进行ISO文件挂载mount -o loop /backup/soft/rhel-server-6.4-x86_64-dvd.iso /mnt/cdrom/3&#xff1a;配置yum[rootdb_test yum.repos.d]# cat local.repo[base]namelocalbaseurlfile:///mnt/cdrom/gpgcheck0enabled1v…

一个很好的开源CRM项目

一个很好的开源CRM项目 http://www.sugarcrm.com,还有简体中文包&#xff0c;是个CRM软件&#xff0c;PHP搞的&#xff0c;打算开始研究之posted on 2005-01-15 19:56 jackyrong的世界 阅读(...) 评论(...) 编辑 收藏 转载于:https://www.cnblogs.com/jackyrong/archive/2005/…

今天游戏修改的麻烦

继续修改程序,到了最后阶段了仍然要修改!没办法,反正是需求方提出的,"老板"让改,我没有办法.只有继续!可恶的是这段代码不是我写的,而是让我去修改,呵呵!我的确是笑了!看到代码的时候更让人想笑-------写的那些全是数字拼凑!Bad smell!Hell!我晕倒!我改吧!从新按照原…

linux tcp ip调试,Linux 网络性能调试工具Tcpdump命令篇

Linux 网络性能调试工具Tcpdump命令篇Linux 网络性能调试工具Tcpdump命令用于监视TCP/IP连接并直接读取数据链路层的数据包头。您可以指定哪些数据包被监视、哪些控制要显示格式。例如我们要监视所有Ethernet上来往的通信&#xff0c;执行下述命令&#xff1a;Tcpdump-i eth0即…

ES6模块语法标准 - 模块化、导入、暴露

文章目录1. 概述2. ES6模块化规范2.1 暴露某个模块的成员变量、方法(默认、自定义)2.2 运行某个模块的内容 - 不需暴露方法3. 导入 - import只有js文件才可以简写后缀名1. 概述 传统开发问题命名冲突文件依赖模块化进行开发&#xff0c;解决上面两大问题 模块化&#xff1a;单独…