我正在尝试获取Session数据,在Service-GlobalServiceService的GetSessionData()方法中使用http get,在我的Component-HeaderComponent中,我订阅来自GetSessionData()的响应,然后分配给UserDetails类型的对象这是一个模型类。 服务 -
import {Observable} from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
import { UserDetails } from "app/model/UserDetails";
@Injectable()
export class GlobalServiceService {
constructor(private lohttp: Http, private loAuthService : AuthService) { }
GetSessionData(): Observable<UserDetails[]>
{
return this.lohttp.get('http://localhost:49609/api/Authentication/checkUserSession')
.map((res:Response) => res.json()
.catch((error:any) => Observable.throw(error.json().error || 'Server
error'))
);
}
}
组件 -
import { GlobalServiceService } from './../services/global-service.service';
export class HeaderComponent implements OnInit {
lsUserName: string;
lsUserDetails:UserDetails[];
constructor(private loAuthService: AuthService,private loGlobalService:
GlobalServiceService,private userDetails:UserDetails) { }
LoadSessionData()
{
this.loGlobalService.GetSessionData()
.subscribe(
data => this.lsUserDetails = data,
err => { console.log(err);}
);
}
ngOnInit() {
this.LoadSessionData();
}
}
我是角色2的新手。我无法理解为什么即使我导入它也会出现如下错误:
1.ERROR Error: No provider for UserDetails!
at injectionError (core.es5.js:1232) [angular]
...
View_AppComponent_0 @ AppComponent.html:2
proxyClass @ compiler.es5.js:13560
...
2. zone.js:569 Unhandled Promise rejection: No provider for UserDetails! ;
3. Zone: <root> ; Task: Promise.then ; Value: Error: No provider for
UserDetails!
at injectionError (core.es5.js:1232) [angular]
4.zone.js:571 Error: Uncaught (in promise): Error: No provider for
UserDetails!
---&gt;当UserDetails添加到app.module.ts中的导入时,它给出了foll错误
未捕获错误:意外值&#39; UserDetails&#39;由模块导入 &#39;的AppModule&#39 ;.请添加@NgModule注释。 在syntaxError(http://localhost:4200/vendor.bundle.js:44937:34)[]
答案 0 :(得分:1)
由于UserDetails
是模型,因此无法将其注入构造函数中。
constructor(private loAuthService: AuthService,private loGlobalService:
GlobalServiceService,private userDetails:UserDetails)
删除此 - &gt; HeaderComponent 中的private userDetails:UserDetails
。