【问题标题】:@Input with promise Angular2@Input 承诺 Angular2
【发布时间】:2017-02-17 09:27:06
【问题描述】:

我有这样的父组件:

import { Component } from '@angular/core';

import { UrlService } from '../../services/url.service';

@Component({
   selector: 'wrapPost',
   templateUrl: './app/html/wrapPost.component.html',
})

export class WrapPost {
    dataPost: any;
    url: string;

    constructor(private urlService: UrlService) {}

    ngOnInit(){
        let url = window.location.pathname;
        this.urlService.getData(url)
            .then(res => this.dataPost = res);
    }
}

从服务器获取日期并返回 this.dataPost。 效果很好。

templateUrl: './app/html/wrapPost.component.html'的这个html

<div class="noteCommonAlign">

    <navigation></navigation>
    <posts [dataPost]="dataPost"></posts>
    <loading class="noteCommonAlign"></loading>
    <pagination [dataPost]="dataPost"></pagination>

</div>

进一步的组件分页(子)

import { Component, Input, SimpleChanges } from '@angular/core';

@Component({
   selector: 'pagination',
   templateUrl: './app/html/pagination.component.html',
   styleUrls: ['app/css/pagination.component.css']
})

export class Pagination {
   pagCurrent: number;
   @Input() dataPost: any;

   constructor(){
   }
    ngOnChanges(changes: SimpleChanges){
        try{
       console.log(this.dataPost);
     } catch(err) {
       console.log(err);
     }
    }
}

我无法异步获取 this.dataPost。当我在没有 ngOnChanges console.log(this.dataPost) 的情况下调用时,会发生错误。我稍微解决了这个问题。我使用了 ngOnChanges 但在控制台中显示了 2 条消息。一条消息有错误,另一条消息效果很好。

我如何理解它已经发生,因为 ngOnChanges 是生命周期,并且在 ngOnInit 之前以及每当一个或多个数据绑定输入属性更改时调用。 我不希望它被调用两次。

如何在具有@Input 的子组件中从服务器获取日期?

我很抱歉语法问题。我会很高兴任何解决方案。

【问题讨论】:

    标签: angular


    【解决方案1】:

    怎么样

    <navigation></navigation>
    <posts [dataPost]="dataPost | async"></posts>
    <loading class="noteCommonAlign"></loading>
    <pagination [dataPost]="dataPost | async"></pagination>
    

    更新

    使用此代码

    ngOnInit(){
        this.urlService.getData()
            .then(res => this.dataPost = res);
    }
    

    您不需要| async,因为this.dataPost 获取分配的类别,而不是Promise

    如果你改成

    ngOnInit(){
        this.dataPost = this.urlService.getData();
            //.then(res => this.dataPost = res);
    }
    

    那么使用| async 是合适的,但如果你真的使用| async

    <pagination [dataPost]="dataPost | async"></pagination>
    

    然后

    @Input() dataPost: Promise<any>;
    

    获得类别,而不是承诺,因为| async 已经解决了获得类别的承诺。

    Plunker example

    【讨论】:

    • 感谢您的宝贵时间。我试图这样做,但效果不佳。我删除了ngOnChanges 并在costructor 中运行console.log(this.dataPost),但结果很差undefined。我认为它没有变得异步。你能帮忙解决一下吗?
    • 另外dataPost$: any; 也无法解决这个问题。当我从服务器获取某些内容并显示在ngAfterViewInit() { console.log(this.dataPost); } 时,然后返回类似null 的结果。
    • urlService.getData 长什么样子?该值在 ngAfterViewInit 中尚不可用。如果urlService.getData 调用服务器,与仅在浏览器中执行的代码相比,这需要很长时间。您需要将所有异步调用与.then(...) 链接起来以获取承诺,或者将.map().do().subscribe(...) 等运算符用于可观察对象。当ngAfterViewInit 执行时,对服务器的调用可能还没有发送事件。
    • urlService.getData 喜欢这个getData(url): Promise&lt;any&gt; { return this.http.post(url, JSON.stringify({name})) .toPromise() .then(res =&gt; res.json()) .catch(this.handleError); } 是的。我的代码中有.then(...)。所以this.urlService.getData(url) .then(res =&gt; this.dataPost = res); 得到了 Promise。我更改@Input() dataPost: any; -> @Input() dataPost: Promise&lt;any&gt;; 然后如果在pagination.component.ts (the child) 中调用ngOnInit(){console.log(this.dataPost)} 那么会出错
    • 管道“AsyncPipe”的参数“[object Object],[object Object],[object Object],[object Object]”无效。服务器发送对象,但为什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-15
    • 2016-10-20
    • 2017-09-07
    相关资源
    最近更新 更多