最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

angular - Importing a JavaScript function in TypeScript and AngularJS 2 - Stack Overflow

programmeradmin3浏览0评论

I have a plugin that works with JavaScript. I want to use the function inside of my TypeScript ponent in AngularJS2. I have used different kinds of ways with no success.

@angular/cli: 1.0.0-rc.0, node: 7.7.2, @angular/core: 2.4.9

Let's assume I want to use the following function cube.js:

function cube(x) {
   return x * x * x;
}
const foo = Math.PI + Math.SQRT2;
export default { cube, foo };

Using Type Definition approach, I create cube.d.ts as follows:

interface Cube {
   cube(x:number):number;
} 
export default Cube;

In my ponent, I have imported the function as follows:

import Cube from './cube';
...
public myCube:Cube;
console.log(myCube.cube(3));

But it gives me the following error:

Error in :0:0 caused by: Cannot read property 'cube' of undefined TypeError: Cannot read property 'cube' of undefined at HComponent.webpackJsonp

in tsconfig.json, allowJS is true (edited: "allowJs": true). @type module has already been installed. Still Can not read a simple JS function.

Any idea?

I have a plugin that works with JavaScript. I want to use the function inside of my TypeScript ponent in AngularJS2. I have used different kinds of ways with no success.

@angular/cli: 1.0.0-rc.0, node: 7.7.2, @angular/core: 2.4.9

Let's assume I want to use the following function cube.js:

function cube(x) {
   return x * x * x;
}
const foo = Math.PI + Math.SQRT2;
export default { cube, foo };

Using Type Definition approach, I create cube.d.ts as follows:

interface Cube {
   cube(x:number):number;
} 
export default Cube;

In my ponent, I have imported the function as follows:

import Cube from './cube';
...
public myCube:Cube;
console.log(myCube.cube(3));

But it gives me the following error:

Error in :0:0 caused by: Cannot read property 'cube' of undefined TypeError: Cannot read property 'cube' of undefined at HComponent.webpackJsonp

in tsconfig.json, allowJS is true (edited: "allowJs": true). @type module has already been installed. Still Can not read a simple JS function.

Any idea?

Share Improve this question edited Mar 20, 2017 at 15:39 Enayat asked Mar 20, 2017 at 15:00 EnayatEnayat 4,0621 gold badge34 silver badges50 bronze badges 2
  • Try instancing it constructor(private myCube: Cube) {} in the constructor – SrAxi Commented Mar 20, 2017 at 15:02
  • Incuding in constructor did not work either – Enayat Commented Mar 20, 2017 at 15:27
Add a ment  | 

2 Answers 2

Reset to default 3

I found the answer.

My cube.js file:

"use strict"; 
class Cube {
    cube(x) {
     return x * x * x;
   }
}
export default Cube;

My cube.d.ts file

export default class Cube {
  cube(x: number): number;
}

In the ts ponent, I have:

/// <reference path="./cube.d.ts" />
import Cube from './cube';
...
        const my = new Cube();
        console.log(my.cube(10));

This works for me

import model = require('../../model');

And in my ponent

model.functionName();
发布评论

评论列表(0)

  1. 暂无评论