typescript - Angular 2 錯誤:未處理的 Promise 拒絕:模板解析錯誤:多於一個元件:
幾個月前,我使用舊的 beta 開發了一個 Angular2 應用程式。 我目前正在將這個應用程式移至最新版本(RC5 版本)的全新構建,到目前為止一切順利。 直到我開始收到以下錯誤為止:
zone.js:484 Unhandled Promise rejection: Template parse errors:
More than one component: ProductComponent,OverlayComponent ("'noscroll': hideBody}">
我有一個子組件 product-component,它被作為 app-component 的子組件。我在 app.module.ts 文件中包含了這兩個組件。
我不確定這個錯誤意味著什麼,也尚未在網上找到任何相關的支援。以下是相關的檔案:
app.module.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 { }
app.component.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();
}
}
product.component.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;
}
}
以前這沒有問題,我對為什麼會出現這個錯誤感到困惑。有人能指引我正確的方向嗎?
謝謝
答案#1
你要麼需要使 ProductComponent、OverlayComponent 的選擇器更具特異性,以免兩者都被應用,要麼將你的應用程式拆分成幾個模組,這樣你只會註冊那些應該實際應用於當前模組模板的 declarations。
如果您只有一個模組,那麼所有 directives 的元件、指令和管道都會套用到所有元件。
答案#2
我遇到了這個問題,而我的問題是 templateURL 與 HTML 檔案的檔案名稱不匹配。