Next Js Router.push不是函数错误

时间:2019-03-03 12:02:33

标签: reactjs next.js

当我尝试使用Router.push()重定向时,出现以下错误:

TypeError: next_router__WEBPACK_IMPORTED_MODULE_3__.Router.push is not a function

我正在尝试从create-react-app迁移到下一个js。

const redirectUser = () => {
        if (true) {
            Router.push('/');
        }
    };

4 个答案:

答案 0 :(得分:0)

我必须这样导入:

// works
import Router from "next/router";
// dont
import { Router } from "next/router";

答案 1 :(得分:0)

路由器模块仅在客户端可用

答案 2 :(得分:0)

使用next.js时必须考虑到重定向应该采用const MyComponent = ()=>{ return <tag> {/* ... */} </tag> } MyComponent.getInitialProps = ({res}) => { if (res) { /* serve-side */ res.writeHead(302, { Location: 'http://example.com' }) res.end() } else { /* client-side */ Router.push('http://example.com') } return {} } 方法,以避免不必要的渲染组件。

例如

    PARTITION_RETURN_STRING=""
PARTITION_TEMP_STRING=""
for i in $(df | awk '{ print $6 }')
do
        if [ "$i" != "Mounted" ]; then
                for abc in $(seq 1 6)
                do
                        PARTITION_TEMP_STRING=$(df -m | awk -v bla=$i '$6 == bla' | tr -s ' ' | cut -d ' ' -f $abc)
                        PARTITION_RETURN_STRING="$PARTITION_RETURN_STRING$PARTITION_TEMP_STRING"
                        if [ "$(($abc%6))" = "0" ]; then
                                PARTITION_RETURN_STRING+="|"
                        else
                                PARTITION_RETURN_STRING+="#"
                        fi
                done
        fi
done

答案 3 :(得分:0)

从 next/router 导入 Router 时不要加大括号

使用这个:

import Router from "next/router";