Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions docs/decorator-legacy.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

装饰器的旧语法与标准语法,有相当大的差异。旧语法以后会被淘汰,但是目前大量现有项目依然在使用它,本章就介绍旧语法下的装饰器。

## experimentalDecorators 编译选项
## experimentalDecorators 编译选项

使用装饰器的旧语法,需要打开`--experimentalDecorators`编译选项。

Expand All @@ -21,7 +21,8 @@ $ tsc --target ES5 --experimentalDecorators
"compilerOptions": {
"target": "ES6",
"experimentalDecorators": true,
"emitDecoratorMetadata": true
"emitDecoratorMetadata": true,
"useDefineForClassFields": false,
}
}
```
Expand Down Expand Up @@ -123,7 +124,7 @@ class A {}
class BugReport {
type = "report";
title: string;

constructor(t:string) {
this.title = t;
}
Expand Down Expand Up @@ -255,7 +256,7 @@ class Greeter {
constructor(message:string) {
this.greeting = message;
}

@enumerable(false)
greet() {
return 'Hello, ' + this.greeting;
Expand Down Expand Up @@ -397,10 +398,10 @@ function Min(limit:number) {

class User {
username: string;

@Min(8)
password: string;

constructor(username: string, password: string){
this.username = username;
this.password = password;
Expand Down Expand Up @@ -455,12 +456,12 @@ class Point {
this._x = x;
this._y = y;
}

@configurable(false)
get x() {
return this._x;
}

@configurable(false)
get y() {
return this._y;
Expand All @@ -480,7 +481,7 @@ function validator(
){
const originalGet = descriptor.get;
const originalSet = descriptor.set;

if (originalSet) {
descriptor.set = function (val) {
if (val > 100) {
Expand Down Expand Up @@ -610,9 +611,9 @@ c.member(5, 5);
执行装饰器时,按照如下顺序执行。

1. 实例相关的装饰器。
1. 静态相关的装饰器。
1. 构造方法的参数装饰器。
1. 类装饰器。
2. 静态相关的装饰器。
3. 构造方法的参数装饰器。
4. 类装饰器。

请看下面的示例。

Expand All @@ -627,7 +628,7 @@ function f(key:string):any {
class C {
@f('静态方法')
static method() {}

@f('实例方法')
method() {}

Expand Down Expand Up @@ -828,4 +829,3 @@ x
- [Deep introduction to property decorators in TypeScript](https://techsparx.com/nodejs/typescript/decorators/properties.html), by David Herron
- [Deep introduction to accessor decorators in TypeScript](https://techsparx.com/nodejs/typescript/decorators/accessors.html), by David Herron
- [Using Property Decorators in Typescript with a real example](https://dev.to/danywalls/using-property-decorators-in-typescript-with-a-real-example-44e), by Dany Paredes