vue 路由动态路径传参、路由嵌套

﹏ヽ暗。殇╰゛Y 2021-07-24 20:42 1068阅读 0赞
  1. 1、路由动态路径传参
  2. (1)使用
  3. 1、在路由路径中
  4. routes: [
  5. { path: '/user/:id', ...}
  6. ]
  7. 混合路径传多个参数
  8. /路径/:键名/路径/:键名
  9. url中/路径/123/路径/223
  10. 同样能够通过第3步获取
  11. 2、传递路径参数
  12. 方式一:
  13. router-link跳转路径中
  14. <router-link to='/user/值'><router-link>
  15. 方式二:
  16. this.$router编程式跳转时传入
  17. 如:
  18. this.$router.push('/detail/123')
  19. this.$router.push({ name:'user',params: {id: 123 } })
  20. 3、获取路径参数
  21. this.$route.params.id
  22. (2)监听路径参数的变化:
  23. 当使用路由参数时,例如从/user/foo导航到/user/bar,原来的组件实例会被复用。意味着组件的生命周期钩子不会再被调用。
  24. 想对路由参数的变化作出响应的话,你可以简单地watch(监测变化)$route对象:
  25. watch: {
  26. $route(to, from) {
  27. ...
  28. }
  29. }
  30. 或路由守卫:
  31. beforeRouteUpdate(to, from, next) {
  32. ...
  33. next();
  34. }
  35. 2、嵌套路由
  36. 1、在父路由中,与component同级,添加
  37. children:[{
  38. path:'不加/的和不加父级路径的名称',
  39. component:子组件
  40. }]
  41. 其中:
  42. (1)多重子路由,在children中再添加children,设置
  43. 方式重复步骤,不要忘了给子路由设置子字路由的显示位置
  44. children:[{xxx,children:[{xxx}]}]
  45. (2)多个同级子路由,children[{xxx},{xxx},xxx]
  46. 2、在父路由的组件中设置给子路由的组件显示位置
  47. <router-view />
  48. 3、在父路由的组件中,设置子路由跳转标签
  49. 此时的路径包括父路径
  50. <router-link to='/父路径/子路径'></router-link>

代码示例:
路由js文件:

  1. import Vue from 'vue'
  2. import VueRouter from 'vue-router'
  3. import HelloWorld from '../components/HelloWorld'
  4. import C from '../components/C'
  5. import D from '../components/D'
  6. import A from '../components/A'
  7. import B from '../components/B'
  8. Vue.use(VueRouter)
  9. export default new VueRouter({
  10. routes:[
  11. {
  12. path:'/hello',
  13. name:"HelloWorld",
  14. component:HelloWorld
  15. },
  16. {
  17. path:'/c',
  18. name:'C',
  19. component:C,
  20. //子路由
  21. children:[
  22. {
  23. path:'cc',
  24. component:A,
  25. //子子路由
  26. children:[
  27. {
  28. path:'ccc',
  29. component:A
  30. }
  31. ]
  32. }
  33. ]
  34. },
  35. {
  36. path:'/d/:name',
  37. name:'D',
  38. component:D,
  39. //子路由
  40. children:[{
  41. path:"dd",
  42. component:B
  43. }]
  44. }
  45. ]
  46. })

主入口文件:

  1. <template>
  2. <div id="app">
  3. <img src="./assets/logo.png">
  4. <ul>
  5. <router-link to='/d/jeff'>d</router-link>
  6. <router-link to='/c'>c</router-link>
  7. </ul>
  8. //主路由的显示
  9. <router-view />
  10. </div>
  11. </template>
  12. <script>
  13. import Vuedemo from './components/Vuedemo'
  14. import Vue1 from './components/Vuee'
  15. import A from './components/A'
  16. import B from './components/B'
  17. import D from './components/D'
  18. export default {
  19. name: 'App',
  20. data()
  21. {
  22. return{
  23. }
  24. },
  25. methods:{
  26. },
  27. components: {
  28. Vuedemo,
  29. Vue1,
  30. A,
  31. B,
  32. D
  33. }
  34. }
  35. </script>
  36. <style>
  37. #app {
  38. font-family: 'Avenir', Helvetica, Arial, sans-serif;
  39. -webkit-font-smoothing: antialiased;
  40. -moz-osx-font-smoothing: grayscale;
  41. text-align: center;
  42. color: #2c3e50;
  43. margin-top: 60px;
  44. }
  45. </style>

父路由组件:

  1. <template>
  2. <div>
  3. ccc
  4. <ul>
  5. <router-link to="/c/cc">c的子路由</router-link>
  6. </ul>
  7. <router-view />
  8. </div>
  9. </template>
  10. <script>
  11. export default{
  12. name:'c',
  13. data()
  14. {
  15. return{
  16. }
  17. }
  18. }
  19. </script>
  20. <style lang='css'>
  21. </style>

父路由组件2:

  1. <template>
  2. <div>
  3. ddd
  4. <ul>
  5. <router-link to="/d/dd">d的子路由</router-link>
  6. </ul>
  7. { { this.$route.params.name}}
  8. <router-view />
  9. </div>
  10. </template>
  11. <script>
  12. export default{
  13. name:'d',
  14. data()
  15. {
  16. return{
  17. }
  18. }
  19. }
  20. </script>
  21. <style lang="css">
  22. </style>

子组件:

  1. <template>
  2. <div>
  3. 子路由A
  4. <ul>
  5. <router-link to="/c/cc/ccc">子子路由</router-link>
  6. </ul>
  7. <router-view />
  8. </div>
  9. </template>
  10. <script>
  11. import B from './B'
  12. export default{
  13. name:'A',
  14. data()
  15. {
  16. return{
  17. }
  18. }
  19. }
  20. </script>
  21. <style lang='css'>
  22. <style>

发表评论

表情:
评论列表 (有 0 条评论,1068人围观)

还没有评论,来说两句吧...

相关阅读

    相关 vue

    前言 之前,查了其他博客,总结一篇,发现貌似不对??,因此又查了一遍好不容易写了出来。 1.query传参 一种是,这样滴。 // 跳转 thi

    相关 vue-03--

    和平时的页面跳转一样,我们可能需要在地址栏上传递某些参数,例如,从商品列表页面,跳转的某个商品的详情页面,这个时候,如果是页面跳转的时候,我们会直接在url上跟上一个商品id,

    相关 Vue跳转+

    之前在原生JS的开发中,我们经常会用到根据某一状态进行页面的跳转。 比如:登录成功跳到首页,点击商品列表的某个商品跳转商品详情等。 而常见的写法就是: locat