【发布时间】:2017-11-14 13:58:36
【问题描述】:
在我的应用程序中,我遇到了 Angular 不绑定属性的问题。它也不会调用ngOnInit 和ngAfterViewInit 或执行指令。
这是部分 LoginComponent 代码:
@Component({
templateUrl: './login.component.html',
styleUrls: ['./login.component.scss']
})
export class LoginComponent implements OnInit
//.....
public test: string = 'some dummy test';
constructor(
private cd: ChangeDetectorRef,
private fb: FormBuilder, private router: Router, private route: ActivatedRoute,
private authService: AuthService,
private usersService: UserService,
private notificationsService: NotificationsService
)
{
this.loginForm = fb.group({
email: ["", Validators.required],
password: ["", Validators.required]
});
console.info("constructor");
}
ngOnInit()
{
console.info("ngOnInit");
this.returnUrl = this.route.snapshot.queryParams['returnUrl'] || '/';
}
ngAfterViewInit()
{
this.initGapi();
console.info("afterviewinit");
}
奇怪的是,这在我打开页面或 HMR 刷新页面时有效,但在我登录和注销时无效。
这是注销代码:
logout(): boolean
{
this.authService.logout();
}
//authservice.ts
logout()
{
localStorage.removeItem("token");
localStorage.removeItem("refresh_token");
localStorage.removeItem("profile");
if (gapi.auth2)
{
const instance = gapi.auth2.getAuthInstance();
if (instance)
{
instance.signOut().then(() =>
{
this.router.navigate(["login"]);
});
return;
}
}
this.router.navigate(["login"]);
}
我注销和绑定不发生的区别是:
按钮没有样式。这应该做
kendoButton指令<button class="center" kendoButton type="submit">缺少测试绑定(一些虚拟测试)
-
ngOnInit和ngAfterViewInit未被调用。
控制台没有错误。我还记录屏幕(它从空白页开始)。我有最新的 Chrome 和 Firefox,两个浏览器都有同样的问题。
有人知道哪里出了问题或我还能尝试什么吗?
【问题讨论】:
-
您的登录逻辑将用户重定向到哪里?
-
可能相关 stackoverflow.com/a/47268066/3731501 ,但我不确定您的路由器插座发生了什么。
-
Danel:它从 localhost:50222/home 重定向到 localhost:50222/login。 estus:我不明白这与你提供的问题有什么关系。
标签: angular angular2-routing angular2-template