新建
ng new pinduoduo
ng new pinduoduo --skip-install --style css --routing false
ng new --help
ng help
typescript interface笔记
app.component.ts
import { Component } from '@angular/core';
interface TopMenu {
title: string;
readonly link?: string;
}
type AddFunc = (x: number, y: number) => number;
interface Dict {
[key: string]: string;
}
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = '拼多多';
menus: TopMenu[] = [
{
title: '热门',
link: ''
}
];
dict: Dict = {
a: '1',
b: '2'
}
constructor() {
this.test();
}
add: AddFunc = (x, y) => x + y;
test(): void {
console.log(this.dict.a);
}
}
app.component.html
<ul>
<li *ngFor="let item of menus"><a href="#">{{item.title}}</a></li>
</ul>
ngFor
<ul>
<li *ngFor="let item of menus;let i=index; trackBy:identify">
<a
href="#"
[class.active]="i === selectedIndex"
(click)="handleSelection(i)"
>
{{item.title}}
</a>
</li>
</ul>
identify( index: any, item: TopMenu): string {
return item.title;
}
创建组件命令
ng generate componment 组件名(驼峰形式)
ng g c ScrollableTab
组件生命周期
双向绑定
组件页面
//视图
<input type="text" [value]="username" (input)="handleInput($event)">
{{username}}
//代码
@Output() usernameChange = new EventEmitter()
// tslint:disable-next-line:variable-name
_username = ''
constructor() { }
ngOnInit(): void {}
@Input()
public set username(value: string) {
this.usernameChange.emit(value);
this._username = value;
}
public get username(): string{
return this._username;
}
handleInput(ev: any): void {
console.log(ev);
this.username = ev.target.value;
}
主页
<app-horizontal-grid [(username)]="username"></app-horizontal-grid>
username = '123'
依赖注入