Angular2无法使用基本的javascript库

前端之家收集整理的这篇文章主要介绍了Angular2无法使用基本的javascript库前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在使用浏览器中的Angular 2中的 Javascript库(及其相关的Typescript类型)时,angular2-polyfills.js中似乎存在错误.

这是一个奇怪的问题:我似乎能够在库中只有一行代码的情况下构建和运行,但是当我进入第二行时,它会构建,但不会运行?!?.

该库是AutobahnJS.它是一个.js文件,完全相同的库设计用于浏览器和节点.
它在浏览器和节点中都可以作为普通(非angular2)javascript库完美运行.
我使用Typescript在Node中工作,它是相关的类型(在绝对类型的github页面上有一个example test)

我已经遵循了基本的Angular 2 Quickstart for Typescript并且没有遇到任何问题.

这是代码输出工作.注意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
    }
}

这里的代码输出不起作用.注意testAutobahn()中的最后一行留在:

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

@Component({
    selector: 'my-app',realm: 'realm1' };

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

唯一的区别是该行的评论/取消注释.
错误似乎来自angular2-polyfills.js和system.src.js

我用’tsd’来安装所有的打字机.它们看起来都是正确的,我的编辑器Atom在高速公路上有智能感.*类型就好了.

可能的问题?:

>使用AutobahnJS v0.9.9,但打字似乎适用于v0.9.6

-> I can't see this being a real problem

>构建时,Typescript会发出Javascript代码,但确实会出错:

“Subsequent variable declarations must have the same type. Variable
‘$’ must be of type ‘cssSelectorHelper’,but here has type
‘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)

使用Typescript v1.7.5,Angular 2 Beta 0和package.json,tsconfig.json和boot.ts文件,按照Angular 2 Quickstart for Typescript.
我的最终目标是让Angular2服务与AutobahnJS合作(但目前只是在婴儿的步骤).

任何帮助将不胜感激….

解决方法

好吧,我最终得到了这个工作,并在AutobahnJS和Angular 2服务方面取得了一些进展.你可以参考我的plunker:

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

但是我仍然很困难.问题是我不能简单地“调用”一个函数(此函数将继续执行并执行Autobahn session.call(…)方法),该函数返回一个值.例如,我想做一些简单的事情:

var x = myMessageService.DoFunct1( someParameter );

猜你在找的Angularjs相关文章