且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Angular2服务未作为单例注入

更新时间:2023-11-26 16:37:04

在以下***中找到了解决方案:

The solution was found in the following ***:

Angular2 router.navigate刷新页面

这使我在Internet Explorer中对其进行了测试,该工作正常,但无法在Chrome中正常工作

This led me to test it in Internet Explorer, which worked OK (it was not working in Chrome)

原来,执行以下行时,整个页面都在刷新:

It turned out that the entire page was refreshing when the following line was executed:

this.router.navigate([this.returnUrl]);

这是导致用户服务重新加载的原因,因为按钮上没有 type 类型,Chrome的默认操作是将按钮单击视为提交.

Which was causing the user service to reload again, because without a type on the button, Chrome's default action was to treat the button click as a submit.

解决方法是将标记 type ="button" 添加到html中的登录按钮

The fix was to add the tag type="button" to the login button in the html

<button type="button" class="btn btn-primary"(click)="validateUser(username, password)">Login</button>

它现在可以在所有浏览器中使用.

It now works in all browsers.