1、vue-router切换路由的方式(命名路由和编程式导航和历史记录)

news/2024/7/10 1:37:14 标签: vue.js, javascript, vue, 前端框架, typescript

/src/router/index.ts

typescript">typescript">import { createRouter, createWebHistory, RouteRecordRaw } from "vue-router";

// 路由数组的类型 RouteRecordRaw
const routes: Array<RouteRecordRaw> = [
    {
        path: '/',
        // 命名路由
        // 好处:不需要path: '/'这样url硬编码的形式去跳转路由
        // 名字需唯一,不要和其他组件的路由名字重复
        name: 'Login',
        component: () => import('../components/Login.vue')
    },
    {
        path: '/reg',
        name: 'Reg',
        component: () => import('../components/Reg.vue')
    }
];

const router = createRouter({
    history: createWebHistory(),
    routes
})

export default router;

/src/main.ts

typescript">typescript">import { createApp } from 'vue'
import './style.css'
import App from './App.vue'
// 使用./router代替./router/index.ts,引入时可以省略index.ts
import router from './router';

const app = createApp(App);

app.use(router);
app.mount('#app');

/src/App.vue

前言:如果页面直接使用a标签,切换a标签时具有默认行为,会刷新页面

<template>

  <!-- 
    如果页面直接使用a标签,切换a标签时具有默认行为,会刷新页面
  -->
  <a href="/">登录</a>
  <a href="/reg">注册</a>

</template>

一、使用<router-link>自定义标签切换路由,不会刷新页面

<router-link>链接方式一

to等于字符串,字符串的值是路由里path的值

<template>

  <!-- 
    一、使用<router-link>自定义标签切换路由,不会刷新页面 

    <router-link>链接方式一
    to等于字符串,字符串的值是路由里path的值
  -->
  <router-link to="/">登录1</router-link>
  <router-link to="/reg">注册1</router-link>

  <router-view></router-view>

</template>

<router-link>链接方式二

:to等于对象,对象的内容和router.push()的内容一致

<template>

  <!-- 
    <router-link>链接方式二
    :to等于对象,对象的内容和router.push()的内容一致
  -->
  <router-link :to="{ name: 'Login' }">登录2</router-link>
  <router-link :to="{ name: 'Reg' }">注册2</router-link>

  <router-view></router-view>

</template>

二、使用编程式导航切换路由,给标签添加点击事件

router.push() 会保留历史记录,浏览器地址栏的左右箭头可以点击

router.replace() 不会保留历史记录,浏览器地址栏的左右箭头不能点击

1、router.push()

(1)goJump()的参数是路由里path的值

typescript">typescript"><script setup lang="ts">
import { useRouter } from "vue-router";

const router = useRouter();

const goJump = (path: string) => {
  
  // 编程式导航router.push()

  // 1、push字符串路径
  // router.push(path);

  // 2、push路径对象
   router.push({
    path
   });
}
<template>

  <!-- 二、使用编程式导航切换路由,给标签添加点击事件 -->
  <!-- goJump()的参数是路由里path的值 -->
  <button @click="goJump('/')">登录3</button>
  <button @click="goJump('/reg')">注册3</button>

  <router-view></router-view>

</template>

(2)goJump()的参数是路由里name的值

typescript">typescript"><script setup lang="ts">
import { useRouter } from "vue-router";

const router = useRouter();

const goJump = (path: string) => {
  // 3、push名字对象
  router.push({
    name: path
  });
}
<template>

  <!-- goJump()的参数是路由里name的值 -->
  <button @click="goJump('Login')">登录4</button>
  <button @click="goJump('Reg')">注册4</button>

  <router-view></router-view>

</template>

2、router.replace()

(1)直接在<router-link>标签写replace属性

typescript">typescript"><template>

  <!-- replace 不会保留历史记录,浏览器地址栏的左右箭头不能点击了 -->
  <!-- (1)直接在<router-link>标签写replace属性 -->
  <router-link replace to="/">登录5</router-link>
  <router-link replace to="/reg">注册5</router-link>

  <router-view></router-view>

</template>

(2)使用router.replace()方法

(3)在router.push()中增加replace属性

typescript">typescript"><script setup lang="ts">
import { useRouter } from "vue-router";

const router = useRouter();

const toJupm = (url: string) => {
  // (2)使用router.replace()方法
  // router.replace(url);

  // (3)在router.push()中增加replace属性
  router.push({
    path: url,
    replace: true
  })

}

</script>

<template>

  <button @click="toJupm('/')">登录6</button>
  <button @click="toJupm('/reg')">注册6</button>

  <router-view></router-view>

</template>

http://www.niftyadmin.cn/n/325887.html

相关文章

【STL】string的使用

放在专栏【C知识总结】&#xff0c;会持续更新&#xff0c;期待支持&#x1f339; STL简介 STL的诞生 STL为英文Standard Template Library的缩写&#xff0c;译为标准模板库。是C标准库的重要组成部分。 长久以来&#xff0c;软件届一直希望建立一种可重复运用的东西。所谓…

Spring Security 6.x 系列【38】授权服务器篇之授权端点源码解析

有道无术,术尚可求,有术无道,止于术。 本系列Spring Boot 版本 3.0.4 本系列Spring Security 版本 6.0.2 本系列Spring Authorization Server 版本 1.0.2 源码地址:https://gitee.com/pearl-organization/study-spring-security-demo 文章目录 1. 前言2. 核心类2.1 OAut…

LeetCode高频算法刷题记录1

文章目录 1. 无重复字符的最长子串【中等】1.1 题目描述1.2 解题思路1.3 代码实现 2. 反转链表【简单】2.1 题目描述2.2 解题思路2.3 代码实现 3. LRU 缓存【中等】3.1 题目描述3.2 解题思路3.3 代码实现 4. 数组中的第K个最大元素【中等】4.1 题目描述4.2 解题思路4.3 代码实现…

做网工10年,没人在30岁前和我讲这些(一)

晚上好&#xff0c;我是老杨。 23年才刚过几天&#xff0c;我就感觉自己又上了点年纪&#xff0c;时常面对年纪比较小的粉丝&#xff0c;无意识的面露慈爱的笑容。 还是每次小冬提醒我&#xff0c;我才发现我的表情不对劲。 我对年轻人的包容度是很强的&#xff0c;尤其是一…

【无标题vue3+ts+vite使用@引入组件是报错:找不到模块“@/components/xxx.vue”或其相应的类型声明。ts】

vue3tsvite使用引入组件是报错&#xff1a;找不到模块“/components/xxx.vue”或其相应的类型声明。ts 解决&#xff1a;在tsconfig.json的compilerOptions里写入&#xff1a; "paths": { "/*": ["./src/*"] }

如何自学C++编程语言,聊聊C++的特点,别轻易踩坑

为什么现在有那么多C培训班呢&#xff1f;因为这些培训班可以为学生安排工作&#xff0c;而外包公司因为缺人&#xff0c;需要做很多项目&#xff0c;可能需要在全国各地分配不同的程序员去干不同的项目&#xff0c;因此需要大量的程序员入职。这样&#xff0c;外包公司就会找培…

【C++】内存分区模型

目录 1、缘起 2、内存分区模型 2.1、程序运行前 2.2、程序运行后 3、总结 1、缘起 前几天学习完了 C 的 基础语法 知识点&#xff0c;现在终于要踏上学习 C 核心编程 的旅程了&#xff0c;期待沿途中所遇到的风景。 2、内存分区模型 C 程序在执行时&#xff0c;将内存大…

Spring实现IOC和DI入门案例(XML版)

文章目录 1 IOC入门案例(XML版)1.1 思路分析1.2 代码实现步骤1:创建Maven项目步骤2:添加Spring的依赖jar包步骤3:添加案例中需要的类步骤4:添加spring配置文件步骤5:在配置文件中完成bean的配置步骤6:获取IOC容器步骤7:从容器中获取对象进行方法调用步骤8:运行程序 2 DI入门案例…