【问题标题】:how to set default page in next js?如何在下一个js中设置默认页面?
【发布时间】:2020-12-01 01:22:56
【问题描述】:

我是next.js 的新手,在index.js 的默认页面next.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 ''
}

【问题讨论】:

标签: reactjs next.js


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:

    从服务器端重定向;

    export const getServerSideProps = async ({ res }) => {
        res.setHeader("location", `/kiosk/welcome`);
    
        res.statusCode = 302;
        res.end();
    
        return { props: {} };
    };
    
    const Index = () => <>Index</>;
    
    export default Index;
    
    

    【讨论】:

      猜你喜欢
      • 2014-12-06
      • 2010-12-27
      • 1970-01-01
      • 2018-06-14
      • 2018-08-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-09-20
      相关资源
      最近更新 更多