我在 React 上的项目中使用 mathjs,我的目标是创建字符串数学函数 'f(X)' 并且每次我给出一个 X 的数量并收到函数的结果但它不起作用,这是编码
import "./styles.css";
import maths from "mathjs";
export default function App() {
const parser = maths.parser();
return (
<div className="App">
<h1>{parser.evaluate("f(x)=12x")}</h1>
<h2>{parser.evaluate("f(1)")}</h2>
</div>
);
}
我收到了那条信息
TypeError
Cannot read properties of undefined (reading 'parser')
App
/src/App.js:4:23
1 | import "./styles.css";
2 | import maths from "mathjs";
3 | export default function App() {
> 4 | const parser = maths.parser();
| ^
5 | return (
6 | <div className="App">
7 | <h1>{parser.evaluate("f(x)=12x")}</h1>
你可以给我其他的想法或建议,我会很高兴的
新编辑 我也试过这个
import "./styles.css";
import { evaluate } from "mathjs";
export default function App() {
return (
<div className="App">
<h1>{evaluate("f(x) = (sin(x) + cos(x/2)) * 5")}</h1>
<h2>{evaluate("f(1)")}</h2>
</div>
);
}
我收到了
Error
Undefined function f
▶ 6 stack frames were collapsed.
App
/src/App.js:7:20
4 | return (
5 | <div className="App">
6 | <h1>{evaluate("f(x) = (sin(x) + cos(x/2)) * 5")}</h1>
> 7 | <h2>{evaluate("f(1)")}</h2>
| ^
8 | </div>
9 | );
10 | }