我正在用angular创建一个基本的用户注册应用程序。我想在成功创建时通知用户,如果抛出任何错误。我正在尝试使用小吃栏通知用户。但是,我无法这样做。我还插入了一个警报,以查看是否要进入该函数,并且该警报可见。预先感谢您的帮助。
这是我的ts代码:
export class AdduserComponent implements OnInit {
constructor(private formBuilder:FormBuilder,private userService:UsersService,private router:Router,private snackBar: MatSnackBar) { }
addForm: FormGroup;
selected = 'option2';
passwordsMatcher = new RepeatPasswordEStateMatcher;
ngOnInit()
{
this.addForm = this.formBuilder.group({
id: [],
userName: ['', Validators.required],
password:new FormControl( '',[ Validators.required]),
passwordAgain: new FormControl('',[ Validators.required]),
userRole:['',Validators.required],
},{ validator: RepeatPasswordValidator });
}
onSubmit() {
if (this.addForm.valid)
{
this.userService.createUser(this.addForm.value)
.subscribe( data => {
console.log(data);
alert("User created");
//this.router.navigate(['adduser']);
},error=>
{console.log(error)
alert("Usernmame already exists,choose a different userName");
this.snackBar.open("message", "action", {
duration: 2000,
});
this.addForm.controls.userName.reset();
});
}
答案 0 :(得分:0)
解决了。警报阻止了小吃店。我删除了警报,小吃栏现在可见。