标签: angular2-providers

如何在Angular 2中将提供程序添加到Service类?

我有一个使用服务的组件。该组件看起来像这样:

@Component({
    moduleId: module.id,
    selector: 'test',
    providers: [HTTP_PROVIDERS, TestService]
})
export class TestComponent implements OnInit {

    constructor(private _testService:TestService) {
    }
Run Code Online (Sandbox Code Playgroud)

如您所见,我HTTP_PROVIDERS在组件中添加了提供程序。之所以可行,是因为DI现在知道这些http类。但是,TestService真正使用Http该类的是我,而不是我的TestComponent

@Injectable()
export class TestService {

    constructor(private _http:Http) {
    }
Run Code Online (Sandbox Code Playgroud)

我觉得由于这是使用Http类的服务,因此应该是包含提供程序本身的服务。在TestComponent不知道什么供应商TestService将需要。

由于服务类没有该组件装饰器,因此我不确定如何实际向其添加提供程序。如何向Service课程添加提供者?

javascript angular2-services angular2-providers angular

5
推荐指数
1
解决办法
2732
查看次数

NullInjectorError: 没有 t 的提供者

问候,我正在尝试使用“ng build --prod --configuration=production”构建我的 angular2 项目,将其部署在 ubuntu 服务器上,但在 Web 控制台中出现此错误:

ERROR Error: "StaticInjectorError[t -> t]: 
  StaticInjectorError(Platform: core)[t -> t]: 
    NullInjectorError: No provider for t!"
    get http://192.168.1.168/main.765684076005ff28e8f4.js:1
    t http://192.168.1.168/main.765684076005ff28e8f4.js:1
    t http://192.168.1.168/main.765684076005ff28e8f4.js:1
    get http://192.168.1.168/main.765684076005ff28e8f4.js:1
    t http://192.168.1.168/main.765684076005ff28e8f4.js:1
    t http://192.168.1.168/main.765684076005ff28e8f4.js:1
    get http://192.168.1.168/main.765684076005ff28e8f4.js:1
    cm http://192.168.1.168/main.765684076005ff28e8f4.js:1
    n http://192.168.1.168/main.765684076005ff28e8f4.js:1
    fm http://192.168.1.168/main.765684076005ff28e8f4.js:1
    cm http://192.168.1.168/main.765684076005ff28e8f4.js:1
    get http://192.168.1.168/main.765684076005ff28e8f4.js:1
    tg http://192.168.1.168/main.765684076005ff28e8f4.js:1
    Jm http://192.168.1.168/main.765684076005ff28e8f4.js:1
    Qm http://192.168.1.168/main.765684076005ff28e8f4.js:1
    kg http://192.168.1.168/main.765684076005ff28e8f4.js:1
    Og http://192.168.1.168/main.765684076005ff28e8f4.js:1
    Gg http://192.168.1.168/main.765684076005ff28e8f4.js:1
    create http://192.168.1.168/main.765684076005ff28e8f4.js:1
    create http://192.168.1.168/main.765684076005ff28e8f4.js:1
    bootstrap http://192.168.1.168/main.765684076005ff28e8f4.js:1
    _moduleDoBootstrap http://192.168.1.168/main.765684076005ff28e8f4.js:1
    _moduleDoBootstrap http://192.168.1.168/main.765684076005ff28e8f4.js:1
    i http://192.168.1.168/main.765684076005ff28e8f4.js:1
    invoke http://192.168.1.168/polyfills.71466010da316f5320a5.js:1
    onInvoke http://192.168.1.168/main.765684076005ff28e8f4.js:1
    invoke http://192.168.1.168/polyfills.71466010da316f5320a5.js:1
    run http://192.168.1.168/polyfills.71466010da316f5320a5.js:1 …
Run Code Online (Sandbox Code Playgroud)

angular2-providers angular

5
推荐指数
1
解决办法
4539
查看次数

如何在angular2全局导入Javascript库

我正在尝试在angular2中导入moment.js库.我发现以下解决方案:

import {Component} from 'angular2/core';
import * as moment from 'moment';

@Component({
  selector: 'app',
  template: require('./app.component.html')
})
export class AppComponent {
  moment:any = moment;
  constructor() {}
}
Run Code Online (Sandbox Code Playgroud)

但是,我不想将其导入到我拥有的每个组件中.有没有办法全局注入它,所以我可以在我的所有组件中使用它?

javascript dependency-injection angular2-providers angular

1
推荐指数
2
解决办法
1327
查看次数

Angular:如何全局监听路由器事件

每当导航发生时,我想显示一个微调框。如何全局监听路由器事件,以便NavigationStartNavigationEnd发生路由时可以显示Spinner 并隐藏它,就像我们如何处理HttpInterceptor全局拦截请求一样。

请给我一些建议。

angular2-routing angular2-providers angular

1
推荐指数
1
解决办法
863
查看次数