angular-cn/public/docs/ts/latest/guide/cheatsheet-cn.json

568 lines
24 KiB
JSON
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

{
"currentEnvironment": "TypeScript",
"version": {
"raw": "2.4.5",
"major": 2,
"minor": 4,
"patch": 6,
"prerelease": [
"local"
],
"build": "sha.a3e98d5",
"version": "2.4.6-local",
"codeName": "snapshot",
"isSnapshot": true,
"full": "2.4.6-local+sha.a3e98d5",
"branch": "master",
"commitSHA": "a3e98d5df3376b04c8c8b57b8aec05409d196074"
},
"sections": [
{
"name": "引导",
"description": "<p><code>import { platformBrowserDynamic } from &#39;@angular/platform-browser-dynamic&#39;;</code>\n</p>\n",
"items": [
{
"syntax": "platformBrowserDynamic().bootstrapModule(AppModule);",
"bold": [
"platformBrowserDynamic().bootstrapModule"
],
"description": "<p>从一个指定的<code>NgModule</code>中使用根组件引导应用</p>\n"
}
],
"index": 0
},
{
"name": "NgModules",
"description": "<p><code>import { NgModule } from &#39;@angular/core&#39;;</code>\n</p>\n",
"items": [
{
"syntax": "@NgModule({ declarations: ..., imports: ...,\n exports: ..., providers: ..., bootstrap: ...})\nclass MyModule {}",
"bold": [
"NgModule"
],
"description": "<p>定义一个模块,其中包括组件、指令、管道和提供商。</p>\n"
},
{
"syntax": "declarations: [MyRedComponent, MyBlueComponent, MyDatePipe]",
"bold": [
"declarations:"
],
"description": "<p>一个数组,包括从属于当前模块的组件、指令和管道。</p>\n"
},
{
"syntax": "imports: [BrowserModule, SomeOtherModule]",
"bold": [
"imports:"
],
"description": "<p>一个数组,包括被导入到当前模块中的所有模块。来自被导入模块的<code>declarations</code>也同样对当前模块有效。</p>\n"
},
{
"syntax": "exports: [MyRedComponent, MyDatePipe]",
"bold": [
"exports:"
],
"description": "<p>一个数组,包括对导入当前模块的模块可见的组件、指令、管道。</p>\n"
},
{
"syntax": "providers: [MyService, { provide: ... }]",
"bold": [
"providers:"
],
"description": "<p>一个数组,包括在对前模块及导入当前模块的模块中的内容物(组件、指令、管道、提供商等)可见的依赖注入提供商。</p>\n"
},
{
"syntax": "bootstrap: [MyAppComponent]",
"bold": [
"bootstrap:"
],
"description": "<p>一个数组,包括由当前模块引导时应该引导的组件</p>\n"
}
],
"index": 1
},
{
"name": "模板语法",
"description": "",
"items": [
{
"syntax": "<input [value]=\"firstName\">",
"bold": [
"[value]"
],
"description": "<p>把属性<code>value</code>绑定到表达式<code>firstName</code>的结果。</p>\n"
},
{
"syntax": "<div [attr.role]=\"myAriaRole\">",
"bold": [
"[attr.role]"
],
"description": "<p>把<code>role</code>这个Attribute绑定到表达式 <code>myAriaRole</code>的结果。</p>\n"
},
{
"syntax": "<div [class.extra-sparkle]=\"isDelightful\">",
"bold": [
"[class.extra-sparkle]"
],
"description": "<p>把元素是否出现CSS类<code>extra-sparkle</code>,绑定到一个表达式<code>isDelightful</code>的结果是否为真。</p>\n"
},
{
"syntax": "<div [style.width.px]=\"mySize\">",
"bold": [
"[style.width.px]"
],
"description": "<p>把样式的<code>width</code>属性绑定到表达式<code>mySize</code>的结果以px为单位。这个单位是可选的。</p>\n"
},
{
"syntax": "<button (click)=\"readRainbow($event)\">",
"bold": [
"(click)"
],
"description": "<p>当按钮(及其子元素)上的click事件被触发时调用<code>readRainbow</code>方法,并把事件对象作为参数传入。</p>\n"
},
{
"syntax": "<div title=\"Hello {{ponyName}}\">",
"bold": [
"{{ponyName}}"
],
"description": "<p>把属性绑定到一个插值表达式字符串,比如 &quot;Hello Seabiscuit&quot;。它等价于:\n<code>&lt;div [title]=&quot;&#39;Hello &#39; + ponyName&quot;&gt;</code></p>\n"
},
{
"syntax": "<p>Hello {{ponyName}}</p>",
"bold": [
"{{ponyName}}"
],
"description": "<p>把文本内容绑定到一个插值表达式,比如 &quot;Hello Seabiscuit&quot;.</p>\n"
},
{
"syntax": "<my-cmp [(title)]=\"name\">",
"bold": [
"[(title)]"
],
"description": "<p>设置双向数据绑定。等价于:<code>&lt;my-cmp [title]=&quot;name&quot; (titleChange)=&quot;name=$event&quot;&gt;</code></p>\n"
},
{
"syntax": "<video #movieplayer ...>\n <button (click)=\"movieplayer.play()\">\n</video>",
"bold": [
"#movieplayer",
"(click)"
],
"description": "<p>创建一个局部变量 <code>movieplayer</code> ,它提供到<code>video</code>元素实例的访问,可用于当前模板中的数据绑定和事件绑定表达式中。</p>\n"
},
{
"syntax": "<p *myUnless=\"myExpression\">...</p>",
"bold": [
"*myUnless"
],
"description": "<p><code>*</code>符号表示当前元素将被转变成一个内嵌模板。等价于:\n<code>&lt;template [myUnless]=&quot;myExpression&quot;&gt;&lt;p&gt;...&lt;/p&gt;&lt;/template&gt;</code></p>\n"
},
{
"syntax": "<p>Card No.: {{cardNumber | myCardNumberFormatter}}</p>",
"bold": [
"{{cardNumber | myCardNumberFormatter}}"
],
"description": "<p>通过名叫<code>myCardNumberFormatter</code>的管道,转换表达式的当前值<code>cardNumber</code>。</p>\n"
},
{
"syntax": "<p>Employer: {{employer?.companyName}}</p>",
"bold": [
"{{employer?.companyName}}"
],
"description": "<p>安全导航运算符(<code>?</code>)表示<code>employer</code>字段是可选的,如果它是<code>undefined</code>,表达式剩下的部分将被忽略</p>\n"
},
{
"syntax": "<svg:rect x=\"0\" y=\"0\" width=\"100\" height=\"100\"/>",
"bold": [
"svg:"
],
"description": "<p>SVG模板需要在它们的根节点上带一个<code>svg:</code>前缀以消除模板中HTML元素和SVG元素的歧义。</p>\n"
},
{
"syntax": "<svg>\n <rect x=\"0\" y=\"0\" width=\"100\" height=\"100\"/>\n</svg>",
"bold": [
"svg"
],
"description": "<p><code>&lt;svg&gt;</code>根元素在无需前缀的情况下也能被自动检测为SVG。</p>\n"
}
],
"index": 2
},
{
"name": "内置指令",
"description": "<p><code>import { CommonModule } from &#39;@angular/common&#39;;</code>\n</p>\n",
"items": [
{
"syntax": "<section *ngIf=\"showSection\">",
"bold": [
"*ngIf"
],
"description": "<p>基于showSection表达式的值移除或重新创建部分DOM树。</p>\n"
},
{
"syntax": "<li *ngFor=\"let item of list\">",
"bold": [
"*ngFor"
],
"description": "<p>把li元素及其内容转化成一个模板并用它来为列表中的每个条目初始化视图。</p>\n"
},
{
"syntax": "<div [ngSwitch]=\"conditionExpression\">\n <template [ngSwitchCase]=\"case1Exp\">...</template>\n <template ngSwitchCase=\"case2LiteralString\">...</template>\n <template ngSwitchDefault>...</template>\n</div>",
"bold": [
"[ngSwitch]",
"[ngSwitchCase]",
"ngSwitchCase",
"ngSwitchDefault"
],
"description": "<p>基于conditionExpression的当前值从内嵌模板中选取一个有条件的切换div的内容。</p>\n"
},
{
"syntax": "<div [ngClass]=\"{active: isActive, disabled: isDisabled}\">",
"bold": [
"[ngClass]"
],
"description": "<p>把一个元素上CSS类的出现与否绑定到一个真值映射表上。右侧的表达式应该返回类似{class-name: true/false}的映射表。</p>\n"
}
],
"index": 3
},
{
"name": "表单",
"description": "<p><code>import { FormsModule } from &#39;@angular/forms&#39;;</code>\n</p>\n",
"items": [
{
"syntax": "<input [(ngModel)]=\"userName\">",
"bold": [
"[(ngModel)]"
],
"description": "<p>提供双向绑定,为表单控件提供解析和验证。</p>\n"
}
],
"index": 4
},
{
"name": "类装饰器",
"description": "<p><code>import { Directive, ... } from &#39;@angular/core&#39;;</code>\n</p>\n",
"items": [
{
"syntax": "@Component({...})\nclass MyComponent() {}",
"bold": [
"@Component({...})"
],
"description": "<p>声明当前类是一个组件,并提供关于该组件的元数据。</p>\n"
},
{
"syntax": "@Directive({...})\nclass MyDirective() {}",
"bold": [
"@Directive({...})"
],
"description": "<p>声明当前类是一个指令,并提供关于该指令的元数据。</p>\n"
},
{
"syntax": "@Pipe({...})\nclass MyPipe() {}",
"bold": [
"@Pipe({...})"
],
"description": "<p>声明当前类是一个管道,并且提供关于该管道的元数据。</p>\n"
},
{
"syntax": "@Injectable()\nclass MyService() {}",
"bold": [
"@Injectable()"
],
"description": "<p>声明当前类有一些依赖,当依赖注入器创建该类的实例时,这些依赖应该被注入到构造函数中。\n\n</p>\n"
}
],
"index": 5
},
{
"name": "指令配置",
"description": "<p><code>@Directive({ property1: value1, ... })</code>\n</p>\n",
"items": [
{
"syntax": "selector: '.cool-button:not(a)'",
"bold": [
"selector:"
],
"description": "<p>指定一个CSS选择器以便在模板中找出该指令。支持的选择器包括<code>element</code>,\n<code>[attribute]</code>, <code>.class</code>, 和 <code>:not()</code>。</p>\n<p>不支持父子关系选择器。</p>\n"
},
{
"syntax": "providers: [MyService, { provide: ... }]",
"bold": [
"providers:"
],
"description": "<p>为当前指令及其子指令提供依赖注入的providers数组。</p>\n"
}
],
"index": 6
},
{
"name": "组件配置",
"description": "<p>\n<code>@Component</code>扩展了<code>@Directive</code>,\n以便<code>@Directive</code>中的配置项也能用在组件上</p>\n",
"items": [
{
"syntax": "moduleId: module.id",
"bold": [
"moduleId:"
],
"description": "<p>如果设置了,<code>templateUrl</code>和<code>styleUrl</code>会被解析成相对于组件的。</p>\n"
},
{
"syntax": "viewProviders: [MyService, { provide: ... }]",
"bold": [
"viewProviders:"
],
"description": "<p>依赖注入provider的数组局限于当前组件的视图中。</p>\n"
},
{
"syntax": "template: 'Hello {{name}}'\ntemplateUrl: 'my-component.html'",
"bold": [
"template:",
"templateUrl:"
],
"description": "<p>当前组件视图的内联模板或外部模板地址</p>\n"
},
{
"syntax": "styles: ['.primary {color: red}']\nstyleUrls: ['my-component.css']",
"bold": [
"styles:",
"styleUrls:"
],
"description": "<p>内联CSS样式或外部样式表URL的列表用于给组件的视图添加样式。</p>\n"
}
],
"index": 7
},
{
"name": "供指令类或组件类用的字段装饰器。",
"description": "<p><code>import { Input, ... } from &#39;@angular/core&#39;;</code>\n</p>\n",
"items": [
{
"syntax": "@Input() myProperty;",
"bold": [
"@Input()"
],
"description": "<p>声明一个输入属性,以便我们可以通过属性绑定更新它。(比如:\n<code>&lt;my-cmp [my-property]=&quot;someExpression&quot;&gt;</code>).</p>\n"
},
{
"syntax": "@Output() myEvent = new EventEmitter();",
"bold": [
"@Output()"
],
"description": "<p>声明一个输出属性,以便我们可以通过事件绑定进行订阅。(比如:<code>&lt;my-cmp (my-event)=&quot;doSomething()&quot;&gt;</code>).</p>\n"
},
{
"syntax": "@HostBinding('class.valid') isValid;",
"bold": [
"@HostBinding('class.valid')"
],
"description": "<p>把宿主元素的属性(比如CSS类<code>valid</code>)绑定到指令/组件的属性(比如:<code>isValid</code>)。</p>\n"
},
{
"syntax": "@HostListener('click', ['$event']) onClick(e) {...}",
"bold": [
"@HostListener('click', ['$event'])"
],
"description": "<p>通过指令/组件的方法(例如<code>onClick</code>)订阅宿主元素的事件(例如<code>click</code>),可选传入一个参数(<code>$event</code>)。</p>\n"
},
{
"syntax": "@ContentChild(myPredicate) myChildComponent;",
"bold": [
"@ContentChild(myPredicate)"
],
"description": "<p>把组件内容查询(<code>myPredicate</code>)的第一个结果绑定到类的<code>myChildComponent</code>属性。</p>\n"
},
{
"syntax": "@ContentChildren(myPredicate) myChildComponents;",
"bold": [
"@ContentChildren(myPredicate)"
],
"description": "<p>把组件内容查询(<code>myPredicate</code>)的全部结果,绑定到类的<code>myChildComponents</code>属性。</p>\n"
},
{
"syntax": "@ViewChild(myPredicate) myChildComponent;",
"bold": [
"@ViewChild(myPredicate)"
],
"description": "<p>把组件视图查询(<code>myPredicate</code>)的第一个结果绑定到类的<code>myChildComponent</code>属性。对指令无效。</p>\n"
},
{
"syntax": "@ViewChildren(myPredicate) myChildComponents;",
"bold": [
"@ViewChildren(myPredicate)"
],
"description": "<p>把组件视图查询(<code>myPredicate</code>)的全部结果绑定到类的<code>myChildComponents</code>属性。对指令无效。</p>\n"
}
],
"index": 8
},
{
"name": "指令和组件的变更检测与生命周期钩子",
"description": "<p>(作为类方法实现)\n</p>\n",
"items": [
{
"syntax": "constructor(myService: MyService, ...) { ... }",
"bold": [
"constructor(myService: MyService, ...)"
],
"description": "<p>类的构造函数会在所有其它生命周期钩子之前调用。使用它来注入依赖,但是要避免用它做较重的工作。</p>\n"
},
{
"syntax": "ngOnChanges(changeRecord) { ... }",
"bold": [
"ngOnChanges(changeRecord)"
],
"description": "<p>在输入属性每次变化了之后、开始处理内容或子视图之前被调用。</p>\n"
},
{
"syntax": "ngOnInit() { ... }",
"bold": [
"ngOnInit()"
],
"description": "<p>在执行构造函数、初始化输入属性、第一次调用完<code>ngOnChanges</code>之后调用。</p>\n"
},
{
"syntax": "ngDoCheck() { ... }",
"bold": [
"ngDoCheck()"
],
"description": "<p>每当检查组件或指令的输入属性是否变化时调用。通过它,可以用自定义的检查方式来扩展变更检测逻辑。</p>\n"
},
{
"syntax": "ngAfterContentInit() { ... }",
"bold": [
"ngAfterContentInit()"
],
"description": "<p>当组件或指令的内容已经初始化、<code>ngOnInit</code>完成之后调用。</p>\n"
},
{
"syntax": "ngAfterContentChecked() { ... }",
"bold": [
"ngAfterContentChecked()"
],
"description": "<p>在每次检查完组件或指令的内容之后调用。</p>\n"
},
{
"syntax": "ngAfterViewInit() { ... }",
"bold": [
"ngAfterViewInit()"
],
"description": "<p>当组件的视图已经初始化完毕,每次<code>ngAfterContentInit</code>之后被调用。只适用于组件。</p>\n"
},
{
"syntax": "ngAfterViewChecked() { ... }",
"bold": [
"ngAfterViewChecked()"
],
"description": "<p>每次检查完组件的视图之后调用。只适用于组件。</p>\n"
},
{
"syntax": "ngOnDestroy() { ... }",
"bold": [
"ngOnDestroy()"
],
"description": "<p>在所属实例被销毁前,只调用一次。</p>\n"
}
],
"index": 9
},
{
"name": "依赖注入配置",
"description": "",
"items": [
{
"syntax": "{ provide: MyService, useClass: MyMockService }",
"bold": [
"provide",
"useClass"
],
"description": "<p>把<code>MyService</code>类的提供商设置或改写为<code>MyMockService</code>。</p>\n"
},
{
"syntax": "{ provide: MyService, useFactory: myFactory }",
"bold": [
"provide",
"useFactory"
],
"description": "<p>把<code>MyService</code>的提供商设置或改写为<code>myFactory</code>工厂函数。</p>\n"
},
{
"syntax": "{ provide: MyValue, useValue: 41 }",
"bold": [
"provide",
"useValue"
],
"description": "<p>把<code>MyValue</code>的提供商设置或改写为值<code>41</code>。</p>\n"
}
],
"index": 10
},
{
"name": "路由与导航",
"description": "<p><code>import { Routes, RouterModule, ... } from &#39;@angular/router&#39;;</code>\n</p>\n",
"items": [
{
"syntax": "const routes: Routes = [\n { path: '', component: HomeComponent },\n { path: 'path/:routeParam', component: MyComponent },\n { path: 'staticPath', component: ... },\n { path: '**', component: ... },\n { path: 'oldPath', redirectTo: '/staticPath' },\n { path: ..., component: ..., data: { message: 'Custom' } }\n]);\n\nconst routing = RouterModule.forRoot(routes);",
"bold": [
"Routes"
],
"description": "<p>为应用配置路由。支持静态、参数化、重定向和通配符路由。还支持自定义路由数据和解析。</p>\n"
},
{
"syntax": "\n<router-outlet></router-outlet>\n<router-outlet name=\"aux\"></router-outlet>\n",
"bold": [
"router-outlet"
],
"description": "<p>标记一个位置,用于加载当前激活路由的组件。</p>\n"
},
{
"syntax": "\n<a routerLink=\"/path\">\n<a [routerLink]=\"[ '/path', routeParam ]\">\n<a [routerLink]=\"[ '/path', { matrixParam: 'value' } ]\">\n<a [routerLink]=\"[ '/path' ]\" [queryParams]=\"{ page: 1 }\">\n<a [routerLink]=\"[ '/path' ]\" fragment=\"anchor\">\n",
"bold": [
"[routerLink]"
],
"description": "<p>基于路由指令创建指向不同视图的链接由路由路径、必选参数、可选参数、查询参数和片段fragment组成。添加“<code>/</code>”前缀可以导航到根路由。添加“<code>./</code>”前缀可以导航到子路由。添加“<code>../</code>”前缀可以导航到兄弟路由或父路由。</p>\n"
},
{
"syntax": "<a [routerLink]=\"[ '/path' ]\" routerLinkActive=\"active\">",
"bold": [],
"description": "<p>当<code>routerLink</code>被激活时就把指定的CSS类添加到该元素上。</p>\n"
},
{
"syntax": "class CanActivateGuard implements CanActivate {\n canActivate(\n route: ActivatedRouteSnapshot,\n state: RouterStateSnapshot\n ): Observable<boolean>|Promise<boolean>|boolean { ... }\n}\n\n{ path: ..., canActivate: [CanActivateGuard] }",
"bold": [
"CanActivate"
],
"description": "<p>一个用来定义类的接口,路由器会首先调用它来决定是否应该激活该组件。应该返回布尔值或能解析为布尔值的可观察对象(Observable)或承诺(Promise)。</p>\n"
},
{
"syntax": "class CanDeactivateGuard implements CanDeactivate<T> {\n canDeactivate(\n component: T,\n route: ActivatedRouteSnapshot,\n state: RouterStateSnapshot\n ): Observable<boolean>|Promise<boolean>|boolean { ... }\n}\n\n{ path: ..., canDeactivate: [CanDeactivateGuard] }",
"bold": [
"CanDeactivate"
],
"description": "<p>一个用来定义类的接口,路由器在导航后会首先调用它来决定是否应该取消该组件的激活状态。应该返回布尔值或能解析为布尔值的可观察对象(Observable)或承诺(Promise)。</p>\n"
},
{
"syntax": "class CanActivateChildGuard implements CanActivateChild {\n canActivateChild(\n route: ActivatedRouteSnapshot,\n state: RouterStateSnapshot\n ): Observable<boolean>|Promise<boolean>|boolean { ... }\n}\n\n{ path: ..., canActivateChild: [CanActivateGuard],\n children: ... }",
"bold": [
"CanActivateChild"
],
"description": "<p>一个用来定义类的接口,路由器会首先调用它来决定是否应该激活该子路由。应该返回布尔值或能解析为布尔值的可观察对象(Observable)或承诺(Promise)。\n"
},
{
"syntax": "class ResolveGuard implements Resolve<T> {\n resolve(\n route: ActivatedRouteSnapshot,\n state: RouterStateSnapshot\n ): Observable<any>|Promise<any>|any { ... }\n}\n\n{ path: ..., resolve: [ResolveGuard] }",
"bold": [
"Resolve"
],
"description": "<p>一个用来定义类的接口,路由器会在渲染该路由之前先调用它来解析路由数据。应该返回一个值或能解析为值的可观察对象(Observable)或承诺(Promise)。</p>\n"
},
{
"syntax": "class CanLoadGuard implements CanLoad {\n canLoad(\n route: Route\n ): Observable<boolean>|Promise<boolean>|boolean { ... }\n}\n\n{ path: ..., canLoad: [CanLoadGuard], loadChildren: ... }",
"bold": [
"CanLoad"
],
"description": "<p>一个用来定义类的接口,路由器会首先调用它来决定一个惰性加载的模块是否应该被加载。应该返回布尔值或能解析为布尔值的可观察对象(Observable)或承诺(Promise)。</p>\n"
}
],
"index": 11
}
]
}