简体   繁体   English

尝试导入错误,无法编译反应应用程序

[英]Attempted import error, failed to compile react app

I am getting the following error and would love it if someone could point me in the right direction :) I've tried different ways but nothing works.我收到以下错误,如果有人能指出我正确的方向,我会很高兴的 :) 我尝试了不同的方法,但没有任何效果。

Failed to compile ./src/App.jsx Attempted import error: 'eval' is not exported from 'mathjs' (imported as 'math').无法编译 ./src/App.jsx 尝试导入错误:'eval' 未从 'mathjs' 导出(导入为 'math')。

 import React, { Component } from 'react'; import './App.css'; import { Button } from './components/Button'; import { Input } from './components/Input'; import { ClearButton } from './components/ClearButton'; import * as math from 'mathjs'; class App extends Component { constructor(props) { super(props); this.state = { input: "" }; } addToInput = val => { this.setState({input: this.state.input + val}); } handleEqual = () => { this.setState({input: math.eval(this.state.input)}); } render() { return ( <div className="app"> <div className="calc-wrapper"> <Input input={this.state.input}></Input> <div className="row"> <Button handleClick={this.addToInput}>7</Button> <Button handleClick={this.addToInput}>8</Button> <Button handleClick={this.addToInput}>9</Button> <Button handleClick={this.addToInput}>/</Button> </div> <div className="row"> <Button handleClick={this.addToInput}>4</Button> <Button handleClick={this.addToInput}>5</Button> <Button handleClick={this.addToInput}>6</Button> <Button handleClick={this.addToInput}>X</Button> </div> <div className="row"> <Button handleClick={this.addToInput}>1</Button> <Button handleClick={this.addToInput}>2</Button> <Button handleClick={this.addToInput}>3</Button> <Button handleClick={this.addToInput}>+</Button> </div> <div className="row"> <Button handleClick={this.addToInput}>.</Button> <Button handleClick={this.addToInput}>0</Button> <Button handleClick={() => this.handleEqual()}>=</Button> <Button handleClick={this.addToInput}>-</Button> </div> <div className="row"> <ClearButton handleClear={ () => this.setState({input: "" })}>Clear</ClearButton> </div> </div> </div> ); } } export default App;

I had this same problem and I solved it as follows:我有同样的问题,我解决了如下:

I made a change:我做了一个改变:

  handleEqual = () => {
    this.setState({ input: math.eval(this.state.input) });
  };

where change eval to evaluate在哪里更改evalevaluate

  handleEqual = () => {
    this.setState({ input: math.evaluate(this.state.input) });
  };

Now it works perfectly.现在它完美地工作。 I hope this has been of great help to you我希望这对你有很大帮助

Try importing as a default import and then use it尝试导入为默认导入,然后使用它

import { evaluate } from 'mathjs'; 
console.log(evaluate('2 + 2'));

Import as evaluate导入为评估

import * as math from 'mathjs';
console.log(math.evaluate('2 + 3'))

// Or
import {evaluate } from 'mathjs';
console.log(evaluate('2 + 3'))

I had the exact same problem and like you I did a lot to solve it but all failed.我遇到了完全相同的问题,和你一样,我做了很多来解决它,但都失败了。 How I eventually solved it was as follows: first make sure you 've downloaded mathjs from the commandline using: npm install mathjs;我最终是如何解决它的:首先确保您已经使用以下命令从命令行下载了 mathjs: npm install mathjs; and also at the top of your react code, place the statement: import * as math from 'mathjs';并且在你的反应代码的顶部,放置语句: import * as math from 'mathjs'; (the latter which I can see you did from your code). (后者我可以从您的代码中看到您所做的)。

Now the crucial second step, is that instead of using math.eval(expr), or math.evaluate(expr) to evaluate your expression, use parse, compile in conjunction with evaluate as in the below example:现在关键的第二步是,不要使用 math.eval(expr) 或 math.evaluate(expr) 来评估您的表达式,而是使用 parse, compile 与evaluate结合使用,如下例所示:

const node1= math.parse('2 + 3');
const code1= node1.compile();
code1.evaluate(); // 5

This was the way i finally tried after much failed effort with this same error and approach, but it worked.这是我在使用相同的错误和方法多次失败后最终尝试的方式,但它奏效了。 it's the alternative way of evaluating expressions as listed in the mathjs library.这是评估 mathjs 库中列出的表达式的另一种方法。 I understand that it's been some months since this question was asked, but this solution could still help someone in the future.我知道自从提出这个问题已经有几个月了,但是这个解决方案将来仍然可以帮助某人。

Use math.evaluate() instead of math.eval().使用 math.evaluate() 而不是 math.eval()。

Refer this https://www.npmjs.com/package/mathjs参考这个https://www.npmjs.com/package/mathjs

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 编译失败。/src/Content.js 尝试导入错误:“重定向”未从“react-router-dom”导出 - Failed to compile ./src/Content.js Attempted import error: 'Redirect' is not exported from 'react-router-dom' 在反应应用程序中尝试导入错误,但路径和一切都是正确的 - Attempted import error in react app but the path and everything is correct 如何修复我的 React 应用程序中尝试导入的错误? - How can I fix attempted import error in my React App? 尝试导入错误:当编译react-native到react-native-web时,不会从&#39;./apiFetcher&#39;导出&#39;ApiGet&#39; - Attempted import error: 'ApiGet' is not exported from './apiFetcher' when compile react-native to react-native-web 尝试导入错误:“App”未从“./App”导出 - Attempted import error: 'App' is not exported from './App' 如何修复“尝试导入错误 React”? - How to fix "attempted import error React"? 我在将找到的现有 React 组件集成到我的应用程序中时遇到问题。 “尝试导入错误:” - I am having an issue with integrating an existing react component I found, into my app. "Attempted import error:" 反应尝试导入 - React Attempted import 错误:尝试导入节点标准库模块“crypto”。 它失败了 - Error: attempted to import the Node standard library module “crypto”. It failed 尝试导入错误:“getMoviesList”未从“./actions”导出; 反应错误 redux - Attempted import error: 'getMoviesList' is not exported from './actions'; error in react redux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM