0

私は他の問題や質問で与えられた解決策を探しましたが、まだ解決できません。問題は、最初にコンポーネントが初期化されるときに2回呼び出されるngOnInitです。私は以下のコードを共有して詳しい情報を得る。2度に呼び出された角度4ngOninit

角度/ CLI @アンギュラバージョン

:1.2.6 ノード:6.10.3 OS:ダーウィンのx64

@angular/cli: 1.2.6 
@angular/common: 4.3.2 
@angular/compiler: 4.3.2 
@angular/compiler-cli: 4.3.2 
@angular/core: 4.3.2 
@angular/forms: 4.3.2 
@angular/http: 4.3.2 
@angular/platform-browser: 4.3.2 
@angular/platform-browser-dynamic: 4.3.2 
@angular/router: 4.3.2 

//

import { DashboardService } from './../dashboard.service'; 
import { Component, OnInit, Input, NgZone } from '@angular/core'; 
import { RouterModule, Router } from "@angular/router"; 
import { MenuService } from './menu.service'; 
import { AuthService } from '../../auth/auth.service'; 

@Component({ 
    selector: 'ca-menu', 
    templateUrl: './menu.component.html', 
    styleUrls: ['./menu.component.css'], 
    providers: [MenuService, AuthService] 
}) 
export class MenuComponent implements OnInit { 
    @Input() showLogo = true; 
    sections: {}[]; 
    selectedSection: number = 0; 
    constructor(
    private _menuService: MenuService, 
    private _dashboardService: DashboardService, 
    private _router: Router, private _authService: AuthService, 
) { } 

    ngOnInit() { 
    // let user_id = this._authService.getUserId(); 
    // this.sections = this._menuService.getSections(); 
    // this.sections.map(section => { 
    // let ignored_ids = section['ignored_ids'] ? section['ignored_ids'] : []; 
    // section['blocked'] = ignored_ids.indexOf(Number(user_id)) == -1 ? false : true; 
    // }); 
    console.log('test'); 

    } 


    setSection(section_name: String) { 
    // this._dashboardService.current_section.next(section_name); 
    } 
} 

//テンプレート側

<div *ngIf='sections'> 
    <div class="container-fluid logo" *ngIf="showLogo"> 
     <img src="assets/logo_main.png"> 
    </div> 
    <div *ngFor="let section of sections; let i=index"> 
     <div class="menu-component d-block" *ngIf='!section.blocked' (click)='setSection(section.section_name)' [routerLink]="[section.url]" routerLinkActive="selected"> 
      <i [class]="section.icon" aria-hidden="true" style="margin: 0 10px"></i> {{section.name}} 
     </div> 
    </div> 
</div> 
+1

このコンポーネントの使用場所を表示できますか? 2つのインスタンスを作成していませんか? –

+0

@ Jamil Alisgenderovあなたはapp.module.tsを投稿できますか?また、このGithubの問題のリンクを見てください:[Github Issue](https://github.com/angular/angular/issues/6782) – mrsan22

答えて

0

私は何とか2つのインスタンスを作成しました、ありがとう!

関連する問題