【发布时间】:2019-08-29 03:25:26
【问题描述】:
如何在 nextjs 中获取上一个 URL?
我认为this.props.router.asPath 和nextProps.router.asPath 的值是不同的。
其实我想在登录后拨打router.push。我知道router.back 转到上一页。但是可以转到另一个站点。有历史栈的用户去上一页,没有历史栈的用户去/主页面。
import { Component } from 'react'
import App, { Container } from 'next/app';
import ErrorComponent from '@/components/error'
export default class MyApp extends App {
render() {
console.log(this.props)
const { Component, pageProps, router } = this.props;
const props = {
...pageProps,
router
}
return (
<ErrorBoundary>
<Container>
<Component {...props} />
</Container>
</ErrorBoundary>
);
}
componentWillReceiveProps(nextProps) {
// previous page url /contents
console.log(this.props.router.asPath) // /about
console.log(nextProps.router.asPath) // /about
console.log('window.history.previous.href', window.history.previous) // undefined
}
}
我该如何解决?或者登录后如何获取上一个URL来移动页面?
【问题讨论】:
-
这个问题基本上是重复的:stackoverflow.com/a/65614253/10367188
标签: next.js