Pinia+Router学习笔记(九)

简介: 本节记录路由历史记录相关内容

的replace属性:

  • 作用:控制路由跳转时操作历史记录的模式,如果加上了replace属性则跳转后会清除之前所有历史记录使浏览器不能后退
  • 使用方法(模板):
<router-link replace to="/reg">点击replace</router-link>

除这种用法外,也可以使用编程式路由导航router.replace

<template>
    <h1>小满最骚</h1>
    <button @click="replace">replace</button>
    <router-view></router-view>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
const replace = () => {
    router.replace('/reg')
}
</script>

<style scoped></style>

需要注意的是,router.replace中传入对象并指定name属性的方法已被废弃,若要使用配置对象请用path属性

<template>
    <h1>小满最骚</h1>
    <button @click="replace">replace</button>
    <router-view></router-view>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
const replace = () => {
    router.replace({
    path:'/reg',
  })
}
</script>

<style scoped></style>

go与back

作用:横跨历史,采用一个整数作为参数,表示在历史记录中前进或后退多少步
<template>
  <h1>小满最骚</h1>
  <button @click="next">next</button>
  <br />
  <button @click="back">back</button>
  <router-view></router-view>
</template>
<script setup lang="ts">
  import { ref, reactive } from 'vue'
  import { useRouter } from 'vue-router'
  const router = useRouter()
  const next = () => {
    // 正数表示前进,负数表示后退
    router.go(1)
  }
  const back = () => {
    router.back()
  }
</script>

完整示例:

<template>
    <h1>小满最骚</h1>
    <router-link replace to="/reg">点击replace</router-link>
    <button @click="toPage('Login')">Login</button>
    <br />
    <button @click="toPage('Reg')">Reg</button>
    <br />
    <button @click="next">next</button>
    <br />
    <button @click="back">back</button>
    <br />
    <button @click="replace">replace</button>
    <router-view></router-view>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
const router = useRouter()
const toPage = (name: string) => {
    router.push({
        name,
    })
}
const replace = () => {
    router.replace({
        path:'/reg',
    })
}
const next = () => {
    router.go(1)
}
const back = () => {
    router.back()
}
</script>

<style scoped></style>
相关文章
|
8月前
|
缓存 Android开发 开发者
Flutter环境配置完成后,如何在Android设备上运行Flutter应用程序?
Flutter环境配置完成后,如何在Android设备上运行Flutter应用程序?
1458 62
|
XML JSON 人工智能
Error while extracting response for type [class xxx] and content type application/xml;charset=UTF-8
Error while extracting response for type [class xxx] and content type application/xml;charset=UTF-8
2393 0
Saga模式在分布式系统中如何保证事务的隔离性
Saga模式在分布式系统中如何保证事务的隔离性
307 7
|
存储 缓存 NoSQL
如何解决Ubuntu server 下 Redis安装报错:“You need tcl 8.5 or newer in order to run the Redis test”.
如何解决Ubuntu server 下 Redis安装报错:“You need tcl 8.5 or newer in order to run the Redis test”.
797 0
|
机器学习/深度学习 传感器 数据采集
【BP回归预测】基于BP神经网络的回归预测附matlab完整代码
【BP回归预测】基于BP神经网络的回归预测附matlab完整代码
|
SQL 运维 监控
MSSQL性能调优深度解析:索引优化策略、SQL查询优化技巧与高效并发管理实践
在Microsoft SQL Server(MSSQL)的运维与优化领域,性能调优是确保数据库高效运行、满足业务需求的关键环节
|
SQL 存储 安全
sql数据库优点,SQL数据库的优点包
SQL数据库优点概述:结构化数据存储,保证一致性和完整性;支持事务处理、数据安全机制;擅长大规模数据处理,标准化查询语言,具良好可移植性;灵活定制,支持多用户并发;具备备份恢复机制,适合数据分析和报表;拥有成熟生态系统和工具支持,广泛应用于各类场景。
182 0
|
应用服务中间件 nginx
X-Accel-Redirect"和"X-Accel-Limit-Rate
X-Accel-Redirect"和"X-Accel-Limit-Rate
392 1
webflux中操作符调试
webflux中操作符调试
230 0
|
Linux 虚拟化
VMware虚拟机中Centos 6.x系统磁盘空间扩容实战
VMware虚拟机中Centos 6.x系统磁盘空间扩容实战
767 0