Service 无法从Angular 2中的组件中的服务获取数据
我使用的是Angular 2.0.0-beta.15。我试图从组件中的服务获取数据。下面是组件中的一段代码Service 无法从Angular 2中的组件中的服务获取数据,service,angular,promise,components,provider,Service,Angular,Promise,Components,Provider,我使用的是Angular 2.0.0-beta.15。我试图从组件中的服务获取数据。下面是组件中的一段代码 import {Component} from 'angular2/core'; import {UserService} from '../services/user.service'; @Component({ selector: 'parent', templateUrl: './views/parentUser.html', providers: [UserServic
import {Component} from 'angular2/core';
import {UserService} from '../services/user.service';
@Component({
selector: 'parent',
templateUrl: './views/parentUser.html',
providers: [UserService]
})
export class ParentUserComponent {
constructor(private _userService:UserService) {
this._userService.getCurrentUser().subscribe(data => console.log(data));
}
}
现在我的UserService代码是
import {Injectable} from 'angular2/core';
import {Http, Response} from 'angular2/http';
import {Observable} from 'rxjs/Observable';
@Injectable()
export class UserService {
private currentUser = {
username: '',
role: '',
targetURL: ''
};
setCurrentUser(currentUser) {
this.currentUser.username = currentUser.username;
this.currentUser.role = currentUser.role;
this.currentUser.targetURL = currentUser.targetURL;
console.log('current User:' + JSON.stringify(this.currentUser));
}
getCurrentUser() {
return Observable.of(this.currentUser);
}
}
我的boots.ts如下:
///<reference path="../../node_modules/angular2/typings/browser.d.ts"/>
import {bootstrap} from 'angular2/platform/browser';
import {provide} from 'angular2/core';
import {ROUTER_PROVIDERS, ROUTER_DIRECTIVES, RouteConfig, Location, LocationStrategy, HashLocationStrategy} from 'angular2/router';
import {AppComponent} from './app.component';
import {HTTP_PROVIDERS} from 'angular2/http';
import {UserService} from './services/user.service';
import 'rxjs/Rx';
bootstrap(AppComponent, [ROUTER_PROVIDERS, HTTP_PROVIDERS, provide(LocationStrategy, {useClass: HashLocationStrategy}), UserService]);
///
从'angular2/platform/browser'导入{bootstrap};
从'angular2/core'导入{provide};
从“angular2/ROUTER”导入{ROUTER_提供者、ROUTER_指令、RouteConfig、Location、LocationStrategy、HashLocationStrategy};
从“./app.component”导入{AppComponent};
从'angular2/HTTP'导入{HTTP_提供者};
从“./services/user.service”导入{UserService};
进口“rxjs/Rx”;
引导(AppComponent、[ROUTER_PROVIDERS、HTTP_PROVIDERS、Provider(LocationStrategy,{useClass:HashLocationStrategy})、UserService]);
现在,当我尝试运行我的组件时。它抛出错误,就像
错误:未捕获(在承诺中):没有提供程序!
angular2 polyfills.js:528未经处理的承诺拒绝:没有供应商!
NoProviderError{消息:“没有为!提供程序,堆栈:”错误:DI异常↵ 在NoProviderError.BaseExc…odules/angular2/bundles/angular2.dev.js:11788:19)”中,键:数组[3],注入器:数组[3]}
有人能指导我吗?你能为Angular2使用.dev.js文件吗(例如angular.dev.js)?这样我们将使消息更容易理解;-)@ThierryTemplier-是的,我在angular 2.Ok中使用.dev.js。如果您删除组件的构造函数,是否存在相同的错误?@ThierryTemplier-我尝试删除构造函数并使用ngOnInit调用该方法。现在的错误类似于“TypeError:无法读取组件的属性“getCurrentUser”[null]中未定义。很抱歉,我是Angular2的新手,只是想知道您提到删除构造函数的原因?我想确定这一级别的依赖项注入是问题所在……您是否可以将.dev.js文件用于Angular2(例如Angular.dev.js)?这样我们将使消息更容易理解;-)@ThierryTemplier-是的,我在angular 2.Ok中使用.dev.js。如果您删除组件的构造函数,是否存在相同的错误?@ThierryTemplier-我尝试删除构造函数并使用ngOnInit调用该方法。现在的错误类似于“TypeError:无法读取组件的属性“getCurrentUser”[null]中未定义。抱歉,我是Angular 2的新手,只是想知道您提到删除构造函数的原因?我想确定此级别的依赖项注入是问题所在。。。