目录
一、场景说明
对于单页应用来说,经常会有登录后访问某个页面的场景。比如
/index -> /login -> /page1
但是在page1返回上一页时,会返回到登录页。回退路径为
/page1-> /login -> /index
因此需要进行跳过登录页的历史记录处理。
二、处理方案
1.router-link + history
<template>
login页
<router-link replace to="/page1">登录后访问page1</router-link>
</template>
此时在page1页的回退路径为
/page1 -> /index
2.编程式跳转
<template>
login页
<button @click="replaceJump">登录后访问page1</button>
</template>
<script setup lang='ts'>
import {useRouter} from 'vue-router'
const router = useRouter();
// 传递全路径跳转
const replaceJump = ()=>{
router.replace('/page1')
}
</script>
<style>
</style>
结果同上。
三、其他api跳转
此外,router对象还有其他跳转api使用说明如下
/**
* Go back in history if possible by calling `history.back()`. Equivalent to
* `router.go(-1)`.
*/
back(): ReturnType<Router['go']>;
/**
* Go forward in history if possible by calling `history.forward()`.
* Equivalent to `router.go(1)`.
*/
forward(): ReturnType<Router['go']>;
/**
* Allows you to move forward or backward through the history. Calls
* `history.go()`.
*
* @param delta - The position in the history to which you want to move,
* relative to the current page
*/
go(delta: number): void;