2015-11-19 96 views
0

我想實現一些非常簡單的登錄嘗試應用良好的OOP概念,但我無法在組件之間共享變量。Angular2如何保護內容

基本我有一個主要組件,有兩個子組件登錄和ProtectedComponent,流程是當一個人登錄我想隱藏登錄組件和顯示受保護的組件。你可以在下面看到代碼。

import {bootstrap, Component, View, NgIf} from 'angular2/angular2'; 
 

 
//Protected-Content Component 
 
@Component({ 
 
    selector: 'protected-content' 
 
}) 
 
@View({ 
 
    templateUrl: 'app/views/protected-component.html' 
 
}) 
 
class ProtectedComponent{ 
 
} 
 

 

 
//Login Component 
 
@Component({ 
 
    selector: 'login' 
 
}) 
 
@View({ 
 
    templateUrl: 'app/views/login.html' 
 
}) 
 
class Login{ 
 
    login(username, password){ 
 
    if(username.value =="test" && password.value=="test"){ 
 
     isLogged = true; 
 
    } 
 
    } 
 
} 
 

 
@Component({ 
 
    selector: 'main' 
 
}) 
 
@View({ 
 
    template:` 
 
    <login *ng-if="!isLogged"></login> 
 
    <protected-content *ng-if="isLogged"></protected-content>`, 
 
    directives:[Login,ProtectedComponent,NgIf] 
 
}) 
 
class Main{ 
 
    isLogged:boolean; 
 
    constructor(){ 
 
    this.isLogged = false; 
 
    } 
 
} 
 

 
bootstrap(Main);

任何想法?

+0

我認爲你不能保護前端的內容。例如Chrome中的控制檯可以從Firebase讀取配置 – Codenator81

回答

0

爲此我使用了EventEmiter,它允許組件之間進行通信。這裏不錯example