简体   繁体   English

如何在 Visual Studio 中使用默认 angular 项目导入组件

[英]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导入和使用此组件的正确方法是什么

  1. Make sure that both components are in a modules declarations array.确保两个组件都在模块声明数组中。
  2. Your FetchDataComponent gonna be a parent component, CounterComponent gonna be it's child, meaning that it will be inside the parents template, that way they can interact with each other and share data.您的 FetchDataComponent 将是一个父组件, CounterComponent 将是它的子组件,这意味着它将位于父模板内,这样它们就可以相互交互并共享数据。
  3. CounterComponent has a selector app-counter-component. CounterComponent 有一个选择器应用程序计数器组件。 Place it in a FetchDataComponent template like so <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.

相关问题 如何在Mac上使用Visual Studio Code将Azure发布设置文件导入Angular项目? - How to import an Azure publishsettings file to an Angular project using Visual Studio Code on mac? 将 Angular 2 项目导入到 Visual Studio 2017 - Import Angular 2 project to Visual Studio 2017 如何使用Visual Studio Code将Visual Studio 2015中开发的Angular 4项目用于Angular 6? - How to use Angular 4 project developed in Visual Studio 2015 to Angular 6 using Visual Studio Code? 如何使用Visual Studio 2017/2015与DevExtreme进行Angular 2项目 - How to do Angular 2 project with DevExtreme using Visual Studio 2017/2015 Visual Studio Code Angular quickfix import 停止在特定项目上工作 - Visual Studio Code Angular quickfix import stopped working on particular project 使用Angular 6模板后如何在Visual Studio中将CSS绑定还原到Angular 8项目 - How to restore CSS bundling to Angular 8 project in Visual Studio after using Angular 6 template Visual Studio-Angular 2项目结构 - Visual studio - Angular 2 project structure 如何使用Yarn而不是Npm发布Visual Studio Angular项目? - How do I publish my Visual Studio Angular project using Yarn instead of Npm? 如何使用 Visual Studio 2019 在 ASP.NET MVC 中集成 Angular 8 项目 - How to integrate Angular 8 project in ASP.NET MVC using Visual Studio 2019 尝试使用 Visual Studio 2022 创建我的第一个 Angular 项目 - Trying to create my first Angular project using Visual Studio 2022
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM