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天前
|
JavaScript
vue.router和vue.route
vue.router和vue.route
|
8天前
|
JavaScript
|
8天前
|
缓存 移动开发 JavaScript
【学习笔记】Vue Router
【学习笔记】Vue Router
33 0
|
8月前
|
缓存 JavaScript
Vue Router 学习 new Router
Vue Router 学习 new Router
83 0
|
8天前
|
JavaScript 前端开发 网络架构
Vue Router:让你的应用路由起来!
Vue Router:让你的应用路由起来!
|
12月前
|
JSON JavaScript Java
vue,router,axios练习
vue,router,axios练习
|
JavaScript
Pinia+Router学习笔记(二)
本节记录修改State中值的五种方式
67 0
Pinia+Router学习笔记(三)
本节记录解构Store过程中的相关操作及注意事项
62 0
|
前端开发 中间件 SEO
Pinia+Router学习笔记(七)
本节介绍Vue-Router的两种路由模式
66 0
|
API
Pinia+Router学习笔记(五)
本节记录例API和Pinia持久化插件相关内容
67 0

热门文章

最新文章