Angular 2 错误 : Unhandled Promise rejection: Template parse errors: More than one component:

标签 angular typescript components

几个月前,我使用旧测试版开发了一个 Angular2 应用程序> 目前,我正在将另一个应用程序转移到最新版本(RC5 版本)的全新版本,目前为止没有任何障碍。直到我收到以下错误:

zone.js:484 Unhandled Promise rejection: Template parse errors:
More than one component: ProductComponent,OverlayComponent ("'noscroll': hideBody}">

我有一个子组件产品组件,它作为应用程序组件的子组件包含在内。我将这两个都包含在 app.module.ts 文件中。

我不确定这个错误是什么意思,并且在网上找不到对此的支持。以下是相关文件:

应用程序模块.ts

import './rxjs-extensions';

import { NgModule }       from '@angular/core';
import { BrowserModule }  from '@angular/platform-browser';
import { HttpModule }     from '@angular/http';

import { AppComponent }   from './app.component';
import { ProductComponent }   from './components/product.component';
import { OverlayComponent }   from './components/overlay-component';

import { ProductService }   from './services/product.service';
import { CategoryService }   from './services/category.service';
import { Breadcrumb} from "./directives/breadcrumb";
import { CategoryTree } from "./directives/category-tree";
import { Files } from "./directives/files";
import { Gallery } from "./directives/gallery";
import { OrderInformation } from "./directives/order-information";




@NgModule({
    imports:      [
        BrowserModule,
        HttpModule
    ],
    declarations: [
        AppComponent,
        ProductComponent,
        OverlayComponent,
        Breadcrumb,
        CategoryTree,
        Files,
        Gallery,
        OrderInformation
    ],
    providers: [
        ProductService,
        CategoryService
    ],
    bootstrap:    [ AppComponent ]

})
export class AppModule { }

应用程序组件.ts

import { Component } from '@angular/core';
import { Product } from "./classes/Product";
import { ProductService } from "./services/product.service";
import { Category } from "./classes/Category";
import { CategoryService } from "./services/category.service";

@Component({
    selector: 'product-display',
    templateUrl: './app/views/main-view.html'
})

export class AppComponent {
    title = `St. David's Poultry Supplies`;
    menuLoaded = false;
    hideBody = false;
    productsLoaded = false;
    categories = [];
    selectedCategory = null;
    selectedProduct = Product;
    breadcrumbCategories = [];
    products = [];
    APIError = [];

    constructor(
        private _productService: ProductService,
        private _categoryService: CategoryService
    ) {

    }

    getProducts(categoryId = 0) {
        var payload = {
            order           : 'asc',
            order_by        : 'title',
            category_id     : categoryId,
            resize          : true,
            imgHeight       : 200,
            imgWidth        : 200
        };

        this._productService.getProducts(payload)
            .subscribe(
                products => {this.products = products},
                error    => {this.APIError = error},
                ()       => {this.productsLoaded = true}
            );
    }

    getCategories() {
        this.productsLoaded = false;
        this._categoryService.getCategories({
            order           : 'asc',
            order_by        : 'name',
            parent_id       : 0,
            //sub_cats        : true,
            //group_by_parent : true
        })
            .subscribe(
                categories => {this.categories = categories},
                error      => {this.APIError = error},
                ()         => {
                    this.menuLoaded = true,
                        this.productsLoaded = true
                }
            );
    }

    selectCategory(category: Category) {
        this.selectedCategory = category;
        this.breadcrumbCategories.push(category);
    }
    resetFilters() {
        this.getProducts();
        this.getCategories();
        this.selectedCategory = null;
        this.selectedProduct = null;
    }
    private changeCategory(category: Category):void {
        this.productsLoaded = false;
        this.selectCategory(category);
        this.getProducts(category.id);
    }

    ngOnInit() {
        this.getCategories();
        this.getProducts();
    }
}

产品.组件.ts

import { Component, Input } from '@angular/core';

import { Product } from "../classes/Product";
import { Category } from "../classes/Category";
import { ProductService } from "../services/product.service";

@Component({
    selector: 'product-view',
    templateUrl: './app/views/product-view.html'
})

export class ProductComponent {
    @Input() products: Product[];
    @Input() selectedCategory: Category;
    selectedProduct: Product;
    contentLoaded = false;
    title = "product viewer";
    APIError = [];

    constructor(
        private _productService: ProductService
    ) {
        _productService.emitter.subscribe(
            product  => {
                this.selectedProduct = product;
                this.contentLoaded = true
            }
        );
    }

    selectProduct(product) {
        this.selectedProduct = product;
        this._productService.emitProduct(this.selectedProduct);
    }

    ngAfterContentInit() {
        this.contentLoaded = true;
    }

    private changeSelectedProduct(product, callback) {
        this.selectedProduct = product;
    }
}

之前没有这个问题,我很困惑为什么会出现这个错误。谁能指出我正确的方向?

谢谢

最佳答案

您要么需要使 ProductComponentOverlayComponent 的选择器更具体,这样它们就不会同时应用,或者将您的应用程序拆分成几个模块,这样您只有declarations 已注册,实际上应该应用于当前模块中的模板。

如果您只有一个模块,那么所有组件、指令和所有 directives 的管道都将应用于所有组件。

关于 Angular 2 错误 : Unhandled Promise rejection: Template parse errors: More than one component:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39245168/

相关文章:

javascript - typescript | JavaScript | Angular 2 : Dynamically Set @HostListener Argument

Angular 4 向控制台打印消息

javascript - 使用 typescript 定义 Angular Directive(指令)的最简单方法和示例

extjs - CQ5,使用extjs在组件对话框中动态设置 'defaultValue'?

java - 在 Swing 运行时动态添加按钮

Java list 属性缺失

javascript - 如何在 Angular 中构建延迟加载侧边栏?

angular - routerLink 正在渲染 %23,而不是#

javascript - 根据源对象是否具有具有特定值的深度嵌套属性,有条件地渲染 JSX 元素

node.js - 如何在 NestJS 中模拟存储库、服务和 Controller (Typeorm 和 Jest)