vue 监听路由

1、在App.vue文件中
	watch:{
		$route(data)
		{
				xx
		}
	}
	在路由改变或参数改变时,会触发监听事件

代码示例:

<template>
  <div id="app">
    <div>
       <router-link to='/'>首页</router-link>
       <router-link to='/a'>a页</router-link>
    </div>
    <router-view></router-view>
  </div>
</template>

<script>

export default {
  name: 'App',
  data()
  {
    return{

    }
  },
  components:{

  },
  methods:{

  },
  watch:{
    $route(data)
    {
        console.log(data);
        console.log('路由改变');
        console.log(this.$route.params.id);
    }
  }
}
</script>

<style scoped >

*{
  margin:0;
  padding: 0;
}


</style>

发布了550 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_43294560/article/details/104595786