错误:(SystemJS)无法解析RegisterComponent的所有参数:(Router,UserService,?)
这是组件:RegisterComponent
import { Component,OnInit,Injectable} from '@angular/core';
import { FormGroup } from '@angular/forms';
import { Router } from '@angular/router';
import { FieldBase } from '../field';
import {UserService} from '../../../core/services/user.service';
import { RegisterService } from '../../../core/services/register.service';
@Component({
moduleId:module.id,
selector: 'register',
templateUrl: 'register.component.html',
styleUrls:['register.component.css'],
})
export class RegisterComponent implements OnInit {
form:FormGroup;
registered = false;
fields:FieldBase<any>[] = [];
alert:any = {msg:'注册成功',type:'success',closable:true};
constructor(
private route:Router,
private userService:UserService,
private registerService:RegisterService
) {
this.fields = registerService.getFields();
}
ngOnInit(){
this.form = this.registerService.toFormGroup(this.fields);
}
showPassword(){
this.fields.forEach((field:any)=>{
if(field.key ==='password'){
field.type = field.type==='password'?'text':'password';
}
});
}
resetForm(){
this.form.reset();
}
register(){
this.registerService.addUser(this.form.value)
.subscribe(res=>{
let body = res.json();
//注册成功
if(body && body.succes){
this.alert.msg = body.message||"注册成功";
this.alert.type = body.success?"success":"danger";
this.registered = true;
this.userService.isLogin = true;
//缓存用户信息
this.userService.userInfo = {username:this.form.value.username,createDate:new Date().toLocaleDateString()}
//跳转到首页
this.route.navigate(['']);
}else{
this.registered = false;
console.log(body.message);
}
},
(error:any)=>
console.log(error)
);
}
}
RegisterService代码:
import { Injectable } from '@angular/core';
import { Http,Headers } from '@angular/http';
import { FormControl,Validators,FormGroup } from '@angular/forms';
import { FieldBase,FieldText,FieldValidators} from '../../user/shared/index';
import { SITE_HOST_URL } from '../../shared/index';
@Injectable()
export class RegisterService {
private registerUrl = `${SITE_HOST_URL}user/add`;
constructor(private http:Http) { }
getFields(){
let fields:FieldBase<any>[]=[
new FieldText({
key:'username',
lable:'用户名',
value:'',
required:true,
pattern:'username',
order:1
}),
new FieldText({
key:'password',
lable:'密码',
type:'password',
value:'',
required:true,
pattern:'password',
order:2
}),
];
return fields.sort((a,b)=>a.order - b.order);
}
toFormGroup(fields:FieldBase<any>[]){
let group :any = {};
fields.forEach(field=>{
group[field.key]=
field.pattern?
new FormControl(field.value||'',(<any>FieldValidators)[field.pattern]):
field.required?
new FormControl(field.value||'',Validators.required):
new FormControl(field.value||'');
});
return new FormGroup(group);
}
addUser(data:Object){
let body = JSON.stringify(data);
let headers = new Headers();
headers.append('Content-Type','application/json');
return this.http.post(this.registerUrl,body,{headers});
}
}
像这样的CoreModule:
import { QuestionnaireService } from './services/questionnaire.service';
import { RegisterService } from './services/register.service';
import { UserService } from './services/user.service';
import { AuthGuardService } from './services/auth-guard.service';
import { LoginService } from './services/login.service';
@NgModule({
imports: [CommonModule,RouterModule],
exports: [],
declarations: [],
providers: [
QuestionnaireService,
RegisterService,
UserService,
LoginService,
AuthGuardService]
})
export class CoreModule {
constructor(@Optional() @SkipSelf() parentModule :CoreModule){
if(parentModule){
throw new Error(
'CoreModule is already laoded,Import it in the AppModule only'
);
}
}
}
规范:其他服务(QuestionnaireService,UserService ..)可以正常运行。 如果按照以下方式修改此参数,它将起作用。
export class RegisterComponent implements OnInit {
form:FormGroup;
registered = false;
fields:FieldBase<any>[] = [];
alert:any = {msg:'注册成功',type:'success',closable:true};
constructor(
private route:Router,
private userService:UserService,
**//private registerService:RegisterService**
) {
this.fields = registerService.getFields();
}