angular - 角度组件问题
问题描述
在过去的几天里,我一直被这段代码所困扰。我已经尝试卸载并重新安装。根据我的导师的说法,代码是正确的,并且在他的计算机上运行良好,所以不明白为什么这对我来说是个难题。
app.component.ts:
import { Component } from '@angular/core';
import { ProductService } from './product.service';
@Component({
selector: 'app-root',
templateUrl:
`<h1>{{ title }}</h1>
<products></products>`,
providers: [ProductService]
})
export class AppComponent {
title = 'sports';
}
产品.service.ts:
export class ProductService{
getProducts() : string[] {
return ["Los Angeles Kings - Hockey","Anaheim Ducks - Hockey","Los Angeles Dodgers - Baseball","Los Angeles Angels of Anaheim - Baseball","San Diego Padres - Baseball"];
}
}
products.component.ts
import { Component } from '@angular/core'
import { ProductService} from './product.service'
@Component({
selector: 'products',
template: `
<h2>Products</h2>
<ul>
<li *ngFor="let product of products">
{{product}}
</li>
</ul>
})
export class ProductsComponent{
products;
constructor(productService: ProductService){
this.products = productService.getProducts();
}
}
app.module.ts:
解决方案
您需要在组件装饰器中使用template
而不是。templateUrl
关于template
:
Angular 组件的内联模板。如果提供,请勿使用 templateUrl 提供模板文件。
@Component({
selector: 'app-root',
template: `
<h1>{{ title }}</h1>
<products></products>
`,
providers: [ProductService]
})
...
还要检查:
ProductsComponent
是您的模块的一部分(declarations
例如添加到数组中AppModule
)
推荐阅读
- linux - 如何将四核的 3 个核心与 Linux 隔离并专门用于 Halcon?
- rust - 如何在 Rocket 中返回从外部 API 查询的对象数组
- excel - 在 Excel 中,每行保存一个数组,使用索引
- sql - SERIAL 字段/自动递增
- webcrypto-api - 使用 Cloudflare Workers 验证 HMAC 哈希
- typescript - 打字稿中的可选对象参数
- react-native - React Native 多入口点
- python - wolfram 'coroutine' 对象没有属性 'text'
- r - R 抽样分布 shapiro.test
- javascript - 当子元素相对于其父元素不发生变化时,减少“重新计算样式”时间