watch监听、生命周期函数

94 阅读1分钟

一、watch监听 (摄像头)

负责监听数据(data、props)的变化,进而做出逻辑操作 不会返回新数据

  1. 浅监听
stu(){ //浅监听  stu
    //可以按需添加逻辑
    console.log('监听到了stu的变化');
    localStorage.setItem('stu',JSON.stringify(this.stu))
},
  1. 深监听
stu:{  //深监听
    handler:function(){  //监听到变化后的处理函数
        console.log('深度监听到了stu的变化');
        localStorage.setItem('stu',JSON.stringify(this.stu))
    },
    deep:true  //开启深监听
}

二、生命周期钩子函数

让开发者有机会介入到,new Vue的过程当中 例如:页面初始化的数据加载,就需要在生命周期中完成 生命周期函数图

  1. 生命周期的四个阶段
  • 创建阶段
    • beforeCreate
    • created
  • 渲染阶段
    • beforeMount
    • mounted
  • 更新阶段
    • beforeUpdate
    • updated
  • 销毁阶段
    • beforeDestroy
    • destroyed
  1. created的使用场景

一般在这个生命周期中,向后端接口发起异步请求,获取数据包,设置给data

  1. 使用axios发起异步请求

Axios 是一个基于 promise 的网络请求库,可以用于浏览器和 node.js 使用promise将XMLHttpRequest进行了封装

文档