Vue3 Vue-Router详解 Vue3配置hash 和 history路由、Vue3封装的路由hook函数(useRouter,useRoute)的使用 路由懒加载、路由分包处理、魔法注释的使用

这篇具有很好参考价值的文章主要介绍了Vue3 Vue-Router详解 Vue3配置hash 和 history路由、Vue3封装的路由hook函数(useRouter,useRoute)的使用 路由懒加载、路由分包处理、魔法注释的使用。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

// hash 模式
// import { createRouter, createWebHashHistory } from 'vue-router'

// history 模式
import { createRouter, createWebHistory } from 'vue-router'

const routes = [
  // redirect 重定向 默认进入去到 home
  { path: '/', redirect: '/home' },
  {
    // name属性:路由记录独一无二的名称
    name: 'homeCom',
    // meta属性:自定义的数据
    meta: { name: 'HachimanC', age: 20 },
    // 跳转的路径
    path: '/home', 
    // 路由懒加载 路由分包处理  npm run build  /* webpackChunkName: "homeCom" */ => 魔法注释 分包后进行打包后 包的名字
    component: () => import(/* webpackChunkName: "homeCom" */ '../views/homeCom.vue')
  },
  // 传递 id
  { path: '/user/:id', component: () => import(/* webpackChunkName: "userCom" */ '../views/userCom.vue') },
  // 路径不正确显示
  { path: '/:pathMatch(.*)', component: () => import(/* webpackChunkName: "notFound" */ '../views/NotFound.vue') },
]

const router = createRouter({
  // history: createWebHashHistory(), // hash 模式
  history: createWebHistory(process.env.BASE_URL),  // history 模式
  routes,
})

export default router

 html部分

<template>
  <div>路由切换渲染</div>
  <router-link to="/home" active-class="linkClass">首页</router-link>
  <!-- replace 替换 点击不会返回上一页直接跳转浏览器首页 -->
  <router-link to="/user/123" replace>用户123</router-link>
  <router-link to="/user/321">用户321</router-link>
  <button class="btn" @click="jumpHandler('/home')">首页</button>
  <button class="btn" @click="jumpHandler('/user/123')">用户</button>
  <router-view></router-view>
</template>

js部分

<script setup>
import { useRouter } from 'vue-router'
const route = useRouter()
const jumpHandler = path => {
  // route.replace(path)
  route.push(path+`?name=Hachiman&age=20&id=123`)
}
</script>

<style scoped>
.linkClass {
  color: #ff0000;
  font-size: 20px;
}
.btn {
  border: none;
  background: #ff0000;
  margin: 0 10px;
}
</style>

 html页面使用路由传来的参数

<template>
  <h2>
    home组件 传递来的参数{{ $route.query }}
  </h2>
</template>

 获取router跳转id

<template>
  <h2>用户id: {{ $route.params.id }}</h2>
  <button @click="backHandler">返回</button>
</template>
<script setup>
import { useRoute, useRouter, onBeforeRouteUpdate } from 'vue-router'
const route = useRoute()
console.log(route.params.id)
// 获取 route 跳转 id
onBeforeRouteUpdate((to, from) => {
  console.log('from:', from.params.id)
  console.log('to:', to.params.id)
})
const router = useRouter()
const backHandler = () => {
  // 后退 和 前进
  // router.back()
  // router.forward()

  // go(1) => forward()
  // go(-1) => back()
  router.go(-1)
}
</script>

获取 路由跳转错误地址文章来源地址https://www.toymoban.com/news/detail-678296.html

<template>
    <div class="notFound">路径{{ $route.params.pathMatch }}不正确</div>
</template>
<style>
.notFound{
  font-size: 30px;
  color: red;
}
</style>

到了这里,关于Vue3 Vue-Router详解 Vue3配置hash 和 history路由、Vue3封装的路由hook函数(useRouter,useRoute)的使用 路由懒加载、路由分包处理、魔法注释的使用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【vue3】13-前端路由-Vue-Router的详解: 从入门到掌握

    路由其实是网络工程中的一个术语: 在 架构一个网络 时,非常重要的两个设备就是 路由器和交换机 。 当然,目前在我们生活中 路由器 也是越来越被大家所熟知,因为我们生活中都会用到 路由器 : 事实上, 路由器 主要维护的是一个 映射表 ; 映射表 会决定数据的流向; 路由

    2024年02月09日
    浏览(36)
  • 基于vscode实现vue3项目创建启动+安装配置路由vue-router实现单页面组件切换

    访问https://nodejs.org/en,点击下载最新版本的nodejs,并安装。 在项目目录文件下,通过cmd运行下述指令。 依次输入下列命令,启动vue项目 在浏览器中加载http://localhost:5173/,页面加载成功,说明vue项目安装启动成功。 建议安装第三方库通过vscode中的终端来操作,项目启动通过

    2024年02月03日
    浏览(46)
  • vue3中路由hash与History的设置

     history模式直接指向history对象,它表示当前窗口的浏览历史,history对象保存了当前窗口访问过的所有页面网址。URL中没有#,它使用的是传统的路由分发模式,即用户在输入一个URL时,服务器会接收这个请求,并解析这个URL,然后做出相应的逻辑处理。 当使用history模式时,

    2024年02月16日
    浏览(56)
  • vue3使用vue-router嵌套路由(多级路由)

    Vue3 嵌套路由的使用和 Vue2 相差不大,主要的区别是 Vue3 的路由实例化使用了 createApp() 方法,所以实例化路由时需要传入根组件。另外,Vue3 的路由对象除了包含 Vue2 中的导航守卫、导航钩子和解析守卫等功能外,还新增了 meta prop 和 route prop。 在使用嵌套路由时,建议将路由

    2024年02月03日
    浏览(39)
  • vue3如何切换hash/history两种路由模式

    本文介绍了在vue3中,如何使用history和hash两种路由模式的方法 1、history模式 使用createWebHistory 2、hash模式 使用createWebHashHistory 综上所述: history 对应 createWebHistory hash 对应 createWebHashHistory

    2024年02月13日
    浏览(31)
  • Vue 路由:Hash 模式与 History 模式详解

            Vue 是一款流行的前端框架,它提供了强大的路由功能,用于构建单页应用程序(SPA)。Vue 路由有两种模式:Hash 模式和 History 模式。本文将详细介绍这两种模式的原理、特点和使用场景,帮助你在 Vue 项目中选择适合的路由模式。         Hash 模式是 Vue 路由的

    2024年02月06日
    浏览(41)
  • vue-router路由模式详解

    目录 一. vue-router(前端路由)有两种模式,hash模式和history模式 二、路由模式解析 三、两种模式的区别 1、hash模式  2、history路由 (3)popstate实现history路由拦截,监听页面返回事件 一. vue-router(前端路由)有两种模式,hash模式和history模式 1.hash 就是指 url 后面的 # 号以及后

    2024年02月03日
    浏览(62)
  • 基于vscode开发vue3项目的详细步骤教程 3 前端路由vue-router

    1、Vue下载安装步骤的详细教程(亲测有效) 1_水w的博客-CSDN博客 2、Vue下载安装步骤的详细教程(亲测有效) 2 安装与创建默认项目_水w的博客-CSDN博客 3、基于vscode开发vue项目的详细步骤教程_水w的博客-CSDN博客 4、基于vscode开发vue项目的详细步骤教程 2 第三方图标库FontAwesome_水w的

    2024年02月02日
    浏览(52)
  • 5.10 Vue配置路由(vue-router)

    官方网站:https://v3.router.vuejs.org/zh/installation Vue Router 是 Vue.js 官方提供的一种路由管理工具,它可以帮助开发者管理 Vue.js 应用程序的路由,并实现路由跳转、参数传递、嵌套路由等功能。Vue Router 可以将一个单页面应用分成多个视图,在不同的路由之间进行切换,从而实现了

    2024年02月08日
    浏览(37)
  • Vue学习笔记 之 History 路由 和 Hash 路由的区别 及 History 模式时,Nginx的配置方式

    一、History 模式、Hash 模式   Vue Router 是 Vue.js 官方的路由管理器,用于构建单页应用的前端路由。它允许你通过定义路由配置来映射不同的 URL 到对应的组件,实现页面间的跳转和导航。Vue Router 支持两种路由模式:history 模式和 hash 模式。 1、History 模式   在 History 模式

    2024年02月07日
    浏览(30)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包