当前位置:   article > 正文

Vue3路由配置createRouter、createWebHistory、useRouter,useRoute

createrouter

Vue3Vue2基本差不多,只不过需要将createRoutercreateWebHistoryvue-router中引入,再进行使用。

手动配置Vue-router环境:

1、下载包: npm i vue-router --save或者 npm i vue-router --S    或者用cdn引入

2、创建路由的js文件(路由、子路由、重定向、开启history模式)

      createRouter、createWebHistory

  1. //路由文件
  2. import { createRouter, createWebHistory } from 'vue-router' //将createRouter、createWebHistory引入vue
  3. const routes = [
  4. {
  5. path: '/', //配置默认路由
  6. name: 'home', //路由名
  7. component: () => import("../views/home.vue"), //引入该路由使用的组件
  8. },
  9. {
  10. path: '/a',
  11. name: 'a',
  12. component: () => import('../views/a.vue'),
  13. redirect: '/a/son1',
  14. children:[ //配置子路由
  15. {
  16. path: '/a/son1', //子路由路径前边必须写父路由路径
  17. name: 'ason1',
  18. component: ()=>import("../views/a-son1.vue")
  19. }
  20. ]
  21. },
  22. {
  23. path: '/b',
  24. name: 'b',
  25. component: () => import('../views/b.vue'),
  26. redirect: '/b/son1', //重定向,进入/b路由时默认进入/b/son1
  27. children:[ //配置子路由
  28. {
  29. path: '/b/son1', //子路由路径前边必须写父路由路径
  30. name: 'bson1',
  31. component: ()=>import("../views/b-son1.vue")
  32. }
  33. ]
  34. }
  35. ]
  36. const router = createRouter({ //设置为history模式
  37. history: createWebHistory(),
  38. routes
  39. })
  40. export default router

3、将配置的路由js文件引入到main.js中

 
 
  1. import { createApp } from 'vue'
  2. import App from './App.vue'
  3. const app=createApp(App)
  4. import router from "./router/index.js" //引入配置路由文件
  5. app.use(router)//记得在mount之前调用
  6. app.mount('#app')

4、界面中使用router-view标签显示路由

组件内部跳转路由、传参useRouter,useRoute

vue3中,在组件内部跳转路由 需要使用useRouter,useRoute方法

useRoute相当于以前的this.$route 跳转路由

用法:

  1. <template>
  2. <h1>aaa</h1>
  3. <router-view></router-view>
  4. <button @click="fn">从a路由跳转到b路由</button>
  5. </template>
  6. <script setup>
  7. import {useRouter} from "vue-router"
  8. let router=useRouter() //接收useRouter方法,在vue2中是直接使用router即可
  9. let fn=()=>{
  10. router.push({path:"/b",query:{name:"小狮子"}}) //path写跳转的路由,同样可以传参
  11. }
  12. </script>
  13. <style scoped>
  14. h1{
  15. width: 400px;
  16. height:200px;
  17. background-color:deeppink;
  18. }
  19. </style>

useRouter相当于this.$router  接受传参(query、params)

注意:1、请注意params只与name(路由文件里配置的路由name)搭配生效(不能使用path)

        2、只能在setup函数内使用

用法

  1. <template>
  2. <h2>这是b-son1</h2>
  3. <button @click="fn">lookquery</button>
  4. </template>
  5. <script setup>
  6. import {useRoute} from "vue-router" //引入
  7. let route=useRoute()
  8. console.log(route.query)//如果是params传参就用route.params接收
  9. let fn=()=>{ //这不是setup函数内部,是取不到传参的,返回undefined
  10. let route=useRoute()
  11. console.log(route)
  12. }
  13. </script>
  14. <style scoped>
  15. h2{
  16. width: 200px;
  17. height:100px;
  18. background-color:aliceblue;
  19. }
  20. </style>

结合前者代码进行验证,发现下图状况

 当我们进行页面跳转时成功获取了传参,但不在setup函数内使用useRouter是获取不了的

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

闽ICP备14008679号