【Vue-Router】路由跳转

news/2025/2/26 5:03:59

1. 路由标签

App.vue

<template>
  <h1>hello world</h1>
  <div>
    <router-link to="/">Login</router-link>
    <router-link style="margin: 10px;" to="/reg">Reg</router-link>
  </div>
  <hr>
  <router-view></router-view>
</template>

<script setup lang="ts">javascript">

</script>

<style scoped>

</style>

2. 命名路由

index.ts

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

const routes: Array<RouteRecordRaw> = [
  {
    path: "/",
    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

App.vue

<template>
  <h1>hello world</h1>
  <div>
    <router-link :to="{name:'Login'}">Login</router-link>
    <router-link style="margin: 10px;" :to="{name:'Reg'}">Reg</router-link>
  </div>
  <hr>
  <router-view></router-view>
</template>

<script setup lang="ts">javascript">

</script>

<style scoped>

</style>

3. <a>标签

<a>标签替换存在默认行为,会刷新页面

index.ts

<template>
  <h1>hello world</h1>
  <div>

    <a href="/">Login</a>
    <a href="/reg">Reg</a>
  </div>
  <hr>
  <router-view></router-view>
</template>

<script setup lang="ts">

</script>

<style scoped>

</style>

4. 编程式导航

App.vue

<template>
  <h1>hello world</h1>
  <div>
    <button @click="toPage('./')">Login</button>
    <button @click="toPage('./reg')">Reg</button>
  </div>
  <hr>
  <router-view></router-view>
</template>

<script setup lang="ts">javascript">
import { useRouter } from 'vue-router';
const router = useRouter();
const toPage = (path: string) => {
  // 1.字符串
  // router.push(path);
  // 2.对象
  router.push({
    path: path
  })
  // 3.命名式
}
</script>

<style scoped></style>
<template>
  <h1>hello world</h1>
  <div>
    <button @click="toPage('Login')">Login</button>
    <button @click="toPage('Reg')">Reg</button>
  </div>
  <hr>
  <router-view></router-view>
</template>

<script setup lang="ts">javascript">
import { useRouter } from 'vue-router';
const router = useRouter();
const toPage = (path: string) => {
  // 1.字符串
  // router.push(path);
  // 2.对象
  // router.push({
  //   path: path
  // })
  // 3.命名式
  router.push({
    path: path
  })
}
</script>

<style scoped></style>

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

相关文章

【Go 基础篇】Go语言字符类型:解析字符的本质与应用

介绍 字符类型是计算机编程中用于表示文本和字符的数据类型&#xff0c;是构建字符串的基本单位。在Go语言&#xff08;Golang&#xff09;中&#xff0c;字符类型具有独特的特点和表示方式&#xff0c;包括Unicode编码、字符字面值以及字符操作。本篇博客将深入探讨Go语言中的…

metaRTC7 demo mac/ios编译指南

概要 metaRTC7.0开始全面支持mac/ios操作系统&#xff0c;新版本7.0.023 mac os demo 包含有srs/zlm的推拉流演示。发布版自带了x64版第三方类库&#xff0c;arm版第三方类库还需开发者自己编译。 源码下载 下载文件metartc7.023.7z https://github.com/metartc/metaRTC/re…

修改Linux中SSH的端口

文章目录 修改Linux中SSH的端口Linux中默认的ssh端口关闭SELinux测试新端口 修改Linux中SSH的端口 Linux中默认的ssh端口 使用root用户操作 修改前先备份ssh_config cp /etc/ssh/sshd_config /etc/ssh/sshd_config_date "%Y%m%d%H%M%S"修改配置文件&#xff0c;找…

RunLoop

1.CFRunLoopModeRef特征代表RunLoop对象内的运行模式(每个RunLoop对象内存中存在很多种运行模式,每个Mode运行模式下必然包含若干个有效的Source0/Source1/Timer/Observer数据序组) 2.RunLoop对象活跃(操作)启动时能且仅能选择某个Mode匹配currentMode(暗示Loop对象的操作运行必…

最新 python 爱心代码?

python程序代码&#xff1a;heart.py from math import cos, pi import numpy as np import cv2 import os, globclass HeartSignal:def __init__(self, curve"heart", title"Love U", frame_num20, seed_points_num2000, seed_numNone, highlight_rate0.…

斐波拉契数列+二进制--夏令营

1. f[40]{0,1} 数组赋值&#xff1a;只赋值前两个的话&#xff0c;剩余的自动为0 2.先要自己写出斐波拉契数列判断一下应该要多少个斐波拉契数样例&#xff0c;第39项已经超样例数500了&#xff0c;所以够用 3.就是把一个数字拆分成斐波拉契数列里的数的和嘛&#xff0c;但是…

【前端|Javascript第4篇】详解Javascript的事件模型:小白也能轻松搞懂!

前言 在当今数字时代&#xff0c;前端技术正日益成为塑造用户体验的关键。而其中一个不可或缺的核心概念就是JavaScript的事件模型。或许你是刚踏入前端领域的小白&#xff0c;或者是希望深入了解事件模型的开发者&#xff0c;不论你的经验如何&#xff0c;本篇博客都将带你揭开…

【Docker】如何在设计 dockerfile 过程中,设置容器启动后的定时任务

如何在设计 dockerfile 过程中&#xff0c;设置容器启动后的定时任务 jwensh 2023.08.14 文章目录 如何在设计 dockerfile 过程中&#xff0c;设置容器启动后的定时任务1. 基于 alpine 设计 dockerfile 过程中&#xff0c;设置容器启动后的定时任务2. 基于 CentOS 设计 Dockerf…