2017-06-05 81 views
0

雖然我已經嘗試了很多次,但沒有工作EMIT或SUBSCRIBE。Angular2訂閱不工作關於服務

我想隱藏的應用程序頭,應用程序,工具條和app-頁腳

LoginComponent。另外* ng如果不監聽GlobalService變量。

在此先感謝您的支持。

/* App HTML */ 

<app-header *ngIf="global.showHeader"></app-header> 

<app-sidebar *ngIf="global.showSidebar"></app-sidebar> 

<div class="content-wrapper"> 
    <router-outlet></router-outlet> 
</div> 

<app-footer *ngIf="global.showFooter"></app-footer> 
/* AppComponent */ 

@Component({ 
    selector: 'app-root', 
    templateUrl: './app.component.html', 
    providers: [AppGlobalService] 
}) 
export class AppComponent implements OnInit{ 

    global = {}; 

    constructor(private globalService: AppGlobalService) { 

    this.globalService.listenUpdated.subscribe(

     data => { 

     this.global = data; 

     } 

    ); 
    } 

    ngOnInit(){ 
    } 
} 
/* AppGlobalService */ 

@Injectable() 
export class AppGlobalService { 

    data={ 
     showHeader: true, 
     showSidebar: true, 
     showFooter: true 
    }; 

    listenUpdated = new EventEmitter<any>(); 

    constructor(){} 

    updateStatus(key: any, value: any) { 
     this.data[key] = value; 
     console.log('SERVICE ', this.data); // It's working... 
    } 
} 
/* LoginComponent */ 

@Component({ 
    selector: 'app-login-index', 
    templateUrl: './index.component.html', 
    providers: [AppGlobalService] 
}) 
export class LoginIndexComponent implements OnInit { 

    private data = {}; 

    constructor(public globalService: AppGlobalService) { 
     this.globalService.updateStatus('showHeader', false); 
     this.globalService.updateStatus('showSidebar', false); 
     this.globalService.updateStatus('showFooter', false); 
     this.globalService.listenUpdated.emit(true); 
    } 

    ngOnInit() { 
    } 

} 

回答

0

我覺得自己像一個更好的方式做這將是有一個登錄組件,然後讓說,一個家庭的組成部分。 Login組件不會包含側邊欄或頁腳的html或任何類似的東西。

當某人未通過身份驗證時,您將使用路由路由到登錄組件。一旦他們通過登錄組件進行身份驗證,您將路由到家庭組件,在此處會顯示頁腳和側欄等內容。

整個guide to routing包括登錄功能。所有這些都可以在現場看到。