• vue3 + vite + ts + setup , 第十七练 vue3 中使用vue-router(一),router跳转传参/嵌套路由/路由重定向/别名


    1、构建vue3项目

    1. npm init vue@latest
    2. //或者
    3. npm init vite@latest
    4. //或者
    5. npm create vite@latest
    6. //或者
    7. yarn create vite
    8. ...

    2、安装vue-router

    使用Vue3 安装对应的router4版本

    使用Vue2安装对应的router3版本

    npm install vue-router@4

    3、创建router文件,src/router/index.ts

    1. //引入路由对象
    2. import {
    3. createRouter,
    4. createWebHistory,
    5. createWebHashHistory,
    6. createMemoryHistory,
    7. RouteRecordRaw,
    8. } from "vue-router";
    9. //vue2 mode history vue3 createWebHistory
    10. //vue2 mode hash vue3 createWebHashHistory
    11. //vue2 mode abstact vue3 createMemoryHistory ssr服务端渲染
    12. //路由数组的类型 RouteRecordRaw
    13. // 定义一些路由
    14. // 每个路由都需要映射到一个组件。
    15. const routes: Array<RouteRecordRaw> = [
    16. {
    17. path: "/",
    18. name: "login",
    19. component: () => import("../components/login.vue"),
    20. },
    21. {
    22. path: "/reg",
    23. name: "reg",
    24. component: () => import("../components/reg.vue"),
    25. },
    26. ];
    27. const router = createRouter({
    28. history: createWebHashHistory(),
    29. routes: routes,
    30. });
    31. //导出router
    32. export default router;

    4、在mian.ts中使用

    1. import { createApp } from 'vue'
    2. import './style.css'
    3. import App from './App.vue'
    4. import router from "./router"
    5. const app = createApp(App)
    6. app.use(router)
    7. app.mount('#app')

    5、router-view#

    router-view 将显示与 url 对应的组件。你可以把它放在任何地方,以适应你的布局

    1. <template>
    2. <div>
    3. 666
    4. <div>
    5. <router-link replace to="/">loginrouter-link>
    6. <router-link replace style="margin-left:20px" to="/reg">regrouter-link>
    7. <div>
    8. <button @click="toPage('login')">loginbutton>
    9. <button @click="toReg('reg')">regbutton>
    10. <button @click="pre()">prebutton>
    11. <button @click="next()">nextbutton>
    12. div>
    13. div>
    14. <hr>
    15. <router-view>router-view>
    16. div>
    17. template>
    18. <script setup lang="ts">
    19. import { useRouter } from "vue-router";
    20. const router = useRouter();
    21. // let toPage = (url: string) => {
    22. // // 字符串path形式
    23. // // router.push(url);
    24. // // 对象形式
    25. // // router.push({
    26. // // path: url,
    27. // // });
    28. // // 命名
    29. // router.push({
    30. // name: url,
    31. // });
    32. // };
    33. // 不留router历史记录
    34. let toPage = (url: string) => {
    35. router.push({
    36. name: url,
    37. query: {
    38. name: 'admin',
    39. pwd: 123456
    40. }
    41. });
    42. };
    43. let toReg = (name: string) => {
    44. router.push({
    45. name: name,
    46. query: {
    47. kkk: '000000'
    48. }
    49. })
    50. }
    51. let pre = () => {
    52. router.back();
    53. // router.go(-1)
    54. };
    55. let next = () => {
    56. router.go(1);
    57. };
    58. script>
    59. <style scoped>
    60. style>

    6、router的几种跳转方式

    router-link

    1. 第一种
    2. <router-link to="/">loginrouter-link>
    3. 第二种 replace 不会留下历史记录
    4. <router-link replace to="/">loginrouter-link>

    编程式导航:router.push()     router.replace()  router.back()  router.go()

     name跳转:

    1. router.push({
    2. name:'login',
    3. query: {
    4. name: 'admin',
    5. pwd: 123456
    6. }
    7. });
    8. 接收参数
    9. import { useRoute } from 'vue-router';
    10. const route = useRoute()
    11. console.log("路由传参", route.query)
    12. 或=====================
    13. router.push({
    14. name: url,
    15. params: {
    16. name: 'admin',
    17. pwd: 123456
    18. }
    19. });
    20. 接收参数
    21. import { useRoute } from 'vue-router';
    22. const route = useRoute()
    23. console.log("路由传参", route.params)

    path跳转:

    1. router.push({
    2. path:'/login',
    3. query: {
    4. name: 'admin',
    5. pwd: 123456
    6. }
    7. });
    8. 接收参数
    9. import { useRoute } from 'vue-router';
    10. const route = useRoute()
    11. console.log("路由传参", route.query)

    动态路由传参

    在 Vue Router 中,我们可以在路径中使用一个动态字段来实现,我们称之为 路径参数

    路径参数 用冒号 : 表示。当一个路由被匹配时,它的 params 的值将在每个组件

     

    1. const routes: Array<RouteRecordRaw> = [
    2. {
    3. path: "/",
    4. name: "login",
    5. component: () => import("../components/login.vue"),
    6. },
    7. {
    8. path: "/reg/:name",
    9. name: "reg",
    10. component: () => import("../components/reg.vue"),
    11. },
    12. ];
    1. router.push({
    2. name: 'Reg',
    3. params: {
    4. name: "我是动态路由"
    5. }
    6. })
    7. 接收参数
    8. import { useRoute } from 'vue-router';
    9. const route = useRoute()
    10. console.log("reg", route.params)
    11. 或传多个参数
    12. {
    13. path: "/reg/:name/:id",
    14. name: "reg",
    15. component: () => import("../components/reg.vue"),
    16. }
    17. router.push({
    18. name: 'Reg',
    19. params: {
    20. name: "我是动态路由",
    21. id:123
    22. }
    23. })

    使用name和path的区别:

    1、name传参可以使用params、query

    2、path传参只能使用query

    3、query 在路由配置不需要设置参数,而 params 必须设置

    4、query 传递的参数会显示在地址栏中

    5、params传参刷新会无效,但是 query 会保存传递过来的值,刷新不变

    嵌套路由

    1. const routes: Array<RouteRecordRaw> = [
    2. {
    3. path: "/",
    4. name: "Login",
    5. component: () => import('../components/login.vue')
    6. },
    7. {
    8. //动态路由参数
    9. path: "/reg",
    10. name: "Reg",
    11. component: () => import('../components/reg.vue'),
    12. children: [
    13. {
    14. path: "",
    15. name: "tab1",
    16. component: () => import('../components/tab1.vue')
    17. },
    18. {
    19. path: "tab2",
    20. name: "tab2",
    21. component: () => import('../components/tab2.vue')
    22. }
    23. ]
    24. }
    25. ]
    1. <template>
    2. <div class="reg">
    3. 我是注册
    4. <div class="tab">
    5. <button @click="$router.push('/reg')">tab1button>
    6. <button @click="$router.push('/reg/tab2')">tab2button>
    7. div>
    8. <router-view />
    9. div>
    10. template>

    路由命名视图

    有时候想同时 (同级) 展示多个视图,而不是嵌套展示,例如创建一个布局,有 sidebar (侧导航) 和 main (主内容) 两个视图,这个时候命名视图就派上用场了。你可以在界面中拥有多个单独命名的视图,而不是只有一个单独的出口。如果 router-view 没有设置名字,那么默认为 default

    一个视图使用一个组件渲染,因此对于同个路由,多个视图就需要多个组件。确保正确使用 components 配置 (带上 s):

    1. const routes: Array<RouteRecordRaw> = [
    2. {
    3. path: "/",
    4. name: "Login",
    5. component: () => import('../components/login.vue')
    6. },
    7. {
    8. //动态路由参数
    9. path: "/reg",
    10. name: "Reg",
    11. component: () => import('../components/reg.vue'),
    12. children: [
    13. {
    14. path: "",
    15. name: "tab1",
    16. component: () => import('../components/tab1.vue')
    17. },
    18. {
    19. path: "tab2",
    20. name: "tab2",
    21. components: {
    22. default: () => import('../components/tab2.vue'),
    23. two: () => import('../components/login.vue')
    24. }
    25. }
    26. ]
    27. }
    28. ]

    对应Router-view 通过name 对应组件 

    1. <div>
    2. <router-view />
    3. <router-view name="two">router-view>
    4. div>

     路由重定向

    1、字符串形式

    1. const routes: Array<RouteRecordRaw> = [
    2. {
    3. //动态路由参数
    4. path: "/",
    5. name: "Reg",
    6. component: () => import('../components/reg.vue'),
    7. redirect: "/tab2",
    8. children: [
    9. {
    10. path: "/tab1",
    11. name: "tab1",
    12. component: () => import('../components/tab1.vue')
    13. },
    14. {
    15. path: "tab2",
    16. name: "/tab2",
    17. components: {
    18. default: () => import('../components/tab2.vue'),
    19. two: () => import('../components/login.vue')
    20. }
    21. }
    22. ]
    23. }
    24. ]

    跳转 '/' 会重定向到  /tab2

    1. router.push({
    2. path: '/',
    3. })

    2、对象形式

    1. const routes: Array<RouteRecordRaw> = [
    2. {
    3. //动态路由参数
    4. path: "/",
    5. name: "Reg",
    6. component: () => import('../components/reg.vue'),
    7. redirect: { path: 'tab1' },
    8. children: [
    9. {
    10. path: "/tab1",
    11. name: "tab1",
    12. component: () => import('../components/tab1.vue')
    13. },
    14. {
    15. path: "tab2",
    16. name: "/tab2",
    17. components: {
    18. default: () => import('../components/tab2.vue'),
    19. two: () => import('../components/login.vue')
    20. }
    21. }
    22. ]
    23. }
    24. ]

    3、函数形式

    1. const routes: Array<RouteRecordRaw> = [
    2. {
    3. //动态路由参数
    4. path: "/",
    5. name: "Reg",
    6. component: () => import('../components/reg.vue'),
    7. redirect: (to) => {
    8. return {
    9. path: '/tab1',
    10. query: to.query
    11. }
    12. },
    13. children: [
    14. {
    15. path: "/tab1",
    16. name: "tab1",
    17. component: () => import('../components/tab1.vue')
    18. },
    19. {
    20. path: "tab2",
    21. name: "/tab2",
    22. components: {
    23. default: () => import('../components/tab2.vue'),
    24. two: () => import('../components/login.vue')
    25. }
    26. }
    27. ]
    28. }
    29. ]

    路由别名 alias

    将 / 别名为 /root,意味着当用户访问 /root时,URL 仍然是 /

    1. const routes: Array<RouteRecordRaw> = [
    2. {
    3. path: "/",
    4. name: "Reg",
    5. component: () => import('../components/reg.vue'),
    6. alias: ['/root', '/roots', '/home'],
    7. children: [
    8. {
    9. path: "/tab1",
    10. name: "tab1",
    11. component: () => import('../components/tab1.vue')
    12. },
    13. {
    14. path: "tab2",
    15. name: "/tab2",
    16. components: {
    17. default: () => import('../components/tab2.vue'),
    18. two: () => import('../components/login.vue')
    19. }
    20. }
    21. ]
    22. }
    23. ]

  • 相关阅读:
    二十七、CANdelaStudio深入-编辑技巧(一致性检查)
    好的摄影师都会iPhone 8和iOS 11的这三项功能
    LC-396. 旋转函数(前缀和+滑动窗口、动态规划)
    Jupyter notebook如何显示目录(动图演示)
    【Semi-Supervised Raw-to-Raw Mapping 半监督 Raw-to-Raw 映射】
    支付宝支付
    MacOS原版镜像下载,详细下载步骤
    缓解Oracles数据库内存不足的问题
    RK3399平台开发系列讲解(内核调试篇)2.50、嵌入式产品启动速度优化
    肖sir__项目讲解流程___(自我介绍、项目流程)
  • 原文地址:https://blog.csdn.net/csl125/article/details/125862159