如何使用 Visual Studio 中的默认 angular 项目导入组件
how to import a component using the default angular project in Visual Studio
我在 angular 使用默认 angular visual studio 项目时遇到问题。我目前不确定如何将多个组件添加到 .ts 文件中,我开始时非常简单
我想做的就是将现有的 "Counter" 组件添加到现有的 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++;
}
}
获取数据-component.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;
}
如您所见,我已经导入了 CounterComponent 并将其添加到导出中 class
export class FetchDataComponent
{
public counter: CounterComponent;
但我得到的只是错误。"incrementCounter is not a function"我不太确定导入 CounterComponent 后去哪里
如何将计数器组件导入并添加到 fetch-data.html 页面?导入和使用该组件的正确方法是什么
- 确保两个组件都在模块声明数组中。
- 您的 FetchDataComponent 将成为父组件,CounterComponent 将成为其子组件,这意味着它将位于父模板内,这样它们就可以相互交互并共享数据。
- CounterComponent 有一个选择器 app-counter-component。像这样将它放在 FetchDataComponent 模板中
<app-counter-component></app-counter-component>
获取-data.component.html
<h1> Some heading</h1>
<app-counter-component></app-counter-component>
<h2> The End </h2>
我在 angular 使用默认 angular visual studio 项目时遇到问题。我目前不确定如何将多个组件添加到 .ts 文件中,我开始时非常简单
我想做的就是将现有的 "Counter" 组件添加到现有的 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++;
}
}
获取数据-component.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;
}
如您所见,我已经导入了 CounterComponent 并将其添加到导出中 class
export class FetchDataComponent
{
public counter: CounterComponent;
但我得到的只是错误。"incrementCounter is not a function"我不太确定导入 CounterComponent 后去哪里
如何将计数器组件导入并添加到 fetch-data.html 页面?导入和使用该组件的正确方法是什么
- 确保两个组件都在模块声明数组中。
- 您的 FetchDataComponent 将成为父组件,CounterComponent 将成为其子组件,这意味着它将位于父模板内,这样它们就可以相互交互并共享数据。
- CounterComponent 有一个选择器 app-counter-component。像这样将它放在 FetchDataComponent 模板中
<app-counter-component></app-counter-component>
获取-data.component.html
<h1> Some heading</h1>
<app-counter-component></app-counter-component>
<h2> The End </h2>