当前位置: 首页 > 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

相关文章:

  • C++ 的协程
  • 前端请求后端php接口跨域 cors问题
  • 如何在算家云搭建Peach-9B-8k-Roleplay(文本生成)
  • Linux kernel 堆溢出利用方法(二)
  • golang分布式缓存项目 Day1 LRU 缓存淘汰策略
  • 【MySQL从入门到放弃】InnoDB磁盘结构(一)
  • 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