3 回答

TA貢獻1797條經(jīng)驗 獲得超4個贊
無需額外的插件。
要進行測試,請復制 NextJs 示例應(yīng)用程序:
npx?create-next-app?nextjs-blog?--use-npm?--example?"https://github.com/vercel/next-learn-starter/tree/master/learn-starter"
next.config.js
使用以下代碼將文件添加到根文件夾:
// this simply tests the redirecting of the root path (source)
module.exports = {
? async redirects() {
? ? return [
? ? ? {
? ? ? ? source: '/',
? ? ? ? destination: 'https://stackoverflow.com/posts/66662033',
? ? ? ? permanent: false,
? ? ? ? basePath: false
? ? ? },
? ? ]
? },
};
當您啟動應(yīng)用程序npm run dev
并訪問 localhost:3000 時,它應(yīng)該重定向到 next.config.js 腳本中指定的目標 URL 。

TA貢獻1829條經(jīng)驗 獲得超6個贊
您可以嘗試使用 window.location
這是一個示例,其中在訪問頁面時將用戶重定向到外部 url
// pages/redirect
import {useEffect} from 'react'
export default function redirect() {
useEffect(() => {
window.location.assign('https://duckduckgo.com/')
})
return(
<>
</>
)
}
添加回答
舉報