声明式导航&编程式导航—Vue

学习分享1年前 (2023)更新 bestcyt
209 0 0
  • 共同点

都能进行导航,都可以触发路由,实现组件切换

  • 区别

写法不一样
声明式导航是写在组件的template中,通过router-link来触发。
编程式导航写在js函数中,通过this.$router.push(xxx)来触发路径。

编程式导航

  • params形式:参数不显示在地址栏
this.$router.push({ name: 'details', params: { userId: this.num }}) //'details'是路由名;'userId'是传入的参数

 
获取
console.log(this.$route.params.userId);

  • query形式:参数显示在地址栏
this.$router.push({ path: 'details', query: { userId: this.num }})

获取
console.log(this.$route.query.userId);
声明式导航&编程式导航—Vue
 

© 版权声明

相关文章