Home  >  Article  >  Web Front-end  >  How vue.js monitors routing changes

How vue.js monitors routing changes

coldplay.xixi
coldplay.xixiOriginal
2020-11-11 16:16:584806browse

Vue.js method of monitoring route changes: 1. Implemented through watch, the code is [watch:{$route(to,from){console.log(to.path);}]; 2. key It is used to prevent "reuse", the code is [

How vue.js monitors routing changes

The operating environment of this tutorial: windows10 system, vue2.9, this article is applicable to all brands of computers.

【Recommended related articles: vue.js

# #vue.js method of monitoring routing changes:

Method 1: Through watch

// 监听,当路由发生变化的时候执行
watch:{
  $route(to,from){
    console.log(to.path);
  }
},

or

// 监听,当路由发生变化的时候执行
watch: {
  $route: {
    handler: function(val, oldVal){
      console.log(val);
    },
    // 深度观察监听
    deep: true
  }
},

or

// 监听,当路由发生变化的时候执行
watch: {
  '$route':'getPath'
},
methods: {
  getPath(){
    console.log(this.$route.path);
  }
}

Method 2: Key is used to prevent "reuse"

Vue provides you with a way to declare "these two elements are completely independent - don't Reuse them”. Just add a key attribute with a unique value (original words from the Vue document)

<router-view :key="key"></router-view>
 
computed: {
  key() {
    return this.$route.name !== undefined? this.$route.name +new Date(): this.$route +new Date()
  }
}

Using the computed attribute and

Date() can ensure that the key is different every time, so You can refresh the data as desired.

Method 3: Through the hook function of vue-router beforeRouteEnter beforeRouteUpdate beforeRouteLeave

<script>
  // 引入 Tabbar组件
  import mTabbar from &#39;./components/Tabbar&#39;
  import mTabbarItem from &#39;./components/TabbarItem&#39;
  // 引入 vuex 的两个方法
  import {mapGetters, mapActions} from &#39;vuex&#39;
 
  export default {
    name: &#39;app&#39;,
    // 计算属性
    computed:mapGetters([
      // 从 getters 中获取值
      &#39;tabbarShow&#39;
    ]),
    // 监听,当路由发生变化的时候执行
    watch:{
      $route(to,from){
        if(to.path == &#39;/&#39; || to.path == &#39;/Prisoner&#39; || to.path == &#39;/Goods&#39; ||
         to.path == &#39;/Time&#39; || to.path == &#39;/Mine&#39;){
          /**
           * $store来自Store对象
           * dispatch 向 actions 发起请求
           */
          this.$store.dispatch(&#39;showTabBar&#39;);
        }else{
          this.$store.dispatch(&#39;hideTabBar&#39;);
        }
      }
    },
    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>

Related free learning recommendations:

JavaScript (video)

The above is the detailed content of How vue.js monitors routing changes. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn