首页 > 解决方案 > 如何在下一个js中设置默认页面?

问题描述

我是新手next.js,在next.js默认页面中index.js,我想将其路径更改为login,有人可以帮我怎么做吗?现在我正在使用Router.push('/login'); ,但它正在产生闪烁问题,有人可以帮忙解决这个问题吗?

import Head from 'next/head'
import styles from '../styles/Home.module.css'
import Router from 'next/router'
import React, { useEffect } from "react";
import { Redirect } from 'react-router';


export default function Home(props) {
  useEffect(() => {
    const { pathname } = Router
    if (pathname == '/') {
      Router.push('/login');
    }
  }, [props]);

  return ''
}

标签: reactjsnext.js

解决方案


您可以像这样添加redirects到您的next.config.js

module.exports = {
  async redirects() {
    return [
      {
        source: '/about',
        destination: '/',
        permanent: true,
      },
    ]
  },
}

推荐阅读