当前位置: 首页 > article >正文

vue3 vue-router 笔记

1.安装

 npm install vue-router

2.创建router文件,在main.js引入,创建home.about页面 app.vue文件添加路由占位符
router/index.js

  import { createRouter,createWebHashHistory, createWebHistory } from 'vue-router'
  import home from './views/home.vue'
  import about from '../views/about.vue'
  const routes = [
    {
      path:'/home',
      component: home
     },
     {
      path:'/about',
      component: about
     }
  ]
  const router = createRouter({
    // hash 模式跟 history 模式
    history: createWebHashHistory(),
    routes
  })
  export default router

main.js

import { createApp } from 'vue'
import App from './App.vue'
import router from "./route/index"
const app = createApp(App)
app.use(router)
app.mount('#app')


app.vue

 <!-- replace 替换路径,不会回退 -->
    <router-link to="/home" replace active-class="active">首页</router-link>
    <router-link to="/about" active-class="active">关于</router-link>
    <router-view></router-view>

3.路由懒加载,redirect 从定向

import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'


// 路由懒加载
// const home = () => import(/* webpackChunkName :'home'*/'../views/home.vue')
// const about = () => import(/* webpackChunkName :'about'*/'../views/about.vue')
const routes = [
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/home',
        name: 'home',
        component: () => import(/* webpackChunkName :'home'*/'../views/home.vue'),
    },
    {
        path: '/about',
        name: 'about',
        component: () => import(/* webpackChunkName :'about'*/'../views/about.vue')
    }
]
const router = createRouter({
    history: createWebHistory(),
    routes
})
export default router

4.动态路由 创建user.vue文件
router/index.js

import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
const routes = [

    // 动态路由
    {
        path: '/user/:id',
        name: 'user',
        component: () => import('../views/user.vue')
    }
]
const router = createRouter({
    history: createWebHistory(),
    routes
})
export default router

app.vue

 <router-link to="/user/123" active-class="active">用户123</router-link>
 <router-link to="/user/456" active-class="active">用户456</router-link>

user.vue

<div>user{{ $route.params.id }}</div>

5.404页面处理,创建404文件
使用 pathMatch

import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
const routes = [
    // 404 页面不存在路由  后面加* 会解析输入的路径,不加则不解析
    {
        path: '/:pathMatch(.*)*',
        component: () => import('../views/404.vue')
    }
]
const router = createRouter({
    history: createWebHistory(),
    routes
})
export default router

6.路由嵌套 创建详情1,2 文件

import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
const routes = [
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/home',
        name: 'home',
        component: () => import(/* webpackChunkName :'home'*/'../views/home.vue'),
        children: [
            {
               path:'/home',
               redirect:'/home/detailOne'
            },
            {
                path: 'detailOne', //等同于 /home/detailOne
                component: () => import('../views/detailOne.vue')
            },
            {
                path:'detailTwo',
                component:()=> import('../views/detailTwo.vue')
            }
        ]
    }
]
const router = createRouter({
    history: createWebHistory(),
    routes
})
export default router

7.编程式跳转
app.vue 添加点击事件

<hr />
    <span @click="homeBtnClick">首页</span>
    <button @click="aboutBtnClick">关于</button>
    <button @click="goBack">返回</button>
    <router-view></router-view>

添加事件方法 query 传参

<script setup>
import { useRouter } from "vue-router";
const router = useRouter();
function homeBtnClick() {
  router.push("/home");
}
function aboutBtnClick() {
  router.push({
    path: "/about",
    query: {
      name: "言念",
      age: 24,
    },
  });
}
function goBack() {
  router.back();
  router.go(-1);
}
</script>

about.vue

<template>
    <div class="about">
         about
         <h1>{{$route.query}}</h1>
    </div>
</template>
<script setup>
</script>
<style scoped>

</style>

8.完整router/index.js

import { createRouter, createWebHashHistory, createWebHistory } from 'vue-router'
// import home from '../views/home.vue'
// import about from '../views/about.vue'


// 路由懒加载
// const home = () => import(/* webpackChunkName :'home'*/'../views/home.vue')
// const about = () => import(/* webpackChunkName :'about'*/'../views/about.vue')
const routes = [
    {
        path: '/',
        redirect: '/home'
    },
    {
        path: '/home',
        name: 'home',
        component: () => import(/* webpackChunkName :'home'*/'../views/home.vue'),
        children: [
            {
               path:'/home',
               redirect:'/home/detailOne'
            },
            {
                path: 'detailOne', //等同于 /home/detailOne
                component: () => import('../views/detailOne.vue')
            },
            {
                path:'detailTwo',
                component:()=> import('../views/detailTwo.vue')
            }
        ]
    },
    {
        path: '/about',
        name: 'about',
        component: () => import(/* webpackChunkName :'about'*/'../views/about.vue')
    },
    // 动态路由
    {
        path: '/user/:id',
        name: 'user',
        component: () => import('../views/user.vue')
    },
    // 404 页面不存在路由  后面加* 会解析输入的路径,不加则不解析
    {
        path: '/:pathMatch(.*)*',
        component: () => import('../views/404.vue')
    }
]
const router = createRouter({
    history: createWebHistory(),
    routes
})
export default router

9.完整的app.vue

<template>
  <div class="app">
    <h1>app content</h1>
    <!-- replace 替换路径,不会回退 -->
    <router-link to="/home" replace active-class="active">首页</router-link>
    <router-link to="/about" active-class="active">关于</router-link>
    <router-link to="/user/123" active-class="active">用户123</router-link>
    <router-link to="/user/456" active-class="active">用户456</router-link>
    <hr />
    <span @click="homeBtnClick">首页</span>
    <button @click="aboutBtnClick">关于</button>
    <button @click="goBack">返回</button>
    <router-view></router-view>
  </div>
</template>

<script setup>
import { useRouter } from "vue-router";
const router = useRouter();
function homeBtnClick() {
  router.push("/home");
}
function aboutBtnClick() {
  router.push({
    path: "/about",
    query: {
      name: "言念",
      age: 24,
    },
  });
}
function goBack() {
  router.back();
  router.go(-1);
}
</script>
<style>
.active {
  color: red;
  font-size: 20px;
}
</style>


http://www.kler.cn/a/135948.html

相关文章:

  • 高效准确的PDF解析工具,赋能企业非结构化数据治理
  • c++ ------语句
  • PHP 微信棋牌开发全解析:高级教程
  • 计算机组成原理的学习笔记(5)--数据的表示与运算·其四 浮点数的储存和加减/内存对齐/大端小端
  • 54、库卡机器人轴的软限位设置
  • react 项目打包二级目 使用BrowserRouter 解决页面刷新404 找不到路由
  • C# static关键字详解
  • 【Java程序员面试专栏 算法训练篇】二叉树高频面试算法题
  • 10-19 HttpServletResponse
  • ComText让机器人有了情节记忆
  • Upwork 新手使用指南——如何快速在Upwork上接单
  • 基于Vue+SpringBoot的校园电商物流云平台开源项目
  • MTK Pump Express 快速充电原理分析
  • 美国服务器:全面剖析其主要优点与潜在缺点
  • npm报错
  • QT自定义信号,信号emit,信号参数注册
  • c++处理tcp粘包问题以及substr方法
  • vue3使用element-plus
  • 拼图小游戏
  • 轻松实现公网访问本地内网搭建的WBO白板【内网穿透】
  • Labview中for循环“无法终止”问题?即使添加了条线接线端,达到终止条件后,仍在持续运行?
  • PostgreSQL 难搞的事系列 --- vacuum 的由来与PG16的命令的改进 (1)
  • 基于LLM+场景识别+词槽实体抽取实现多轮问答
  • 进程管理(三)
  • POS系统完整体系的介绍 Pos终端主密钥MK、DUKPT、PEK、DEK、MEK、TUSN的含义 ---安全行业基础篇7
  • CICD 持续集成与持续交付——git