• Vue基础-04


    本节依旧是明歌的视频

    一、路由的安装和配置

    (1)在项目终端下载路由:

    npm install vue-router@4

    package.son可以看到整个项目的所有依赖,package-lock.json更详细一点。

    下载完路由,在这里看到有router就说明安装好了。

     (2)在main.js文件配置成这样。

    let routes这里面是配置路由的地址,要创建出对应的路径和文件,否则会报错。

    1. import { createApp } from 'vue'
    2. import './style.css'
    3. import App from './App.vue'
    4. //1.引入路由
    5. import { createRouter, createWebHashHistory} from 'vue-router'
    6. //2.路由配置
    7. let routes = [
    8. { path: '/', component: () => import('../views/Login.vue') },
    9. { path: '/test', component: () => import('../views/Test.vue') },
    10. ]
    11. const router = createRouter({
    12. history: createWebHashHistory(),//路由的形式。hash形式就是会在浏览器路径会有个#
    13. routes
    14. })
    15. //3.原来的路由的挂载:createApp(App).mount('#app'),改成下面这样
    16. let app = createApp(App)
    17. app.use(router)
    18. app.mount('#app')

    PS:vue3框架代码写 vbase3-setup

    1. <template>
    2. <div></div>
    3. </template>
    4. <script setup>
    5. </script>
    6. <style scoped>
    7. </style>

     (3)在APP.vue文件,配置路由的视图.这个插件里面不能加任何内容,不然页面会不显示

      <router-view></router-view>
    

     这样路由就配置完了。

    然后发现页面有点距离,则在index.vue里面去掉所有的padding和margin的值。

     二、路由的使用

    Test页面点击之后通过路由传值给Login页面。

    test页面:

    (1)引入路由 (2)注册路由 (3)route.push跳转

    1. <template>
    2. <div>test</div>
    3. <div>
    4. <button @click="oneClick">按下天天,出现彩虹</button>
    5. </div>
    6. </template>
    7. <script setup>
    8. //1、引入路由
    9. // import { ref, reactive } from 'vue'
    10. import { useRouter, useRoute } from 'vue-router'
    11. //2、注册路由
    12. const router = useRouter()
    13. const route = useRoute()//获取上级路由传参
    14. //3、使用路由跳转传值的方法
    15. function oneClick() {
    16. router.push({
    17. path: '/',
    18. query: {//传参
    19. name: "彩虹",
    20. id: 1,
    21. },
    22. })
    23. }
    24. </script>
    25. <style scoped>
    26. </style>

     login接收数据的页面:

    1.引入路由  2.注册路由  3.赋值到当前页面 

    1. <template>
    2. <div>Login</div>
    3. {{ info.id }}----{{ info.name }}
    4. </template>
    5. <script setup>
    6. //1、引入路由
    7. import { useRouter, useRoute } from 'vue-router'
    8. import { ref, reactive } from 'vue'//引入要用的模块
    9. //2、注册路由
    10. const router = useRouter()
    11. const route = useRoute()//获取上级路由传参
    12. console.log(reactive)//输出获取的数组
    13. let info = reactive({//3.赋值到当前页面
    14. id: route.query.id,
    15. name: route.query.name,
    16. })
    17. </script>
    18. <style scoped>
    19. </style>

    三、二级路由的使用

     

    (1)路由跳转:1.引入路由 2.路由注册 3.写方法,使用router.oush跳转路径。

     代码

    1. <template>
    2. <div>后台管理首页</div>
    3. <div style="display:flex">
    4. <div>
    5. <button @click="oneClick">跳转page1</button>
    6. <button @click="twoClick">跳转page2</button>
    7. </div>
    8. <router-view></router-view>
    9. </div>
    10. </template>
    11. <script setup>
    12. //1、引入路由
    13. import { useRouter, useRoute } from 'vue-router'
    14. //2、注册路由
    15. const router = useRouter()
    16. const route = useRoute()//获取上级路由传参
    17. //3、使用路由跳转传值的方法
    18. function oneClick() {
    19. router.push({
    20. path: '/main/page1',
    21. })
    22. }
    23. function twoClick() {
    24. router.push({
    25. path: '/main/page2',
    26. })
    27. }
    28. </script>
    29. <style scoped>
    30. </style>

    传参的方式传路由

    1. <template>
    2. <div>后台管理首页</div>
    3. <div style="display:flex">
    4. <div>
    5. <button @click="oneClick('page1')">跳转page1</button>
    6. <button @click="twoClick('page2')">跳转page2</button>
    7. </div>
    8. <router-view></router-view>
    9. </div>
    10. </template>
    11. <script setup>
    12. //1、引入路由
    13. import { useRouter, useRoute } from 'vue-router'
    14. //2、注册路由
    15. const router = useRouter()
    16. const route = useRoute()//获取上级路由传参
    17. //3、使用路由跳转传值的方法
    18. function oneClick(val) {
    19. router.push({
    20. path: '/main/' + val,
    21. })
    22. }
    23. function twoClick(val2) {
    24. router.push({
    25. path: '/main/' + val2,
    26. })
    27. }
    28. </script>
    29. <style scoped>
    30. </style>

    (2)main.js的二层路由配置

    1. let routes = [
    2. {
    3. path: '/main', component: () => import('./views/main.vue'),
    4. children: [{ path: '/main/page1', component: () => import('./views/Page1.vue') },
    5. { path: '/main/page2', component: () => import('./views/Page2.vue') }]
    6. },
    7. ]

  • 相关阅读:
    使用反射调用私有内部类方法
    μC/OS-II---事件标志组管理1(os_flag.c)
    postman:Tests模块之断言
    电脑重装系统后usbcleaner怎么格式化u盘
    [正式学习java①]——java项目结构,定义类和创建对象,一个标准javabean的书写
    微信小程序开发(四) - 页面配置 - json 文件
    基于 socketio 的 room 的使用
    wav文件碎片多删除后恢复案例
    【STM32】STM32学习笔记-WDG看门狗(46)
    用户授权设置全局变量为undefined?
  • 原文地址:https://blog.csdn.net/m0_59214979/article/details/126415586