MAC M1尝试导入错误:'交换机'未从'Reaction-Router'导出 [英] Mac M1 Attempted import error: 'Switch' is not exported from 'react-router'

查看:29
本文介绍了MAC M1尝试导入错误:'交换机'未从'Reaction-Router'导出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Windows上npm run start工作正常,但是M1上的npm run start显示错误。
您为什么要这样做?

Failed to compile
./node_modules/react-router-config/esm/react-router-config.js
Attempted import error: 'Switch' is not exported from 'react-router'.
This error occurred during the build time and cannot be dismissed.

"react-router-dom": "^5.3.0", "react-scripts": "^4.0.1",

这是app.js代码。

import React from 'react';
import { BrowserRouter } from 'react-router-dom';
import routes from "./Routes";
import {renderRoutes} from "react-router-config";


import theme from './theme';
import {ThemeProvider} from "styled-components";

export default function App() {
    return (
                    <BrowserRouter>
                        {renderRoutes(routes)}
                    </BrowserRouter>
    );
};
``

推荐答案

如果您意外地将react-router-dom更新到版本6,它将不再导出Switch组件。已替换为必须必须直接包装/呈现Route组件的Routes组件。

  1. Switch替换为Routes组件。

    import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
    ...
    
    function App() {
      return (
        <Router>
          <Routes>
            <Route path="/" element={<Home/>} />
          </Routes>
        </Router>
      );
    }
    

    按照Upgrading from v5跨应用程序将项目从v5迁移到v6,因为有相当多的组件API更改是破坏性的。

  2. 恢复到react-router-domv5。

    • 运行npm un -s react-router-dom
    • 运行npm i -s react-router-dom@5.3.0

这篇关于MAC M1尝试导入错误:&#39;交换机&#39;未从&#39;Reaction-Router&#39;导出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆