首页 > 其他 > 详细

vue 监听路由变化

时间:2018-07-26 16:26:01      阅读:220      评论:0      收藏:0      [点我收藏+]
<script>
  // 引入 Tabbar组件
  import mTabbar from ‘./components/Tabbar‘
  import mTabbarItem from ‘./components/TabbarItem‘
  // 引入 vuex 的两个方法
  import {mapGetters, mapActions} from ‘vuex‘
 
  export default {
    name: ‘app‘,
    // 计算属性
    computed:mapGetters([
      // 从 getters 中获取值
      ‘tabbarShow‘
    ]),
    // 监听,当路由发生变化的时候执行
    watch:{
      $route(to,from){
        if(to.path == ‘/‘ || to.path == ‘/Prisoner‘ || to.path == ‘/Goods‘ || to.path == ‘/Time‘ || to.path == ‘/Mine‘){
          /**
           * $store来自Store对象
           * dispatch 向 actions 发起请求
           */
          this.$store.dispatch(‘showTabBar‘);
        }else{
          this.$store.dispatch(‘hideTabBar‘);
        }
      }
    },
    beforeRouteEnter (to, from, next) {
      // 在渲染该组件的对应路由被 confirm 前调用
      // 不!能!获取组件实例 `this`
      // 因为当钩子执行前,组件实例还没被创建
    },
    beforeRouteUpdate (to, from, next) {
      // 在当前路由改变,但是该组件被复用时调用
      // 举例来说,对于一个带有动态参数的路径 /foo/:id,在 /foo/1 和 /foo/2 之间跳转的时候,
      // 由于会渲染同样的 Foo 组件,因此组件实例会被复用。而这个钩子就会在这个情况下被调用。
      // 可以访问组件实例 `this`
    },
    beforeRouteLeave (to, from, next) {
      // 导航离开该组件的对应路由时调用
      // 可以访问组件实例 `this`
    },
    components:{
      mTabbar,
      mTabbarItem
    },
    data() {
      return {
        select:"Building"
      }
    }
  }
</script>

vue 监听路由变化

原文:https://www.cnblogs.com/web-chuanfa/p/9372486.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!