我有一个运行 next.js 的简单网页,该页面只返回“Hello World”元素,我希望它将我重定向到另一个 URL (youtube)。
它基本上是加载时的重定向页面。
我的简单页面:
function Home() {
return <div>Hello World</div>
}
export default Home
我什至尝试了js的window.location函数,但是没有用
最佳答案
在 next.js
中,您可以使用 Router
重定向页面加载后 例如:
import Router from 'next/router'
componentDidMount(){
const {pathname} = Router
if(pathname == '/' ){
Router.push('/hello-nextjs')
}
}
或者用钩子(Hook):
import React, { useEffect } from "react";
...
useEffect(() => {
const {pathname} = Router
if(pathname == '/' ){
Router.push('/hello-nextjs')
}
});
关于javascript - 将页面重定向到 url Next.JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65952817/