2016-08-23 52 views
1

我只是將我的ng2應用程序從rc.4升級到rc.5。 在我app.html是什麼:angular2多個組件錯誤

<div> 
    <switch-app [account]="cache.accountInfo" [app]="cache.current_app"> 
    <router-outlet></router-outlet> 
</div> 

我宣佈在app.module.ts的SwitchAppComponent:

@NgModule({ 
    imports: [ 
    ] 
    declarations: [ 
     SwitchAppComponent 
    ], 
    bootstrap: [AppComponent] 
}) 
export class AppModule { 
} 

再發生這樣的錯誤:

More than one component: AppComponent,SwitchAppComponent 

[ERROR ->]<switch-app [account]="cache.accountInfo" [app]="cache.current_app"></switch-app> 

我檢查了我應用程序,AppComponent和SwitchAppComponent的選擇器是不同的。

這裏是兩個組件的簡單代碼: 應用組分:

@Component({ 
    selector: '[app]', 
    styleUrls: ['./app.css', './custom.scss'], 
    encapsulation: ViewEncapsulation.None, 
    templateUrl: './app.html' 
}) 
export class AppComponent extends CommonComponent implements OnInit { 

    cache = cache; 
} 

開關應用組件:

@Component({ 
    selector: 'switch-app', 
    templateUrl: './switch-app.html' 
}) 
export class SwitchAppComponent implements OnInit { 
    @Input('app') app; 
    @Input('account') account; 
    ngOnInit() { console.log(this.account, this.app);} 
} 
+0

我發現這個問題,導致我用一個屬性[app]聲明我的AppComponent,但是當我插入switch-app組件時,我使用[app]屬性作爲它的輸入。只要我改變switch-app的[app] attr,所有的事情都是正確的 – shun

回答

3

這個問題是存在的:

<switch-app [account]="cache.accountInfo" [app]="cache.current_app"></switch-app> 

這匹配switch-app選擇器和[app] select或者兩者都是組件的選擇器。 Angular不能在同一元素上使用2個組件。

不知道你在這裏試圖做什麼,也許一個組件應該是一個指令,而不是?