简体   繁体   English

我在角度2的shareService发送值正常工作,但是当我渲染我的其他组件时会获得默认值

[英]my shareService in angular 2 send value work Properly but when i render my another components get default values

I'm trying to create a application with angular 2. have an Userservice and when user signIn or singOut Im send a value to another components that say user is login or not and its worked properly i meaning data sending. 我正在尝试创建一个angular 2.的应用程序angular 2.有一个Userservice,并且当用户signIn或singOut Im将值发送到另一个组件,该组件表示用户是否登录并且其工作正常时,这意味着发送数据。 my problem is Im have private property in another components with name islogin and with default value(false) that i subscribe my userService islogin...but when my components rendering default value again set. 我的问题是我在另一个名为islogin且具有默认值(false)的组件中拥有私有属性,我订阅了我的userService islogin ...但是当我的呈现默认值的组件再次设置时。 how to fix this? 如何解决这个问题? this is my codes: 这是我的代码:

***********************userService*********************************

@Injectable()
export class UserService {

    private static _data: any;

    isLogin$:Subject<boolean> = new Subject();
    UserInfo$:Subject<any> = new Subject();

    // Observable streams
    CheckUser = this.isLogin$.asObservable();
    userinfo = this.UserInfo$.asObservable();

    constructor(private _util: UtilService) {


    }


    public signUp(data: any) {
        return this._util.post('api/auth/register', data);
    }

    public signIn(data: any) {
        var promise = this._util.post('api/auth/login', data);
        promise.then(value => {
            if (value.status == 0) {
                UserService._data = value.result;
                this.isLogin$.next(true);
                this.UserInfo$.next(UserService._data);
            }
        });
        return promise;
    }

    public signOut() {

        var promise = this._util.post('api/auth/logout');
        promise.then(value => {
            if (value.status == 0) {
                this.reset();
                this.isLogin$.next(false);
                this.UserInfo$.next(UserService._data);

            }
        });
        return promise;

    }
}

*********************someComponent like user profle********************

export class Profile extends Ext {

    private isLogin:boolean;
    private model ;


    constructor( 
                private _router: Router,
                private _routeParams: RouteParams, 
                private _seo: SEOService,
                private _user: UserService)
    {
        super();


        this._user.CheckUser.subscribe(val =>{

            this.isLogin = val;
            console.log(this.isLogin);
            alert(val);

        });

        this._user.userinfo.subscribe(val => {

            this.model = val;
            alert(val);

        });
}
}

and this is template for this component : 这是此组件的模板:

<div *ngIf="isLogin | async" class="container">
   hiiiii {{model.name}}
</div>

what is my problem?! 我有什么问题?

The problem is that you use the async pipe on something that isn't an observable (ie isLogin ) but a boolean property. 问题是您在不是可观察的对象(即isLogin )而是布尔属性的对象上使用了async管道。

So either you use this: 因此,您可以使用以下命令:

<div *ngIf="isLogin" class="container">
  hiiiii {{model.name}}
</div>

Either you update your component like this: 您可以像这样更新组件:

export class Profile extends Ext {
  private isLogin:Observable<boolean>; // <-----
  private model ;

  constructor( 
            private _router: Router,
            private _routeParams: RouteParams, 
            private _seo: SEOService,
            private _user: UserService)
  {
    super();

    this.isLogin = this._user.CheckUser; // <-----

    (...)
  }
}

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

相关问题 我如何使我的React组件及时呈现? - How do i get my React components to render in a timely fashion? 如何获取我的handleClick渲染不同的组件 - How to get my handleClick to render different components 在Angular中,我无法使我的渲染路径正确 - In Angular, I Can't get my render paths correct 当构建时不在布局中渲染我的子组件但是当 npm state 它工作正常 - When get build not Render my child components in Layout But when npm state It works right 如何获取我的角度应用程序以在ie8中渲染? - How can I get my angular app to render in ie8? 我的 Angular 7 Web 组件 @Input<boolean> 当我绑定错误值时不起作用 - My angular 7 web component @Input<boolean> doesn't work when I bind a false value 我无法让我的JavaScript事件监听器正常工作 - I can't get my JavaScript eventlistener to work properly 如何让我的机器人向另一个频道发送消息? - How can I get my bot to send a message to another channel? 当我更改父组件 state 时,为什么我的 React 子组件没有获取值(重新渲染)? - Why doesn't my React child component get value (re-render) when I change the parent state? 按下发送按钮后,如何将我的表格发送到我的电子邮件? - How do I get my form to send to my e-mail when the send button is pressed?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM