2016-08-23 50 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个组件。

不知道你在这里试图做什么,也许一个组件应该是一个指令,而不是?