简体   繁体   English

oninit之前的angular2渲染组件

[英]angular2 render component before oninit

I have following code snippets 我有以下代码片段

import { Component, OnInit } from "@angular/core";
import { AuthenticationService } from "../shared/services/authentication.service";
import { User } from "./user";
import { UserService } from "./user.service";

@Component({
    templateUrl:'./user.component.html'
})
export class UserComponent implements OnInit{
    user: User;

    constructor(
        private _userService: UserService,
        private _authService:AuthenticationService){
    }

    ngOnInit(){

        let user = this._userService.getUser();

        if(!user){
            this._authService.getLoggedUser().subscribe(
                user => {
                    this.user = user;
                    this._userService.setLoggedUser(user);
                }
            );

            this._userService._loggedUser$.subscribe(
                user => {
                    this.user = user;
                }
            )
        }else{
            this.user = user;
        }
    }
}

this working fine with first route initializing(/user/profile) with else block.But when user refresh the browser window I made a request again to the server and get user back and render my html component( if(!user) block ).here is html view 这与使用else块进行第一个路由初始化(/ user / profile)的效果很好。但是当用户刷新浏览器窗口时,我再次向服务器发出了一个请求,并让用户返回并渲染了我的html组件( if(!user)块)。这是HTML视图

<form>
        <fieldset>
            <legend>Profile</legend>
            <div class="form-group">
                <label>Name</label>
                <input name="name" type="text" class="form-control" [(ngModel)]="user.name">
            </div>
            <div class="form-group">
                <label>Mobile</label>
                <input name="mobile" type="text" class="form-control" [(ngModel)]="user.mobileNumber">
            </div>
            <div class="form-group">
                <label>Password</label>
                <input name="password" type="password" class="form-control">
            </div>
            <div class="form-group">
                <label>Email</label>
                <input name="email" type="text" class="form-control" [(ngModel)]="user.email" disabled>
            </div>
        </fieldset>
    </form>

And I have the AppComponent and it will do the same inside onInit (get user via http call if not exist).The problem is when I render my UserComponent on page refresh first before finishing the init method of userComponent it will navigate to AppComponent init method and just after that user.component.html will crash without user data saying Cannot read property 'name' of undefined .and again it will redirect to onInit of UserComponent and but at this time user html had already rendered and carshed. 我有AppComponent,它将在onInit内执行相同的onInit (如果不存在,则通过http调用获取用户)。问题是当我在完成userComponent的init方法之前首先刷新页面上的UserComponent时将导航到AppComponent的init方法并在该user.component.html崩溃之后,没有用户数据说Cannot read property 'name' of undefined并且它将再次重定向到UserComponent的onInit,但此时用户html已经呈现并消失。

How can I fix this scenario and Is there a best way to handle a situation like this? 我该如何解决这种情况?有没有最好的方法来处理这种情况?

Thanks in advance.. 提前致谢..

you can put your <form> inside a <div *ngIf="user">..</div> and this component is loading only once you have some values inside your user. 您可以将<form>放在<div *ngIf="user">..</div>并且仅当用户内部具有一些值时,此组件才会加载。 so you will not get that error. 因此您不会收到该错误。

<div *ngIf="user">
     <form> <fieldset>
            <legend>Profile</legend>
            <div class="form-group">
                <label>Name</label>
                <input name="name" type="text" class="form-control" [(ngModel)]="user.name">
            </div>
           .............
           .............
    </form>
</div>

there is one more way you can use resolve in your route so only when your resolve is done then only user.component will start. 还有另一种方法可以在路由中使用resolve,因此只有完成解析后,才会启动user.component。

export const AppRoutes: Routes = [
  ...
  { 
    path: 'contact/:id',
    component: ContactsDetailComponent,
    resolve: {
      contact: 'contact'
    }
  }
];

read from here about resolve http://blog.thoughtram.io/angular/2016/10/10/resolving-route-data-in-angular-2.html 从此处阅读有关解决问题的信息http://blog.thoughtram.io/angular/2016/10/10/resolving-route-data-in-angular-2.html

What about this? 那这个呢? Show the form once user is in. <fieldset *ngIf="user"> 用户进入后显示表单。 <fieldset *ngIf="user">

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

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