NG2 RC5: HTTP_PROVIDERS is deprecated

前端 未结 2 501
囚心锁ツ
囚心锁ツ 2020-12-16 16:26

So, in version RC5 of Angular2, they deprecated the HTTP_PROVIDERS and introduced the HttpModule. For my application code, this is working fine, b

2条回答
  •  遥遥无期
    2020-12-16 16:36

    I run into a similar problem when updating from pre-RC5 code to RC6. To expand on Joe W's answer above, I replaced this code:

    import { ReflectiveInjector, provide } from '@angular/core';
    import { HTTP_PROVIDERS, RequestOptions } from '@angular/http';
    
    export function main() {
      describe('My Test', () => {
        let myService: MyService;
    
        beforeAll(() => {
          let injector = ReflectiveInjector.resolveAndCreate([
            HTTP_PROVIDERS,
            provide(RequestOptions, { useValue: getRequestOptions() }),
            MyService
          ]);
          myService = injector.get(MyService);
        });
    
        it('should be instantiated by the injector', () => {
          expect(myService).toBeDefined();
        });
    ...
    

    with this RC6 code (which, I guess, should also work for RC5):

    import { TestBed } from '@angular/core/testing';
    import { HttpModule, RequestOptions } from '@angular/http';
    
    export function main() {
      describe('My Test', () => {
        let myService: MyService;
    
        beforeAll(() => {
          TestBed.configureTestingModule({
            imports: [HttpModule],
            providers: [
              { provide: RequestOptions, useValue: getRequestOptions() },
              MyService
            ]
          });
          myService = TestBed.get(MyService);
        });
    
        it('should be instantiated by the testbed', () => {
          expect(myService).toBeDefined();
        });
    ...
    

提交回复
热议问题