javascript - 将页面重定向到 url Next.JS

标签 javascript url redirect next.js

我有一个运行 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/

相关文章:

javascript - 将函数分配给变量在 javascript firefox 扩展中不起作用

javascript 按键事件和 php

javascript - 使用自动表单验证 Meteor Method 上的模式

jquery - 如何在发生任何更改时检测当前 url

bash - "2>&1 "是什么意思?

javascript - 需要帮助思考如何异步编程

url - 网址缩短器将如何影响搜索引擎爬虫和社交媒体?

php - 使用 PHP 和 Apache 清理类似 Wordpress 的 URL

javascript - MySQL 查询总是失败。页面没有重定向

http - 将 http 流量暂时重定向到另一台服务器