简体   繁体   中英

Member should be declared before all private instance

private userSubject$: BehaviorSubject<UserModel> = new BehaviorSubject<UserModel>(null);
userChanged$: Observable<UserModel> = this.userSubject$.asObservable();

It says:

Member userChanged$ should be declared before all private instance field definitions.eslint@typescript-eslint/member-ordering

I like this rule. But how can I use the above code without disabling this rule?

If I'll change the order then it says:

Property 'userSubject$' is used before its initialization.

You can move the initialization into the constructor:

userChanged$: Observable<UserModel>;

private userSubject$: BehaviorSubject<UserModel>;

constructor() {
    this.userSubject$ = new BehaviorSubject<UserModel>(null);
    this.userChanged$ = this.userSubject$.asObservable();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM