简体   繁体   English

在Typescript中导入一个简单的Javascript文件

[英]Importing a simple Javascript file in Typescript

I'm using the ionic 2 beta and I try to make a simple calculator for learning. 我正在使用Ionic 2 Beta,并尝试制作一个用于学习的简单计算器。 I want to use the math.js library ( http://mathjs.org ) but I don't know how to import this file to use it in my typescript code. 我想使用math.js库( http://mathjs.org ),但是我不知道如何导入此文件以在我的打字稿代码中使用它。

For example: 例如:

export class HomePage
{
    constructor() {}

    calculate()
    {
        // Here I want to use math.js
    }
}

I tried the following: 我尝试了以下方法:

export class HomePage
{
    var m = require('path/to/math.js');

    constructor() {}

    calculate()
    {
        m.math.parse(expression); // assuming expression is defined
    }
}

But I get the error undefined is not an object (evaluating 'm.math.parse') 但是我得到undefined is not an object (evaluating 'm.math.parse')的错误undefined is not an object (evaluating 'm.math.parse')

您可以在index.html添加脚本,例如

<script src="path/to/math.js"></script>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM