簡體   English   中英

嘗試導入錯誤,無法編譯反應應用程序

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

我收到以下錯誤,如果有人能指出我正確的方向,我會很高興的 :) 我嘗試了不同的方法,但沒有任何效果。

無法編譯 ./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;

我有同樣的問題,我解決了如下:

我做了一個改變:

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

在哪里更改evalevaluate

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

現在它完美地工作。 我希望這對你有很大幫助

嘗試導入為默認導入,然后使用它

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

導入為評估

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

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

我遇到了完全相同的問題,和你一樣,我做了很多來解決它,但都失敗了。 我最終是如何解決它的:首先確保您已經使用以下命令從命令行下載了 mathjs: npm install mathjs; 並且在你的反應代碼的頂部,放置語句: import * as math from 'mathjs'; (后者我可以從您的代碼中看到您所做的)。

現在關鍵的第二步是,不要使用 math.eval(expr) 或 math.evaluate(expr) 來評估您的表達式,而是使用 parse, compile 與evaluate結合使用,如下例所示:

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

這是我在使用相同的錯誤和方法多次失敗后最終嘗試的方式,但它奏效了。 這是評估 mathjs 庫中列出的表達式的另一種方法。 我知道自從提出這個問題已經有幾個月了,但是這個解決方案將來仍然可以幫助某人。

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

參考這個https://www.npmjs.com/package/mathjs

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM