假设我有一个这样的组件
原始代码:
import { HttpClient } from '@angular/common/http';
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `<pre>{{ response | json }}</pre>`,
styleUrls: ['./app.component.css'],
})
export class AppComponent {
constructor(private _http: HttpClient) {
this.fetchUsers();
}
response!: any;
fetchUsers() {
this._http.get(`https://jsonplaceholder.typicode.com/users`).subscribe(
(resp) => {
console.log(resp);
this.response = resp;
},
(err) => {
console.error(err);
this.response = err;
},
() => {
console.log('Subscription Complete');
}
);
}
}
我有以下方法来重构上面的代码...
重构方法1:
import { HttpClient } from '@angular/common/http';
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `<pre>{{ response | json }}</pre>`,
styleUrls: ['./app.component.css'],
})
export class AppComponent {
constructor(private _http: HttpClient) {
this.fetchUsers();
}
response!: any;
onSubscribe = (resp: any) => {
console.log(resp);
this.response = resp;
};
onError = (err: any) => {
console.error(err);
this.response = err;
};
onCompletion = () => {
console.log('Subscription Complete');
};
fetchUsers() {
this._http
.get(`https://jsonplaceholder.typicode.com/users`)
.subscribe(this.onSubscribe, this.onError, this.onCompletion);
}
}
重构方法2:
import { HttpClient } from '@angular/common/http';
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `<pre>{{ response | json }}</pre>`,
styleUrls: ['./app.component.css'],
})
export class AppComponent {
constructor(private _http: HttpClient) {
this.fetchUsers();
}
response!: any;
onSubscribe(resp: any) {
console.log(resp);
this.response = resp;
}
onError(err: any) {
console.error(err);
this.response = err;
}
onCompletion() {
console.log('Subscription Complete');
}
fetchUsers() {
this._http.get(`https://jsonplaceholder.typicode.com/users`).subscribe(
(resp) => this.onSubscribe(resp),
(err) => this.onError(err),
() => this.onCompletion()
);
}
}
重构方法3:
import { HttpClient } from '@angular/common/http';
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `<pre>{{ response | json }}</pre>`,
styleUrls: ['./app.component.css'],
})
export class AppComponent {
constructor(private _http: HttpClient) {
this.fetchUsers();
}
response!: any;
fetchUsers() {
this._http.get(`https://jsonplaceholder.typicode.com/users`).subscribe({
next: (resp: any) => {
console.log(resp);
this.response = resp;
},
error: (err: any) => {
console.error(err);
this.response = err;
},
complete: () => {
console.log('Subscription Complete');
},
});
}
}
重构方法4:
import { HttpClient } from '@angular/common/http';
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `<pre>{{ response | json }}</pre>`,
styleUrls: ['./app.component.css'],
})
export class AppComponent {
constructor(private _http: HttpClient) {
this.fetchUsers();
}
response!: any;
onSubscribe = (resp: any) => {
console.log(resp);
this.response = resp;
};
onError = (err: any) => {
console.error(err);
this.response = err;
};
onCompletion = () => {
console.log('Subscription Complete');
};
fetchUsers() {
this._http.get(`https://jsonplaceholder.typicode.com/users`).subscribe({
next: this.onSubscribe,
error: this.onError,
complete: this.onCompletion,
});
}
}
重构方法5:
import { HttpClient } from '@angular/common/http';
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `<pre>{{ response | json }}</pre>`,
styleUrls: ['./app.component.css'],
})
export class AppComponent {
constructor(private _http: HttpClient) {
this.fetchUsers();
}
response!: any;
onSubscribe(resp: any) {
console.log(resp);
this.response = resp;
}
onError(err: any) {
console.error(err);
this.response = err;
}
onCompletion() {
console.log('Subscription Complete');
}
fetchUsers() {
this._http.get(`https://jsonplaceholder.typicode.com/users`).subscribe({
next: (resp: any) => this.onSubscribe(resp),
error: (err: any) => this.onError(err),
complete: () => this.onCompletion(),
});
}
}
"现在的问题是"
考虑到性能(首先是可读性)-哪一个是最好的选择?
1.重构方法1?
1.重构方法2?
1.重构方法3?
1.重构方法4?
1.重构方法5?
1.原始代码?
2条答案
按热度按时间s71maibg1#
我认为最好的方法是数字4,原因如下:
谈到可读性,在订阅上传递三个参数(next、error、complete)的方式实际上对于RxJS来说是不推荐的,因为函数参数可能会导致代码难以阅读。You can read more about it here
相反,RxJS建议您使用一个JS对象,就像一个参数,您可以在其中定义不同回调的代码
并且将代码分成函数可以帮助您使其更易于阅读。
谈到性能,如果您向订阅传递一个参数(如JS对象),RxJS会向其中一个回调传递一个空函数,而不是您自己执行此操作。
6rqinv9w2#
就我个人而言,我会重构为声明式方法
1.创建一个服务来封装所有的数据访问。
1.在服务中,定义一个变量(而不是方法)来管理从http get返回的Observable。
1.在组件中,定义一个变量(不是构造函数或生命周期钩子)来管理从服务返回的Observable
1.使用样板中的
async
管道。示例服务:
组件/模板示例
有关此声明性模式的更多信息,请参见:https://youtu.be/0XPxUa8u-LY