javascript - Angular 应用程序编译成功,但在“ng build --prod”中不存在提供错误属性
问题描述
Angular 应用程序编译成功,但在“ng build --prod”中出现以下错误
ERROR in src\app\header\header.component.html(31,124): : Property 'searchText' does not exist on type 'HeaderComponent'.
src\app\dashboard\dashboard.component.html(3,72): : Property 'newsService' is private and only accessible within class 'DashboardComponent'.
src\app\dashboard\dashboard.component.html(3,72): : Property 'p' does not exist on type 'DashboardComponent'.
src\app\dashboard\dashboard.component.html(29,46): : Property 'p' does not exist on type 'DashboardComponent'.
我在我的 html 文件中使用了这些属性,如下所示
header.component.html
:
<input type="text" class="form-control mr-2 align-self-center" required placeholder="Search" name="searchText" [ngModel]="searchText" value="">
dashboard.component.html
文件
<pagination-controls class="text-center" (pageChange)="p = $event"></pagination-controls>
我的header.component.html
档案
import { Component, OnInit, Output, EventEmitter, ViewEncapsulation } from '@angular/core';
import { NgForm } from '@angular/forms';
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {
filterText : string;
@Output() search = new EventEmitter();
@Output() filterButton = new EventEmitter();
constructor() { }
ngOnInit() {
}
onSubmit(form : NgForm)
{
console.log(form);
this.search.emit(form);
}
filterClicked($event)
{
this.filterText = $event.target.text;
this.filterButton.emit(this.filterText);
}
}
我的dashboard.component.html
档案
import { Component, OnInit, ViewEncapsulation } from '@angular/core';
import { NewsService } from '../shared/news.service';
import { NewsModel } from '../shared/news.model';
import { Form } from '@angular/forms';
import { Pipe, PipeTransform } from '@angular/core';
import { element } from 'protractor';
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {
articles : any;
temp : NewsModel = new NewsModel;
constructor(private newsService : NewsService) { }
ngOnInit() {
this.FetchHeadlines();
}
FetchHeadlines()
{
this.newsService.GetAllGaurdian()
.subscribe((result) =>
{
this.articles = result;
this.articles.response.results.forEach(element => {
this.newsService.newsArticles.push(this.newsService.CusotomMapper(element));
});
})
}
}
无法准确找出错误在哪里!
解决方案
我认为错误描述尽可能准确。他们每个人都告诉您您的组件有问题,让我们检查每个人
错误:
ERROR in src\app\header\header.component.html(31,124): : Property 'searchText' does not exist on type 'HeaderComponent'.
您在HTML 中有searchTextHeaderComponent
,但在组件本身中没有
解决方案:将 searchText 变量添加到组件
@Component({
selector: 'app-header',
templateUrl: './header.component.html',
styleUrls: ['./header.component.css']
})
export class HeaderComponent implements OnInit {
searchText:string
...
}
错误 :
src\app\dashboard\dashboard.component.html(3,72): : Property 'newsService' is private and only accessible within class 'DashboardComponent'.
您在模板中使用的所有字段,必须是组件本身内部的公共字段,否则将无法编译
解决方案:在 newService 处将 private 修饰符更改为 public
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {
constructor(public newsService : NewsService) { }
...
}
错误:
src\app\dashboard\dashboard.component.html(3,72): : Property 'p' does not exist on type 'DashboardComponent'.
src\app\dashboard\dashboard.component.html(29,46): : Property 'p' does not exist on type 'DashboardComponent'.
与 HeaderComponent 相同。您正在使用p字段,但未在 DashboardComponent 中定义
解决方案:将 p 字段添加到仪表板组件
@Component({
selector: 'app-dashboard',
templateUrl: './dashboard.component.html',
styleUrls: ['./dashboard.component.css']
})
export class DashboardComponent implements OnInit {
p:any
...
}
推荐阅读
- regex - 如何通过 typeorm 和 nest.js 中的正则表达式正确设置验证
- python - 如果满足条件,将列表系列中的 -n 元素附加到另一个列表系列
- jquery - 获取 img 的 src 值并将另一个元素作为背景图像
- c# - ScrollView 在 Xamarin Forms 中的另一个 ScrollView 内
- javascript - React Native:没有创建 Firebase App '[DEFAULT]' - 调用 Firebase App.initializeApp() (app/no-app)
- python - matplotlib plot_surface 颜色图不随 z 轴缩放
- reactjs - 单击anychart中的任何堆积柱形图时如何获取系列名称?
- c - GStreamer:根据条件将数据写入文件的管道
- c# - 在 wpf .net 核心应用程序中以应用程序为中心的 StackPanel 的特定元素
- arrays - 根据id重新排列php多维数组