当前位置:   article > 正文

vue3中使用路由_vue3 路由

vue3 路由

路由官方官网

 
  1. https://router.vuejs.org/zh/

项目中使用路由

 
  1. 1.安装路由
  2. cnpm i --save-dev vue-router
  3. "vue-router": "^4.1.6",
 
  1. 2.项目中配置路由
  2. import vueRouter from "vue-router";
  3. //vue2 写法是上面这种 vue3 路由改为单暴漏
  4. import { createRouter } from "vue-router";
  5. console.log(createRouter);
 
  1. 3.在main.js中创建路由 使用常用的api
  2. //配置项目路由
  3. import { createRouter, createWebHistory } from "vue-router";
  4. //创建路由实例
  5. const router = createRouter({
  6. history: createWebHistory(), //mode:"history" vue3 路由模式 history: createWebHistory()
  7. routes: [], //路由配置项
  8. //配置滚动行为
  9. scrollBehavior(to, from, savePosiation) {
  10. //回到之前路由组件的滚动位置
  11. if (savePosiation) {
  12. return savePosiation;
  13. }
  14. return { left: 0, top: 0 }; //vue3 返回值存在变化
  15. },
  16. });
  17. //将路由关联到vue3 项目
  18. createApp(App).use(router).mount("#app");
  19. //vue项目路由可以配置公共路径
  20. vue2 baseUrl:"/admin"
  21. vue3 history: createWebHistory("/admin"),

浏览器效果

配置一级路由,使用routerView 做路由出口

 
  1. import { createApp } from "vue";
  2. import "./style.css";
  3. import App from "./App.vue";
  4. import "element-plus/theme-chalk/index.css";
  5. //配置项目路由
  6. import { createRouter, createWebHistory } from "vue-router";
  7. //引入组件
  8. import Index from "./views/Index.vue";
  9. import Login from "./views/Login.vue";
  10. import NotFound from "./views/Not-found.vue";
  11. //创建路由实例
  12. const router = createRouter({
  13. history: createWebHistory("/admin"), //mode:"history" vue3 路由模式 history: createWebHistory()
  14. routes: [
  15. {
  16. path: "/",
  17. component: () => import("./views/Index.vue"),
  18. },
  19. {
  20. path: "/login",
  21. component: Login,
  22. },
  23. {
  24. path: "/not-found",
  25. component: NotFound,
  26. },
  27. ], //路由配置项
  28. //配置滚动行为
  29. scrollBehavior(to, from, savePosiation) {
  30. //回到之前路由组件的滚动位置
  31. if (savePosiation) {
  32. return savePosiation;
  33. }
  34. return { left: 0, top: 0 }; //vue3 返回值存在变化
  35. },
  36. });
  37. //将路由关联到vue3 项目
  38. createApp(App).use(router).mount("#app");

路由配置非路由重定向

 
  1. {
  2. path: "*",
  3. redirect: "/not-found",
  4. },

 
  1. {
  2. path: "/:pathMatch(.*)*", //vue2 * vue3 改为正则
  3. redirect: "/not-found",
  4. },
  5. //在官网 路由动态匹配下 not found 404

子路由嵌套

 
  1. routes: [
  2. {
  3. path: "/",
  4. component: Index,
  5. children: [
  6. {
  7. path: "/index",
  8. component: User,
  9. },
  10. {
  11. path: "/list",
  12. component: List,
  13. },
  14. ],
  15. },
  16. {
  17. path: "/login",
  18. component: Login,
  19. },
  20. {
  21. path: "/not-found",
  22. component: NotFound,
  23. },
  24. {
  25. path: "/:pathMatch(.*)*", //vue2 * vue3 改为正则
  26. redirect: "/not-found",
  27. },
  28. ], //路由配置项

需要在一级路由界面下 配置二级路由出口

 
  1. <script setup lang="ts"></script>
  2. <template>
  3. admin
  4. <router-link to="/index">User界面</router-link>
  5. <router-link to="/list">List界面</router-link>
  6. <!-- 二级出口 -->
  7. <router-view></router-view>
  8. </template>

路由的编程导航

vue3 组件为vue2组件写法

 
  1. 代码编程导航一致
  2. // 字符串路径
  3. this.router.push('/users/eduardo')
  4. // 带有路径的对象
  5. this.router.push({ path: '/users/eduardo' })

vue3 使用的是setup简写组件

使用vuerouter的组合式api

 
  1. //引入路由的组合式api
  2. import { useRoute, useRouter } from "vue-router";
  3. //定义接收对象
  4. let route = useRoute();
  5. let router = useRouter();
  6. //跳转登录
  7. let goLogin = () => {
  8. console.log(route, router);
  9. };

代码中编程导航

 
  1. //跳转登录
  2. let goLogin = () => {
  3. console.log(route, router);
  4. //跳转登录
  5. router.replace(`/login?redirect=${route.path}`);
  6. };

路由传值

  1. get路由传值
  2. 路由动态匹配传值

get路由传值

 
  1. <router-link to="/list?id=10086&name=张三">List界面</router-link>

 
  1. <script setup lang="ts">
  2. import { useRoute } from "vue-router";
  3. //获取当前路由对象
  4. let route = useRoute();
  5. //使用query接收
  6. console.log(route.query);
  7. </script>
  8. <template>List</template>

路由动态匹配传值

 
  1. 在路由的配置文件path上配置传递参数
  2. path:"/admin/:id"
  3. 这种传值方式严格匹配路由路径
  4. <router-link to="/list/10086">List界面</router-link>
  5. <script setup lang="ts">
  6. import { useRoute } from "vue-router";
  7. let route = useRoute();
  8. //路由动态匹配传值 使用params获取
  9. console.log(route.params);
  10. </script>
  11. <template>User</template>

路由二级默认

项目中路由模块化

 
  1. index.ts
  2. //配置项目路由
  3. import { createRouter, createWebHistory } from "vue-router";
  4. //导入配置
  5. import routes from "./routes";
  6. //创建路由实例
  7. const router = createRouter({
  8. history: createWebHistory("/admin"), //mode:"history" vue3 路由模式 history: createWebHistory()
  9. routes, //路由配置项
  10. //配置滚动行为
  11. scrollBehavior(to, from, savePosiation) {
  12. //回到之前路由组件的滚动位置
  13. if (savePosiation) {
  14. return savePosiation;
  15. }
  16. return { left: 0, top: 0 }; //vue3 返回值存在变化
  17. },
  18. });
  19. export default router;
 
  1. routes.ts 配置文件
  2. //路由配置
  3. //引入组件
  4. import Index from "@/views/Index.vue";
  5. import Login from "@/views/Login.vue";
  6. import NotFound from "@/views/Not-found.vue";
  7. import User from "@/views/children/User.vue";
  8. import List from "@/views/children/List.vue";
  9. import Dashborad from "@/views/children/dashborad.vue";
  10. export default [
  11. {
  12. path: "/",
  13. component: Index,
  14. redirect: "/dashborad",
  15. children: [
  16. {
  17. path: "/dashborad",
  18. component: Dashborad,
  19. },
  20. {
  21. path: "/index/:id/:name",
  22. component: User,
  23. },
  24. {
  25. path: "/list",
  26. component: List,
  27. },
  28. ],
  29. },
  30. {
  31. path: "/login",
  32. component: Login,
  33. },
  34. {
  35. path: "/not-found",
  36. component: NotFound,
  37. },
  38. {
  39. path: "/:pathMatch(.*)*", //vue2 * vue3 改为正则
  40. redirect: "/not-found",
  41. },
  42. ];

项目中使用路由守卫

 
  1. //全局前置
  2. router.beforeEach((to, from) => {
  3. // ...
  4. // 返回 false 以取消导航
  5. return false
  6. })
  7. return false 不跳转
  8. //跳转其他路由
  9. router.beforeEach((to, from) => {
  10. // ...
  11. // 直接跳转登录界面
  12. return {path:"/login"}
  13. })

项目守卫检测用户是否登录

 
  1. //路由守卫
  2. //白名单
  3. let whiteList = ["/login", "/not-found"];
  4. router.beforeEach((to, from) => {
  5. console.log("守卫");
  6. // //检测token
  7. let token = localStorage.getItem("_token");
  8. //未登录
  9. if (!token) {
  10. //检测白名单
  11. if (whiteList.indexOf(to.path) == -1) {
  12. //token不存在直接跳转登录
  13. return { path: "/login" };
  14. }
  15. } else {
  16. //已经登录
  17. if (to.path == "/login") {
  18. return { path: "/" };
  19. }
  20. }
  21. });

实现vue3 动态路由添加

 
  1. 1.在路由守卫中登录成功之后 获取menu菜单数据
  2. //1.发送网络请求获取menu数据
  3. // let res=await 接口
  4. //2.网络数据解析
  5. //3.将component 解析为懒加载组件
  6. //4.解析之后的菜单数据 添加路由实例
  7. // router.addRoute("admin", { path: "" });
  8. // router.getRoutes(); 查配置

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/黑客灵魂/article/detail/804888
推荐阅读
相关标签
  

闽ICP备14008679号