以下是一个示例的Angular 8应用程序设计,包含通用对象和大型计算函数的解决方法:
// common.ts
export interface User {
id: number;
name: string;
email: string;
}
export function calculateSum(numbers: number[]): number {
return numbers.reduce((sum, num) => sum + num, 0);
}
// app.component.ts
import { Component } from '@angular/core';
import { User, calculateSum } from './common';
@Component({
selector: 'app-root',
template: `
User List
-
{{ user.name }} - {{ user.email }}
Sum: {{ sum }}
`
})
export class AppComponent {
users: User[] = [
{ id: 1, name: 'John', email: 'john@example.com' },
{ id: 2, name: 'Jane', email: 'jane@example.com' },
{ id: 3, name: 'Mike', email: 'mike@example.com' }
];
sum: number;
constructor() {
const numbers = [1, 2, 3, 4, 5];
this.sum = calculateSum(numbers);
}
}
// app.module.ts
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
@NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
标签,以在浏览器中呈现Angular应用程序。
Angular App