简体   繁体   English

如何在节点脚本中重用Angular模块

[英]How to reuse Angular modules in node scripts

I want to reuse Angular2's @angular/http module from a node script. 我想从节点脚本中重用Angular2的@angular/http模块。 I'm using node v4.6.0, @angular/http version 2.1.2 from where npm gets it. 我正在使用npm从中获取节点v4.6.0, @angular/http版本2.1.2。

In this specific case I want to do this so I can easily isolate the module, confirm it's behavior, and play around with it (I mean I should be able to right - that is why it's called a module..). 在这种特定情况下,我想这样做,这样我就可以轻松隔离模块,确认其行为并进行处理(我是说我应该能够纠正-这就是为什么它被称为模块。)。 But I'm also after general advice on reusing Angular modules that don't have inherent browser dependencies in node. 但是我也有关于重新使用Angular模块的一般建议,这些模块在node中没有固有的浏览器依赖性。

Going off how the web application I'm looking at uses the module I tried this: 我正在研究的Web应用程序如何使用我尝试过的模块:

myUrl = '...'
http = require('@angular/http')
Http = new http.Http()
Http.get(myUrl).then(result => console.log(result))

And got: 并得到:

TypeError: Cannot read property 'merge' of undefined at mergeOptions (/home/sam/node_modules/@angular/http/bundles/http.umd.js:1578:30) at Http.get (/home/sam/node_modules/@angular/http/bundles/http.umd.js:1672:45) at repl:1:6 at REPLServer.defaultEval (repl.js:262:27) at bound (domain.js:287:14) at REPLServer.runBound [as eval] (domain.js:300:12) at REPLServer. TypeError:无法读取Http.get(/ home / sam / node_modules / @)处mergeOptions(/home/sam/node_modules/@angular/http/bundles/http.umd.js:1578:30)上未定义的属性“合并”在REPLServer.runBound的绑定(domain.js:287:14)的REPLServer.defaultEval(repl.js:262:27)的repl:1:6处的angular / http / bundles / http.umd.js:1672:45) [按评估](domain.js:300:12)在REPLServer。 (repl.js:431:12) at emitOne (events.js:82:20) at REPLServer.emit (events.js:169:7) at REPLServer.Interface._onLine (readline.js:212:10) (repl.js:431:12)位于REPLServer.emit(events.js:169:7)处的emitOne(events.js:82:20)(REPLServer.Interface._onLine(readline.js:212:10))

So, is it done? 那么,完成了吗? Can it be done? 能做到吗 How to go about it in the specific case of Http and in general? 在Http的特定情况下以及一般情况下如何处理?

After some (a lot) more learning about NG2 (Angular2), I realized what seems obvious once you learn it: That NgModules are not merely simple containers for stuff like the term "module" implies. 在对NG2(Angular2)进行了一些(大量)学习之后,我意识到,一旦学习了NgModules ,它就会变得显而易见: NgModules不仅仅是像“模块”一词所暗示的简单容器。 They have very specific responsibilities and are tightly coupled to the NG2 core. 它们负有非常具体的责任,并且与NG2核心紧密耦合。 So you can't just go about importing them into node. 因此,您不能只是将它们导入节点。 You need a bootstrapped NG2 core module to do just about anything with anything in NG2. 您需要一个自举的 NG2核心模块才能对NG2中的任何内容进行任何处理。 Accessing Http which is an injectable service provided by the HttpModule is more complicated again, due to the injection system. 由于注入系统,访问HttpModule提供的可注入服务Http再次变得更加复杂。

I was able to get somewhat isolated access to NG2's Http service using NG2's karma testing framework and utilities. 我能够使用NG2的业力测试框架和实用程序对NG2的Http服务进行某种隔离的访问。 The entire test setup is hideously complicated, but it's all done for you in the QuickStart application (the test setup essentially gives you a bootstrapped NgModule, in a server environment that you can run tests with). 整个测试设置非常复杂,但是在QuickStart应用程序中已经为您完成了所有工作(测试设置实际上是在可用于运行测试的服务器环境中为您提供自举的NgModule)。 I stripped the quickstart app back and added a simple unit test like this to it then ran npm test : 我剥离了快速入门应用程序,并向其中添加了一个简单的单元测试,然后运行npm test

import { async, ComponentFixture, TestBed } from '@angular/core/testing';
import { DebugElement } from '@angular/core';
import { HttpModule, Headers, Http, Response } from '@angular/http';
import { Observable }     from 'rxjs';
import 'rxjs/add/operator/toPromise';

describe('Test frame for accessing Http!', function () {
  let http: Http

  beforeEach(async(() => {
   TestBed.configureTestingModule({
      imports: [ HttpModule ]
    })
    .compileComponents();
  }));

  beforeEach(() => {
      http = TestBed.get(Http);
  });

  it('Test some HTTP', () => {
    http.get('/base/foo.json').toPromise()
      .then((r: Response) => console.warn(r))
      .catch((e: any) => console.warn('An error occured'))
  });
});

The server backing this is karma, so you may need to tweak it's configuration to tell it to serve whatever from where ever.. 支持此功能的服务器是业力,因此您可能需要调整其配置,以使其能够从任何地方提供服务。

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

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