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

我收到以下错误,如果有人能指出我正确的方向,我会很高兴:) 我尝试了不同的方法,但没有任何效果。


无法编译 ./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>

        </div>

      </div>

    );

  }

}

export default App;


holdtom
浏览 124回答 3
3回答

斯蒂芬大帝

我有同样的问题,我解决了如下:我做了一个改变:&nbsp; handleEqual = () => {&nbsp; &nbsp; this.setState({ input: math.eval(this.state.input) });&nbsp; };eval在哪里更改evaluate&nbsp; handleEqual = () => {&nbsp; &nbsp; this.setState({ input: math.evaluate(this.state.input) });&nbsp; };现在它完美地工作了。我希望这对你有很大帮助

智慧大石

导入为评估import * as math from 'mathjs';console.log(math.evaluate('2 + 3'))// Orimport {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 库中列出的评估表达式的另一种方法。我知道这个问题被问到已经有几个月了,但这个解决方案在未来仍然可以帮助某人。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript