我有一个使用服务的组件。该组件看起来像这样:
@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
课程添加提供者?
问候,我正在尝试使用“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中导入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)
但是,我不想将其导入到我拥有的每个组件中.有没有办法全局注入它,所以我可以在我的所有组件中使用它?
每当导航发生时,我想显示一个微调框。如何全局监听路由器事件,以便NavigationStart
在NavigationEnd
发生路由时可以显示Spinner 并隐藏它,就像我们如何处理HttpInterceptor
全局拦截请求一样。
请给我一些建议。