【问题标题】:Angular2 not working with basic javascript libraryAngular2 不能使用基本的 JavaScript 库
【发布时间】:2016-04-19 05:49:08
【问题描述】:

当在浏览器中的 Angular 2 中获取 Javascript 库(及其相关的 Typescript 类型)时,angular2-polyfills.js 中似乎存在错误。

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

图书馆是AutobahnJS。它是一个单独的 .js 文件,并且完全相同的库旨在同时在浏览器和节点中工作。 它在浏览器和节点中都可以作为普通(非 angular2)javascript 库完美运行。 我让它在 Node 中使用 Typescript 和它相关的 Typings 工作(有一个 example test 在绝对类型的 github 页面)

我遵循了 Typescript 的基本 Angular 2 Quickstart 并让它正常工作。

这里是可以工作的代码和输出。注意 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',
    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!
    }
}

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

我已经使用 'tsd' 来安装所有类型。它们看起来都正确,我的编辑 Atom 在高速公路上有智能感知。* 类型很好。

可能的问题?:

  1. 使用 AutobahnJS v0.9.9,但 Typings 似乎适用于 v0.9.6

    -> I can't see this being a real problem
    
  2. 在构建时,Typescript 会发出 Javascript 代码,但确实会报错:

"后续的变量声明必须具有相同的类型。变量 '$' 必须是 '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) 

使用 Typescript v1.7.5、Angular 2 Beta 0 和 package.json、tsconfig.json 和 boot.ts 文件,根据 Typescript 的 Angular 2 Quickstart。 我的最终目标是让 Angular2 服务与 AutobahnJS 一起工作(但目前还只是起步阶段)。

任何帮助将不胜感激......

【问题讨论】:

  • 已经有很多关于这个意外令牌的问题。它们似乎都与进口有关。我不知道TS。我只记得看到他们回答了。
  • 这条线有没有给你一些东西? console.log( autobahn.Connection ); 还有,注释掉 cssSelectorHelper 声明而不是 JQueryStatic 一个,jquery 定义有很多东西,你会在 angular2 安装的两个量角器文件中找到 cssSelectorHelper

标签: javascript angular autobahn


【解决方案1】:

好吧,我现在终于完成了这项工作,并在 AutobahnJS 和 Angular 2 服务方面取得了一些进展。你可以参考我的plunker:

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

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

var x = myMessageService.DoFunct1( someParameter );

【讨论】:

  • 在此期间,我会看看Wampy,看看我有没有运气。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-17
  • 1970-01-01
  • 2013-09-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多