首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于认证角2的链路隐藏

基于认证角2的链路隐藏
EN

Stack Overflow用户
提问于 2017-03-31 14:40:23
回答 1查看 2.9K关注 0票数 4

我试图在没有刷新页面的情况下立即隐藏基于用户身份验证的Navbar链接。

AppComponent.html

代码语言:javascript
复制
<div class="content">
    <md-toolbar>
        <md-icon>menu</md-icon>
        <span class="fill-space"></span>
        <button md-icon-button><md-icon>search</md-icon></button>
        <md-input-container>
            <input mdInput name="search" #search placeholder="Search">
        </md-input-container>

        <button md-raised-button color="primary" (click)="OpenLoginDialog()" *ngIf="!isAuthenticated">Login</button>
        <button md-raised-button (click)="OpenSignupDialog()" *ngIf="!isAuthenticated">Signup</button>
        <button md-raised-button (click)="Logout()" *ngIf="isAuthenticated">Logout</button>
    </md-toolbar>
</div>

认证服务

代码语言:javascript
复制
import { Injectable } from "@angular/core";
import { Observable } from "Rxjs";
import { Http, Headers } from "@angular/http";
import { User } from "./UserModel";

@Injectable()
export class AuthenticationService {
    isLoggedin: boolean = false;

redirectUrl: string;
constructor(private http: Http) { }
login(model: User) {
    debugger;
    let headers = new Headers({ 'Content-Type': 'application/json' });
    return this.http.post("/Account/Login", JSON.stringify({ model }), { headers: headers }).map(res => res.json()).map(res => {
        debugger;

        if (res.isSuccess) {
            localStorage.setItem("auth_token", res.UserInfo);
            this.isLoggedin = true;
        }
        return res.isSuccess;
    });



}
logout(): void
{
    localStorage.removeItem('auth_token');
    this.isLoggedin = false;
} 
isLoggedIn() {
    debugger;
    if (localStorage.getItem("auth_token") == null) {
        this.isLoggedin = false;
        return this.isLoggedin;
    }
    else {
        return true;
    }
}
}

App.Component

代码语言:javascript
复制
    import { Component, OnInit } from "@angular/core";
import { FormBuilder, FormControl, FormGroup, Validators } from '@angular/forms';
import { Validate } from "./Custom.Validator";
import { MdDialog } from "@angular/material";
import { DialogComponent } from "./Dialog.Component";
import { LoginComponent } from "./Login.Component";
import { SignupComponent } from "./Signup.Component";
import { AuthenticationService } from "./Authentication.Service";
@Component({
    selector: 'app-main',
    templateUrl: "../App/App.html",
    styles: [`div {
    padding: 1rem;
  }`]
})

export class AppComponent implements OnInit {
    selectedEmoji: string;
    isAuthenticated: boolean;
    myForm: FormGroup;
    constructor(private fb: FormBuilder, public dialog: MdDialog, public authService: AuthenticationService) { }
    ngOnInit() {
        this.myForm = this.fb.group({
            state: ['', Validators.required],
            zip: ['', [Validators.required, Validate]],
        });
        this.isLoggedIn();
    }
    openEmojiDialog() {
        let dialog = this.dialog.open(DialogComponent);
        dialog.afterClosed().subscribe(selection => {
            if (selection) {
                this.selectedEmoji = selection;

            }
            else {

            }
        });
    }
    OpenLoginDialog() {
        let dialog = this.dialog.open(LoginComponent);
    }
    OpenSignupDialog() {
        let dialog = this.dialog.open(SignupComponent);
    }
    Logout() {
        this.authService.logout();
    }
    isLoggedIn()
    {
        this.isAuthenticated = this.authService.isLoggedIn();
    }
}

这些链接仅在页面刷新后才会隐藏,但我希望它在用户身份验证后立即隐藏。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-03-31 14:47:18

由于您的Authentication Service中已经有了一个函数,并且您已经将Authentication Service注入到您的App.Component中,所以继续使用一个简单的*ngIf="authService.isLoggedIn()",而不是在App.Component中创建一个局部变量,其中的数据将变得陈旧。

它看起来像这样在使用中:

代码语言:javascript
复制
<li *ngIf="authService.isLoggedIn()">
  <a [routerLink]="['/path1']">Path 1</a>
</li>
<li *ngIf="authService.isLoggedIn()">
  <a [routerLink]="['/path2']">Path 2</a>
</li>

可以将您在身份验证服务中已经可用的变量缩短为*ngIf="authService.isLoggedin"

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43143429

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档