简体   繁体   English

如何正确使用* ng?

[英]How to use *ngIf correctly?

I want to use *ngIf in my angular project. 我想在我的角度项目中使用* ngIf。 I use app.component.html to handle login page and as well as dashboard. 我使用app.component.html来处理登录页面和仪表板。 So I want to hide dashboard before user login to the website. 因此,我想在用户登录网站之前隐藏仪表板。 After user logged to the system I want to show the dashboard. 用户登录系统后,我想显示仪表板。

So I use *ngIf to handle this. 所以我用* ngIf处理这个。 I use following code to handle the case. 我使用以下代码来处理这种情况。

In my app.component.html I use following div to show when ever user is not log to the system. 在我的app.component.html中,我使用以下div来显示用户何时未登录到系统。

<div class="login-page" *ngIf="!isLog()">

... go some code here...

</div>

And following div is use to show the dashboard after user is log to the system. 用户登录到系统后,以下div用于显示仪表板。

<div class="main-dashboard" *ngIf="isLog()">

... go some code here and router outlet as well ...

<router-outlet></router-outlet>

</div>

Finally in my app.component.ts file is use isLog() method to check whether user is logged or not. 最后,在我的app.component.ts文件中,使用isLog()方法检查用户是否已登录。


isLog(){
// Use to back end api to check user log
var log : boolean = false;
// If User is Log
// Use back end api to handle this
log = true;
return log;
} 

But the issue is when user try to log to the system for first time this code structure works fine for that case. 但是问题在于,当用户首次尝试登录系统时,此代码结构在这种情况下可以正常工作。 But after user log to the system if user refresh the page for little time (0.5 ms > time) system shows the login page and suddenly shows the current page. 但是,在用户登录到系统后,如果用户在短时间内刷新页面(0.5 ms>时间),系统将显示登录页面,并突然显示当前页面。

I think this is because every time user refresh the page website try to check user is log. 我认为这是因为每次用户刷新页面网站时都会尝试检查用户是否为日志。 During that time isLog() method return false so website shows login page for that time. 在此期间,isLog()方法返回false,因此网站将显示该时间的登录页面。

After isLog() return the valid value it means true. 在isLog()返回有效值之后,它表示true。 Then it shows the dashboard. 然后显示仪表板。

But I want to avoid this case. 但我想避免这种情况。 After user log to the system I don't want to show login page even for little time. 用户登录到系统后,我什至不想显示登录页面。 Because user get confused. 因为用户感到困惑。

How can avoid this case? 如何避免这种情况? Is there are any possible way to handle this type of issue?? 是否有任何可能的方式来处理此类问题? OR recommended way??? 或推荐方式???

If that's the case, try to store this info in sessionStorage which will persist the data for that tab and you wont have to wait for server response. 如果是这种情况,请尝试将此信息存储在sessionStorage中 ,该信息将保留该选项卡的数据,而您不必等待服务器响应。

Also, as a side note, using *ngIf="isLog()" is not a good approach. 另外,作为附带说明,使用*ngIf="isLog()"也不是一种好方法。 You can try to put console.log in isLog() and you'll see it getting triggered periodically. 您可以尝试将console.log放入isLog()并看到它会定期触发。 That's because of change Detection cycle of angular which works on all the expressions assigned to template , and since *ngIf has function associated to it, that function ( isLog() ) is executed. 这是因为对*ngIfchange Detection周期适用于分配给template所有表达式,并且由于*ngIf具有与其关联的函数,因此将执行该函数( isLog() )。

Its better if you can evaluate this value on ngOnInit and assign it some variable which will be used as a flag to check *ngIf condition 如果您可以在ngOnInit上评估此值并为其分配一些变量,该变量将用作检查*ngIf条件的标志,则*ngIf

Of course it depends on your needs but the best practise is making the login page a seperate component and use Route Guards to protect the routes. 当然,这取决于您的需求,但是最佳实践是使登录页面成为单独的组件,并使用Route Guards保护路由。

If having it in the same page and using ngIf is enough for your requirements which means you have a small application and it is not likely to expand, it is fine. 如果将它放在同一页面上并使用ngIf足以满足您的要求,这意味着您的应用程序很小,并且不太可能扩展,那就很好了。 The problem you experience is well explained by @Shashank Vivek in his answer. @Shashank Vivek在回答中很好地解释了您遇到的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM