Vue精彩案例、教程

Vue注意事项(遇到的坑)

2017-12-29  本文已影响2284人  唯轩_443e

要注意的细节

// 如监听某个数据变化,在数据变化后并且DOM也更新完,才执行回调里面的内容
watch() {
  // 监听播放状态
  playing(val) {
        const audio = this.$refs.audio
        this.$nextTick(() => {
          val ? audio.play() : audio.pause()
        })
      },
}
props: {
      switches: {
        type: Array,
        default: () => []
      }
    },
// 在组件销毁时(即切换组件或关闭页面),
// 调用destroyed方法清除计时器
destroyed(){
  clearTimeout(this.timer)
}
// 好处 DOM会缓存在内容中
// 切换导航时不会重复获取数据
<keep-alive>
  <router-view></router-view>
</keep-alive>
<my-component @click.native="handleClick">Click Me</my-component>
{{ message | filterFun }}

new Vue({  
  // ...  
  filters: {  
    //  进行格式转换并返回
    filterFun: function (value) {  
      return value;
    }  

  }  
})  
//filter中可有传多个参数,默认第一个为message的值,自定义参数从第二个开始
filters: {  
    filterFun: function (value, sta1, sta2) {  
      return value;
    }  
  }  
import navTitle from './title.vue'

components:{
  navTitle
}
调用
<nav-title></nav-title>
win_W: window.innerWidth || document.body.clientWidth

根组件绑定窗口改变事件
window.onresize = function(){
    _this_.win_W = window.innerWidth || document.body.clientWidth;
}
窗口改变时,其他子组件也能得到
3级组件 ;  @click="trgSidebar"      
2级组件:   @trgSidebar="trgSidebar"
methods: {
    trgSidebar: () {
        this.$emit('trgSidebar');
    }
}
<li :class="{ active: index ===0 }"></li>
上一篇 下一篇

猜你喜欢

热点阅读