-
Notifications
You must be signed in to change notification settings - Fork 0
Description
错误日志如下:
(SystemJS) Can't resolve all parameters for RegisterComponent: (?, UserService, Router).
组件:RegisterComponent 部分代码如下:
export class RegisterComponent implements OnInit {
form:FormGroup;
registered = false;
fields:FieldBase[] = [];
alert:any = {msg:'注册成功',type:'success',closable:true};
constructor(
private registerService:RegisterService,
private userService:UserService,
private route:Router
) {
this.fields = registerService.getFields();
}
RegisterService.ts的代码如下:
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});
}
}
1.组件中私有属性服务:RegisterService已在父模块中注入了.
2.如果注释掉构造方法中的私有属性private registerService:RegisterService,错误会消失.
3.如果注释掉RegisterService中的toFormGroup(),getFields()方法,则错误会消失.