vue组件定时刷新

太过爱你忘了你带给我的痛 2022-02-20 01:55 538阅读 0赞

遇到问题:需要定时刷新获取新的数据进行展示
解决方法:setInterval()定时刷新
备注:在vue项目中,我们该将刷新放在生命周期的mounted阶段。
代码:

  1. //定时刷新
  2. mounted() {
  3. if(this.timer){
  4. clearInterval(this.timer)
  5. }else{
  6. this.timer=setInterval(()=>{
  7. //获取数据
  8. this.loadData()
  9. },6000)
  10. }
  11. },
  12. //组件销毁时清除
  13. destroyed(){
  14. clearInterval(this.timer)
  15. },

首先看一下生命周期:
看一段代码:(可粘贴复制直接运行)

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <script type="text/javascript" src="https://cdn.jsdelivr.net/vue/2.1.3/vue.js"></script>
  6. </head>
  7. <body>
  8. <div id="app">
  9. <p>{
  10. { message }}</p>
  11. </div>
  12. <script type="text/javascript">
  13. new Vue({
  14. el: '#app',
  15. data: {
  16. message: "weiqiujuan"
  17. },
  18. beforeCreate: function () {
  19. console.group('beforeCreate 创建前状态===============》');
  20. console.log("%c%s", "color:red", "el : " + this.$el); //undefined
  21. console.log("%c%s", "color:red", "data : " + this.$data); //undefined
  22. console.log("%c%s", "color:red", "message: " + this.message)
  23. },
  24. created: function () {
  25. console.group('created 创建完毕状态===============》');
  26. console.log("%c%s", "color:red", "el : " + this.$el); //undefined
  27. console.log("%c%s", "color:red", "data : " + this.$data); //已被初始化
  28. console.log("%c%s", "color:red", "message: " + this.message); //已被初始化
  29. },
  30. beforeMount: function () {
  31. console.group('beforeMount 挂载前状态===============》');
  32. console.log("%c%s", "color:red", "el : " + (this.$el)); //已被初始化
  33. console.log(this.$el);
  34. console.log("%c%s", "color:red", "data : " + this.$data); //已被初始化
  35. console.log("%c%s", "color:red", "message: " + this.message); //已被初始化
  36. },
  37. mounted: function () {
  38. console.group('mounted 挂载结束状态===============》');
  39. console.log("%c%s", "color:red", "el : " + this.$el); //已被初始化
  40. console.log(this.$el);
  41. console.log("%c%s", "color:red", "data : " + this.$data); //已被初始化
  42. console.log("%c%s", "color:red", "message: " + this.message); //已被初始化
  43. },
  44. beforeUpdate: function () {
  45. console.group('beforeUpdate 更新前状态===============》');
  46. console.log("%c%s", "color:red", "el : " + this.$el);
  47. console.log(this.$el);
  48. console.log("%c%s", "color:red", "data : " + this.$data);
  49. console.log("%c%s", "color:red", "message: " + this.message);
  50. },
  51. updated: function () {
  52. console.group('updated 更新完成状态===============》');
  53. console.log("%c%s", "color:red", "el : " + this.$el);
  54. console.log(this.$el);
  55. console.log("%c%s", "color:red", "data : " + this.$data);
  56. console.log("%c%s", "color:red", "message: " + this.message);
  57. },
  58. beforeDestroy: function () {
  59. console.group('beforeDestroy 销毁前状态===============》');
  60. console.log("%c%s", "color:red", "el : " + this.$el);
  61. console.log(this.$el);
  62. console.log("%c%s", "color:red", "data : " + this.$data);
  63. console.log("%c%s", "color:red", "message: " + this.message);
  64. },
  65. destroyed: function () {
  66. console.group('destroyed 销毁完成状态===============》');
  67. console.log("%c%s", "color:red", "el : " + this.$el);
  68. console.log(this.$el);
  69. console.log("%c%s", "color:red", "data : " + this.$data);
  70. console.log("%c%s", "color:red", "message: " + this.message)
  71. }
  72. })
  73. </script>
  74. </body>
  75. </html>

结果:
在这里插入图片描述
是不是很清楚呀!

所以,我们的setInterval是放在mounted上的。
注: 使用setInterval时this指向的问题,如果我们直接使用this.timer=setInterval(function(){//要触发的函数})报错的,因为this指向的是window,但是我们要触发的函数是挂载在vm实例上的,所以需要使用箭头函数。

发表评论

表情:
评论列表 (有 0 条评论,538人围观)

还没有评论,来说两句吧...

相关阅读