https://cn.vuejs.org/v2/guide/computed.html

    1. // 计算属性有缓存性:如果值没有发生改变,则页面不回重新渲染
    2. computed:{
    3. total(){
    4. return: this.list.length
    5. }
    6. }
    1. // 默认情况下 watch 初始化时不执行
    2. watch: {
    3. courses(newVal, oldVal){
    4. console.log(newVal, oldVal)
    5. this.total = newVal.length
    6. }
    7. }
    1. //
    2. watch: {
    3. courses: {
    4. immediate: true, // 立即执行一次
    5. deep: true, // 深层监听,耗性能
    6. handler(newVal, oldVal) {
    7. console.log(newVal, oldVal)
    8. this.total = newVal.length
    9. }
    10. }
    11. }