简体   繁体   English

Angular2无法使用基本的javascript库

[英]Angular2 not working with basic javascript library

There seems to be an error in angular2-polyfills.js when getting a Javascript library (with its related Typescript typings) working in Angular 2 in the Browser. 在使用浏览器中的Angular 2中的Javascript库(及其相关的Typescript类型)时,angular2-polyfills.js中似乎存在错误。

It's a weird problem: I seem to be able to build and run when there is only 1 line of code from the library, but when I enter a 2nd line, it builds, but doesn't run?!?. 这是一个奇怪的问题:我似乎能够在库中只有一行代码的情况下构建和运行,但是当我进入第二行时,它会构建,但不会运行?!?。

The library is AutobahnJS . 图书馆是AutobahnJS It's a single .js file and the exact same library is designed to work in both the Browser and Node. 它是一个.js文件,完全相同的库设计用于浏览器和节点。 It works perfectly in both the Browser and Node as a normal (non-angular2) javascript library. 它在浏览器和节点中都可以作为普通(非angular2)javascript库完美运行。 I have it working in Node using Typescript and it's related Typings (There is an example test at the Definitely Typed github page) 我使用Typescript在Node中工作,它是相关的类型(在绝对类型的github页面上有一个示例测试

I have followed the basic Angular 2 Quickstart for Typescript and got it working no problems. 我已经遵循基本的Angular 2 Quickstart for Typescript并且没有遇到任何问题。

HERE is the code and output which DOES work. 这是代码和输出工作。 Note the last line in testAutobahn() is commented out: 注意testAutobahn()中的最后一行被注释掉:

import {Component} from 'angular2/core';
import autobahn = require('autobahn');

@Component({
    selector: 'my-app',
    template: `
        <h1>Project: {{_projectName}}</h1>
    `
})
export class AppComponent {

    constructor() {}

    private _projectName : string = "EXAMPLE7";

    testAutobahn() {
        var options: autobahn.IConnectionOptions =
         { url: 'wss://demo.crossbar.io/ws', realm: 'realm1' };

        //var connection = new autobahn.Connection(options);    //  <- THIS line commented out, app runs fine
    }
}

输出工作

HERE is the code and output which does NOT work. 这里的代码和输出不起作用。 Note the last line in testAutobahn() is left in: 注意testAutobahn()中的最后一行留在:

import {Component} from 'angular2/core';
import autobahn = require('autobahn');

@Component({
    selector: 'my-app',
    template: `
        <h1>Project: {{_projectName}}</h1>
    `
})
export class AppComponent {

    constructor() {}

    private _projectName : string = "EXAMPLE7";

    testAutobahn() {
        var options: autobahn.IConnectionOptions =
         { url: 'wss://demo.crossbar.io/ws', realm: 'realm1' };

        var connection = new autobahn.Connection(options);    //  <- THIS line left in, app not run!
    }
}

输出notworking

The only difference is the commenting/uncommenting of that line. 唯一的区别是该行的评论/取消注释。 The error seems to come from angular2-polyfills.js and system.src.js 错误似乎来自angular2-polyfills.js和system.src.js

I have used 'tsd' to install all the typings. 我用'tsd'来安装所有的打字机。 They all look correct and my editor Atom has intellisense on the autobahn.* types just fine. 他们看起来都很正确,我的编辑Atom在高速公路上有智能感。*类型很好。

Possible issues?: 可能的问题?:

  1. Using AutobahnJS v0.9.9 but the Typings seem to be for v0.9.6 使用AutobahnJS v0.9.9但类型似乎是v0.9.6

     -> I can't see this being a real problem 
  2. When building, Typescript emits the Javascript code, but it does give an error: 在构建时,Typescript会发出Javascript代码,但确实会出错:

"Subsequent variable declarations must have the same type. Variable '$' must be of type 'cssSelectorHelper', but here has type 'JQueryStatic'." “后续变量声明必须具有相同的类型。变量'$'必须是'cssSelectorHelper'类型,但这里的类型为'JQueryStatic'。”

        -> I have resolved this error by simply commenting out the line   /* declare var $: JQueryStatic; */   in the jquery.d.ts file (I'm not using JQuery anyway) 

Using Typescript v1.7.5, Angular 2 Beta 0 with the package.json, tsconfig.json and boot.ts files as per the Angular 2 Quickstart for Typescript. 使用Typescript v1.7.5,Angular 2 Beta 0与package.json,tsconfig.json和boot.ts文件按照Angular 2 Quickstart for Typescript。 My end goal is to get an Angular2 Service working with AutobahnJS (but just at the baby steps at the moment). 我的最终目标是让Angular2服务与AutobahnJS合作(但目前只是在婴儿的步骤)。

Any help would be greatly appreciated.... 任何帮助将不胜感激....

Well I have eventually got this working now and made some progress towards an AutobahnJS and Angular 2 Service. 好吧,我最终得到了这个工作,并在AutobahnJS和Angular 2服务方面取得了一些进展。 You may refer to my plunker: 你可以参考我的plunker:

[plunker](https://plnkr.co/edit/Dgipr76Rbhgh31PH4pmM)

however I'm still quite stuck. 但是我仍然很困难。 The problem is I can't simply 'call' a function (where this function will then go-ahead and execute the Autobahn session.call(...) method) which returns a value. 问题是我不能简单地“调用”一个函数(此函数将继续执行并执行Autobahn session.call(...)方法),该函数返回一个值。 For example, I'm wanting to do something simple like: 例如,我想做一些简单的事情:

var x = myMessageService.DoFunct1( someParameter );

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

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