[英]how to import a component using the default angular project in Visual Studio
I am having a problem with angular using the default angular visual studio project.我在使用默认的 angular Visual Studio 项目时遇到了 angular 的问题。 I am currently unsure of how to add more than one component to a.ts file and I am starting very simple我目前不确定如何将多个组件添加到 a.ts 文件中,并且我开始非常简单
all i am trying to do is add the existing "Counter" component to the existing fetch-data.html page我要做的就是将现有的“计数器”组件添加到现有的 fetch-data.html 页面
import { Component } from '@angular/core';
@Component({
selector: 'app-counter-component',
templateUrl: './counter.component.html'
})
export class CounterComponent {
public currentCount = 0;
public incrementCounter() {
this.currentCount++;
}
}
to fetch-data-component.html获取数据组件。html
<h1 id="tableLabel">Weather forecast</h1>
<p>This component demonstrates fetching data from the server.</p>
<h1>Counter</h1>
<p>This is a simple example of an Angular component.</p>
<p aria-live="polite">Current count: <strong>{{ counter }}</strong></p>
<button class="btn btn-primary" (click)="counter.incrementCounter()">Increment</button>
<p *ngIf="!forecasts"><em>Loading...</em></p>
<table class='table table-striped' aria-labelledby="tableLabel" *ngIf="forecasts">
<thead>
<tr>
<th>Date</th>
<th>Temp. (C)</th>
<th>Temp. (F)</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let forecast of forecasts">
<td>{{ forecast.date }}</td>
<td>{{ forecast.temperatureC }}</td>
<td>{{ forecast.temperatureF }}</td>
<td>{{ forecast.summary }}</td>
</tr>
</tbody>
</table>
import { Component, Inject } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { CounterComponent } from '../counter/counter.component';
@Component({
selector: 'app-fetch-data',
templateUrl: './fetch-data.component.html',
})
export class FetchDataComponent
{
public counter: CounterComponent;
public forecasts: WeatherForecast[];
constructor(http: HttpClient, @Inject('BASE_URL') baseUrl: string)
{
http.get<WeatherForecast[]>(baseUrl + 'weatherforecast').subscribe(result =>
{
this.forecasts = result;
}, error => console.error(error));
}
}
interface WeatherForecast {
date: string;
temperatureC: number;
temperatureF: number;
summary: string;
}
as you can see I have imported CounterComponent and added it to the export class如您所见,我已导入 CounterComponent 并将其添加到导出 class
export class FetchDataComponent
{
public counter: CounterComponent;
but all I get is errors."incrementCounter is not a function" I am not really sure where to go after importing CounterComponent但我得到的只是错误。“incrementCounter 不是函数”我不确定导入 CounterComponent 后 go 的位置
how can I import and add the counter component to the fetch-data.html page?如何将计数器组件导入并添加到 fetch-data.html 页面? What is the right way to import and use this component导入和使用此组件的正确方法是什么
<app-counter-component></app-counter-component>
将它放在 FetchDataComponent 模板中,例如<app-counter-component></app-counter-component>
fetch-data.component.html获取数据.component.html
<h1> Some heading</h1>
<app-counter-component></app-counter-component>
<h2> The End </h2>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.