聊聊Vue3中路由,浅析路由配置方式

本篇文章带大家了解一下vue3中路由,聊聊路由的基本配置、动态路由的配置、路由模式、路由重定向等,希望对大家有所帮助。

聊聊Vue3中路由,浅析路由配置方式

【相关推荐:《vue.js教程》】

路由的基本配置

1、安装插件

  1. npm install vue-router@next --save

登录后复制

2、创建一个routers.ts文件

立即学习“前端免费学习笔记(深入)”;

3、在routers.ts中引入组件并配置路径。

  1. import { createRouter,createWebHashHistory } from 'vue-router';// 引入组件import Home from './components/Home.vue';import News from './components/News.vue';import User from './components/User.vue';const router = createRouter({  history: createWebHashHistory(),  routes: [    {path: '/', component: Home},    {path: '/news', component: News},    {path: '/user', component: User},  ]})export default router;

登录后复制

4、在main.ts中将路由文件挂载到vue身上。

  1. import { createApp } from 'vue'import App from './App.vue'import routers from './routers';// createApp(App).mount('#app')const app = createApp(App);app.use(routers);app.mount('#app');

登录后复制

5、在用到路由的组件通过router-view组件或者router-link

  1.   @@##@@  
        
  •       首页    
  •     
  •       新闻    
  •     
  •       用户    
  •   
  

登录后复制

挂载router-link后,只需要在组件对应的页面路径上输入指定路由即可完成跳转,router-link则实现a标签进行跳转的形式路由。

动态路由的配置

在routes.ts中按照下面的方式进行配置路由,通过/:aid的方式来进行动态路由的配置。

  1. //配置路由const router = createRouter({    history: createWebHashHistory(),    routes: [        { path: '/', component: Home , alias: '/home' },        { path: '/news', component: News },        { path: '/user', component: User },        { path: '/newscontent/:aid', component: NewsContent },    ], })

登录后复制

通过router-link进行跳转的时候,需要模板字符串和冒号+to。

  1.  

登录后复制登录后复制             {{item}}    

通过this.$route.params获取动态路由传过来的值。

  1. mounted(){    // this.$route.params 获取动态路由的传值    console.log(this.$route.params)}

登录后复制

如果我们想要实现类似与GET传值,我们可以通过下面的方式

1、将路由配置为普通路由。

  1. const router = createRouter({    history: createWebHashHistory(),    routes: [        { path: '/', component: Home , alias: '/home' },        { path: '/news', component: News },        { path: '/user', component: User },        { path: '/newscontent', component: NewsContent },    ], })

登录后复制

2、router-link通过问号的形式进行跳转。

  1.  {{item}}

登录后复制

3、通过this.$route.query获取到get传值。

  1. console.log(this.$route.query);

登录后复制

路由编程式导航(JS跳转路由)

只需要通过this.$router.push进行指定即可。

  1.   this.$router.push({    path: '/home'  })

登录后复制

如果想要实现get传值,可以通过下列的方式。

  1. this.$router.push({    path: '/home',    query: {aid: 14}  })}

登录后复制

动态路由需要使用下面的这种方式。

  1.   this.$router.push({    path: '/home/123',    // query: {aid: 14}  })

登录后复制

路由模式

Hash模式

Hash模式的典型特点就是页面路由中含有一个井号。

  1. const router = createRouter({    history: createWebHashHistory(),    routes: [        ...,    ], })

登录后复制

HTML5 history模式

引入createWebHistory。

router的配置项中的history属性设置为createWebHistory()。

  1. import { createRouter, createWebHistory } from 'vue-router'//配置路由const router = createRouter({    history: createWebHistory(),    routes: [        ...    ], })

登录后复制

注意:开启HTML5 History模式之后,发布到服务器需要配置伪静态。

配置伪静态的方法:

https://router.vuejs.org/zh/guide/essentials/history-mode.html#%E5%90%8E%E7%AB%AF%E9%85%8D%E7%BD%AE%E4%BE%8B%E5%AD%90

命名路由

一般情况

定义路由的时候配置name属性

  1. { path: '/news', component: News,name:"news" }

登录后复制

传入对象进行跳转

  1. 新闻

登录后复制

通过GET传值的方式

定义路由的时候配置name属性

  1. { path: '/newscontent', component: NewsContent, name: "content" },

登录后复制

传入包括query的对象

  1.  

登录后复制登录后复制     {{item}}

通过动态路由的方式

定义动态路由并指定name属性

  1. { path: '/userinfo/:id', name: "userinfo", component: UserInfo }

登录后复制

传入包括params的对象

  1. 跳转到用户详情

登录后复制

编程式路由

和上面的方式很类似。

登录后复制

路由重定向

  1. { path: '', redirect: "/home" },   // 路由重定向{ path: '/home', component: Home },

登录后复制

路由别名

下面的这个实例中,访问people这个路由和访问alias这个路由是一致的。

  1. { path: '/user', component: User, alias: '/people' }

登录后复制

alias也可以是一个数组。

  1. { path: '/user', component: User, alias: ['/people','/u']}

登录后复制

动态路由的形式。

  1. { path: '/userinfo/:id', name: "userinfo", component: UserInfo, alias: '/u/:id' }

登录后复制

嵌套路由

嵌套路由的应用场景一般在导航栏上。

定义嵌套路由

  1. {  path: '/user', component: User,  children: [    { path: '', redirect: "/user/userlist" },    { path: 'userlist', component: UserList },    { path: 'useradd', component: UserAdd }  ]}

登录后复制

router-link和router-view配合显示内容

  1.   
          
    •       用户列表    
    •     
    •       增加用户    
    •   
      

登录后复制

更多编程相关知识,请访问:vue.js教程!!

聊聊Vue3中路由,浅析路由配置方式

以上就是聊聊Vue3中路由,浅析路由配置方式的详细内容,更多请关注【创想鸟】其它相关文章!

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

点点赞赏,手留余香

给TA打赏
共0人
还没有人赞赏,快来当第一个赞赏的人吧!
    编程技术

    Ant Design Vue中如何让Textarea组件有“字数统计”功能

    2025-4-1 17:22:34

    编程技术

    一文快速教你用VuePress + Github Pages搭建一个博客(实战)

    2025-4-1 17:22:43

    0 条回复 A文章作者 M管理员
    欢迎您,新朋友,感谢参与互动!
      暂无讨论,说说你的看法吧
    个人中心
    购物车
    优惠劵
    今日签到
    私信列表
    搜索