[英]Angular 2 routers not working as expected
i am trying to achieve following : 我正在努力实现以下目标:
User logins then get redirected to site component(First default page after login) and i store the user configuration ie which all components user can access, in Local Storage or session storage. 然后,用户登录名将重定向到站点组件(登录后的第一个默认页面),我将用户配置(即用户可以访问的所有组件)存储在本地存储或会话存储中。
If user tries to access the /site component directly without logging in , then user should be redirected to login page. 如果用户尝试不登录而直接访问/ site组件,则应将用户重定向到登录页面。
Problem :Above functionality is working very well but : If i try to access /site as first page then angular router redirects me to /login but after logging in , it is not redirecting me to site again.I am expecting that after login it should redirect me to site component again . 问题 :上面的功能运行良好,但是:如果我尝试以第一页访问/ site,那么角度路由器会将我重定向到/ login,但是登录后,它不会将我重新重定向到site。我希望登录后应该可以再次将我重定向到网站组件。
Steps involved: 涉及的步骤:
Open new Tab/Window (so that we dont have any configuration in Local or session storage). 打开新的标签页/窗口(这样我们在本地或会话存储中就没有任何配置)。
Try to access /site, you should automatically be redirected to /login component. 尝试访问/ site时,应自动将其重定向到/ login组件。
After /login it should again redirect to /site (Its not working). 在/ login之后,它应该再次重定向到/ site(它不起作用)。
Login Component: 登录组件:
@Component({
selector: 'login',
templateUrl: './login.component.html',
styleUrls: ['./login.component.css']
})
@NgModule({
providers:[AuthenticationService]
})
export class LoginComponent implements OnInit {
public user: User;
public isUserAuthenticated = false;
// For testing we are going to use Dummy Configuration.
// Actual data will come from Rest Service
private dummyConfiguration:Object={
"user":"dummy",
"component":[{
"name":"CustomerComponent",
"access":"ENABLED"
},
{
"name":"InvoicingComponent",
"access":"HIDDEN"
}
]
};
constructor(private router: Router,private authenticationService : AuthenticationService) {
this.user = new User();
}
login() {
this.isUserAuthenticated = true;
this.authenticationService.saveInSession(this.dummyConfiguration);
this.router.navigate(['/site', {}]);
}
ngOnInit() {
}
}
export class User {
public email: string;
public password: string;
}
SiteComponent SiteComponent
@Component({
selector: 'site',
templateUrl: './site.component.html',
styleUrls: ['./site.component.css']
})
export class SiteComponent extends SuperParentComponent{
constructor(private router: Router, private authenticationService: AuthenticationService) {
super();
this.validateSession(router,authenticationService);
}
}
SuperParentComponent 超级父组件
export class SuperParentComponent {
constructor(){
}
validateSession( router: Router, authenticationService: AuthenticationService) {
if (!authenticationService.isUserLoggedIn()) {
router.navigate(['/login', {}]);
}
}
}
AuthenticationService:ts AuthenticationService:ts
export class AuthenticationService {
@SessionStorage() public userConfiguration: Object;
isAuthentic(component: string):boolean {
if (this.isComponentAllowed(component)){
return true;
}
}
public getUserConfiguration():Object {
return this.userConfiguration;
}
saveInSession(data: Object) {
this.userConfiguration = data;
}
isUserLoggedIn():boolean{
if(this.userConfiguration==null){
return false;
}
return true;
}
isComponentAllowed(component:string){
var result:Array<Object>;
if(this.userConfiguration=={}){
return false;
}
if(this.userConfiguration.hasOwnProperty("component")){
result=this.userConfiguration["component"];
for (var i = 0; i < result.length; i++) {
var currentComponent:Object=result[i];
if (currentComponent["name"] ==component && currentComponent["access"]== AccessType.ENABLED) {
return true;
}
}
}
return false;
}
}
enum AccessType {
ENABLED=<any>"ENABLED",
HIDDEN=<any>"HIDDEN"
}
This user configuration is just for authorization , if user can access the given component or not. 如果用户可以访问给定的组件,则此用户配置仅用于授权。 I will get it from Server.
我将从服务器上获得它。
Here is my full code : https://github.com/tsingh38/Angular2.git 这是我的完整代码: https : //github.com/tsingh38/Angular2.git
UPDATE 更新
As suggested answer, i have adapted the code : 作为建议的答案,我已经修改了代码:
Site component consists of subcomponents GeneralComponent and which consists of Customer Component and Invoice Component. 站点组件由子组件GeneralComponent组成,并且由客户组件和发票组件组成。
They are not shown if site is redirected after login. 如果在登录后重定向站点,则不会显示它们。
Site .html 网站.html
<div style="width: 100%;">
<div style="float:left; width: 20%">
<navigation></navigation>
</div>
<div style="float:right;">
<general></general>
</div>
</div>
<div style="clear:both"></div>
General.Component.html General.Component.html
<div style="border:1px solid black;">
<customer></customer></div>
<div style="border:1px solid blue;float:bottom">
<invoice></invoice>
</div>
Customer.html Customer.html
<div *ngIf="allowed">This is customer data</div>
<div *ngIf!="allowed"></div>
Invoice.html Invoice.html
<div *ngIf="allowed">This is invoice data</div>
<div *ngIf!="allowed"></div>
Customer Component 客户组成
@Component({
selector: 'customer',
templateUrl: './customer.component.html',
styleUrls: ['./customer.component.css'],
providers:[AuthenticationService]
})
export class CustomerComponent extends SuperChildComponent{
private authenticationService:AuthenticationService;
constructor(authenticationService : AuthenticationService) {
super(authenticationService);
this.authenticationService=authenticationService;
this.isAllowed(this.constructor.name);
}
}
SuperChildComponent 超级子组件
export class SuperChildComponent {
public allowed: boolean = false;
private superAuthenticationService:AuthenticationService;
constructor(authenticationService: AuthenticationService) {
this.superAuthenticationService = authenticationService;
}
isAllowed(component: string) {
this.allowed = this.superAuthenticationService.isAuthentic(component);
}
}
Thanks. 谢谢。
I am doing it a similar way. 我正在做类似的方式。 I just use
我只是用
this.router.navigate(['/site']);
That is working for me. 那对我有用。
So change your 所以改变你的
this.router.navigate(['/site', {}]);
in LoginComponent to the above and it should work as expected. 在上面的LoginComponent中,它应该可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.