this.$router.go(-1)要调用2次
时间: 2024-04-28 16:20:05 浏览: 7
可能是因为你需要先调用一次 `this.$router.back()` 方法将页面返回到上一页,然后再调用一次 `this.$router.go(-1)` 方法将页面返回到上上一页。如果仍然不行,你可以尝试使用 `this.$router.push({ path: '/' })` 方法将页面跳转到首页,然后再使用 `this.$router.go(-2)` 方法将页面返回到上上一页。希望这能帮到你。
相关问题
react this.$router.go(-1);
在React中,我们使用react-router来进行路由管理。如果你想在React组件中实现类似于`this.$router.go(-1)`的功能,可以按照以下步骤操作:
1. 首先,确保你已经安装了`react-router-dom`依赖包。你可以使用以下命令进行安装:
```shell
npm install react-router-dom
```
2. 在你的组件文件中引入`BrowserRouter`和`useHistory`:
```javascript
import { BrowserRouter, useHistory } from "react-router-dom";
```
3. 在组件函数内部使用`useHistory`钩子来获取路由历史对象:
```javascript
const history = useHistory();
```
4. 然后,你可以通过调用`history.goBack()`方法来返回上一个路由页面:
```javascript
history.goBack();
```
这样就可以实现在React中返回上一个页面的功能了。希望对你有所帮助!
this.$router.go(-1)返回上一页路由携带的参数会消失吗
和this.$router.back()` 方法类似,`this.$router.go(-1)` 方法返回上一页时,上一页的路由信息和参数也会保留,不会丢失路由参数。
如果你需要在返回上一页时传递参数,可以使用 `query` 或 `params` 参数进行传递。例如,如果你要传递一个名为 `id` 的参数,可以这样调用 `this.$router.go(-1, {query: {id: yourId}})` 或 `this.$router.go(-1, {params: {id: yourId}})`,这样返回上一页时,参数会被带回并可以在上一页的组件中进行处理。