简体   繁体   English

如何从 angular 的构造函数中为变量赋值?

[英]How I assign a value to variable from constructor in angular?

Problem with my Angular app.我的 Angular 应用程序出现问题。 This is my first angular web.这是我的第一个 angular web。 I want to assign a value to my variable named _name from the constructor.我想从构造函数中为名为_name的变量赋值。 I tried the below way but didn't work.我尝试了以下方法,但没有奏效。 How can I assign a value for that?我怎样才能为此分配一个值? The value comes to that page in the console.log the value is displayed.该值出现在console.log中的该页面,该值被显示。 I want to assign that value to my _name variable and display the value in the HTML page when the page load.我想将该值分配给我的_name变量,并在页面加载时在 HTML 页面中显示该值。 How can I do that?我怎样才能做到这一点?

This is my current code这是我当前的代码

export class DashboardTemplateComponent implements OnInit, OnDestroy {
    private userSub: Subscription;
    inAuthenticate = false;
     _name: any;
      constructor(private router: Router, private authService: userService ) {
        this.userSub = this.authService.user.subscribe(user => {
          console.log(user.token);
          return this._name = user.name;
        });
      }
    
      ngOnInit(): void {
        this.userSub = this.authService.user.subscribe(user => {
          this.inAuthenticate = !user ? false : true;
    
        return this._name = user.name;
        });
      }

代码截图

I would rewrite your code like below avoiding complication我会像下面这样重写您的代码,以避免复杂化

import { Subject } from 'rxjs';
import { takeUntil, tap } from 'rxjs/operators';


  export class DashboardTemplateComponent implements OnInit {
    destroyed$ = new Subject();
    inAuthenticate = false;
    _name: any;
    name$ = this.authService.user.pipe(
        tap(user => this._name = user.name),
        tap(user => this.inAuthenticate = !user ? false : true)
        takeUntil(this.destroyed$)
      )
     
      constructor(private router: Router, private authService: userService ) {
      }
    
      ngOnInit(): void {
        this.name$.subscribe();
      }
  }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将Angular Material自动完成中的值分配给组件中的变量? - How can I assign a value from the Angular Material autocomplete to a variable in my component? 如何在角度6的http响应中为变量分配值? - How to assign value to a variable from http response in angular 6? 如何在角度订阅方法中为变量赋值 - How to assign value to variable in angular subscribe method 如何将值分配给角度2中的参考变量? - how to value is assign to reference variable in angular 2? Angular 8. 如何在构造函数中为服务属性分配可观察值? - Angular 8. How to assign observable value to service property in constructor? 如何通过从数据库(角度6和firebase)中检索数据的服务为组件变量赋值? - How to assign value to a component variable from a service retrieving data from a database (angular 6 and firebase)? 如何将从函数调用返回的值分配给Angular 2中html内的变量 - how to assign the value returned from function call to a variable inside html in angular 2 通过单击角度2从* ngFor中的另一个组件分配变量值 - Assign variable value from another component in *ngFor by click in angular 2 无法将从 api 返回的数据值分配给 Angular 8 中的 class 变量 - Not able to assign data value returned from api to class variable in Angular 8 如何在 Type Script Angular 中为类的成员变量赋值 - How to assign value to the member variable of a Class in Type Script Angular
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM