【问题标题】:Getting TypeError: undefined is not an object (evaluating '_mathjs.default.fraction'), React-Native获取 TypeError: undefined is not an object (evalating '_mathjs.default.fraction'), React-Native
【发布时间】:2022-01-26 00:52:47
【问题描述】:

每当我使用“mathjs”时都会收到此错误

TypeError: undefined is not an object (evalating '_mathjs.default.fraction')

基本上我的代码是这样的

import React from 'react';
import {View, Button} from 'react-native';
import mathjs from 'mathjs';
const App = () => {
  return (
    <View>
      <Button
        title="TestButton"
        onPress={() => {
           console.log(mathjs.fraction(1, 2)); // equals to ½
         }}
        />
   </View>
  );
};

export default App;

【问题讨论】:

    标签: node.js reactjs react-native mathjs


    【解决方案1】:

    查看library's npm page,我认为您需要使用命名导入,而不是默认的:

    import { fraction } from 'mathjs';
    

    命名导出不会显示为默认导出的属性。您可以使用此语法来获取所有命名的导出:

    import * as mathjs from 'mathjs;
    

    尽管除非你真正使用它们,否则我不推荐这样做,因为我认为它会破坏你的 javascript 包的 tree-shaking。

    更多信息请参见import docs。

    【讨论】:

    • 我在写这个!该死的!以一种更长的格式,寿。下次不喝水了~非常感谢!
    猜你喜欢
    • 2016-07-30
    • 2020-08-31
    • 2020-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多