我在尝试在浏览器中呈现新组件时遇到一些问题。我已经使用 Angular CLI 创建了一个新项目。我还创建了一个名为list-employees
的组件。
当我执行ng serve -o
时,项目已成功编译,但浏览器未显示任何内容(IE和Google Chrome均为空白)。即使Chrome中的Web控制台也不会显示任何错误消息,而是打印 Angular在开发模式下运行。调用enableProdMode()以启用生产模式。
如果我更改 app.component.html 中的内容:
<app-list-employees></app-list-employees>
而我改用静态HTML:
<p>Hello world</p>
浏览器按预期显示内容。我觉得这很奇怪,因为我没有收到任何错误。请您能帮我了解为什么会这样吗?
以下是 package.json 文件的内容:
{
"name": "crud",
"version": "0.0.0",
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"test": "ng test",
"lint": "ng lint",
"e2e": "ng e2e"
},
"private": true,
"dependencies": {
"@angular/animations": "~8.2.12",
"@angular/common": "~8.2.12",
"@angular/compiler": "~8.2.12",
"@angular/core": "~8.2.12",
"@angular/forms": "~8.2.12",
"@angular/platform-browser": "~8.2.12",
"@angular/platform-browser-dynamic": "~8.2.12",
"@angular/router": "~8.2.12",
"bootstrap": "^3.4.1",
"rxjs": "~6.4.0",
"tslib": "^1.10.0",
"zone.js": "~0.9.1"
},
"devDependencies": {
"@angular-devkit/build-angular": "~0.803.15",
"@angular/cli": "~8.3.15",
"@angular/compiler-cli": "~8.2.12",
"@angular/language-service": "~8.2.12",
"@types/node": "~8.9.4",
"@types/jasmine": "~3.3.8",
"@types/jasminewd2": "~2.0.3",
"codelyzer": "^5.0.0",
"jasmine-core": "~3.4.0",
"jasmine-spec-reporter": "~4.2.1",
"karma": "~4.1.0",
"karma-chrome-launcher": "~2.2.0",
"karma-coverage-istanbul-reporter": "~2.0.1",
"karma-jasmine": "~2.0.1",
"karma-jasmine-html-reporter": "^1.4.0",
"protractor": "~5.4.0",
"ts-node": "~7.0.0",
"tslint": "~5.15.0",
"typescript": "~3.5.3"
}
}
在这里,当我调用列表雇员组件时,来自app.componen.html的输出:
<body>
<app-root _nghost-xoo-c0="" ng-version="8.2.12">
<app-list-employees _ngcontent-xoo-c0="" _nghost-xoo-c1="">
<!--bindings={}-->
</app-list-employees>
</app-root>
<script src="runtime.js" type="module"></script>
<script src="polyfills.js" type="module"></script>
<script src="styles.js" type="module"></script>
<script src="vendor.js" type="module"></script>
<script src="main.js" type="module"></script>
</body>
已更新
以下是相应的代码:
list-employees.component.ts
import { Component, OnInit } from '@angular/core';
import { Employee } from '../models/employee.model';
@Component({
selector: 'app-list-employees',
templateUrl: './list-employees.component.html',
styleUrls: ['./list-employees.component.css']
})
export class ListEmployeesComponent implements OnInit {
employee: Employee[] = [
{
id: 1,
name: 'Mark',
gender: 'Male',
contactPreference: 'Email',
email: 'mark@pragimtech.com',
dateOfBirth: new Date('10/25/1988'),
department: 'IT',
isActive: true,
photoPath: 'assets/images/mark.png'
},
{
id: 2,
name: 'Mary',
gender: 'Female',
contactPreference: 'Phone',
phoneNumber: '2345978640',
dateOfBirth: new Date('11/20/1979'),
department: 'HR',
isActive: true,
photoPath: 'assets/images/mary.png'
},
{
id: 3,
name: 'John',
gender: 'Male',
contactPreference: 'Phone',
phoneNumber: '5432978640',
dateOfBirth: new Date('3/25/1976'),
department: 'IT',
isActive: false,
photoPath: 'assets/images/john.png'
},
];
constructor() { }
ngOnInit() {
}
}
list-employees.component.html
<div class="panel panel-primary" *ngFor="let employee of employees">
<div class="panel-heading">
<h3 class="panel-title">{{employee.name}}</h3>
</div>
<div class="panel-body">
<div class="col-xs-10">
<div class="row vertical-align">
<div class="col-xs-4">
<img class="imageSize" [src]="employee.photoPath"/>
</div>
<div class="col-xs-8">
<div class="row">
<div class="col-xs-6">
Gender:
</div>
<div class="col-xs-6">
{{employee.gender}}
</div>
</div>
<div class="row">
<div class="col-xs-6">
Birth of date:
</div>
<div class="col-xs-6">
{{employee.dateOfBirth | date}}
</div>
</div>
<div class="row">
<div class="col-xs-6">
Contact Preference:
</div>
<div class="col-xs-6">
{{employee.contactPreference}}
</div>
</div>
<div class="row">
<div class="col-xs-6">
Phone:
</div>
<div class="col-xs-6">
{{employee.phoneNumber}}
</div>
</div>
<div class="row">
<div class="col-xs-6">
Email:
</div>
<div class="col-xs-6">
{{employee.email}}
</div>
</div>
<div class="row">
<div class="col-xs-6">
Department:
</div>
<div class="col-xs-6">
{{employee.department}}
</div>
</div>
<div class="row">
<div class="col-xs-6">
Is active:
</div>
<div class="col-xs-6">
{{employee.isActive}}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
app.component.html
<app-list-employees></app-list-employees>
app.module.ts
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { ListEmployeesComponent } from './employees/list-employees.component';
@NgModule({
declarations: [
AppComponent,
ListEmployeesComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
答案 0 :(得分:3)
问题在于,在ListEmployeesComponent
中,您正在循环使用employees
属性,该属性不存在。
相反,有一个错误命名的employee属性(不带s),其中包含数组。
我建议安装Angular Language Service插件(如果您的文本编辑器中提供该插件,我将使用VS Code),这会捕获到此类错误,并用红色突出显示了令人讨厌的行。