【问题标题】:Access inner child page's public variable within the parent page在父页面中访问内部子页面的公共变量
【发布时间】:2018-01-09 05:02:57
【问题描述】:

你能告诉我如何在父页面中访问内部子页面的公共变量吗?即我需要访问这个变量offlineArticlesCount

注意:以下所有 3 个组件都有自己的modules

myPage.html - 父页面

 <picks *ngFor="let pick of pickData" [data]="pick"></picks>
 <p>{{instance.offlineArticlesCount}}</p> //How to do this?

myPage.ts

 @ViewChild(DownloadOfflineArticleComponent) instance: DownloadOfflineArticleComponent;

picks.html

<download-offline-article [data]="data" (onDownloadOfflineArticle)="downloadOfflineArticle($event)"></download-offline-article>

download-offline-article.ts - 内部子组件

export class DownloadOfflineArticleComponent {
   offlineArticlesCount: number = 0;
   constructor(){} 

downloadOfflineArticle() {
    this.articleService.downloadOfflineArticle(this.data.id)
      .subscribe((res: any) => {
        this.onDownloadOfflineArticle.emit(true);

        this.localCacheService.clearMyLibraryPageCacheGroup().then(() => {
          this.getAllMyPurchasedOfflineArticles().subscribe((res: any) => {
            this.offlineArticlesCount = res.count;//here is the place where it updates
          },
            error => { },
            () => { });
        });
      },
      error => { },
      () => { });
  }
    }

【问题讨论】:

  • 您是否已经尝试过像这样使用 ViewChild:@ViewChild(DownloadOfflineArticleComponent) instance: DownloadOfflineArticleComponent;,然后在视图中使用 {{ instance?. offlineArticlesCount}}

标签: angular typescript ionic2 ionic3


【解决方案1】:

在您编辑问题以阐明组件层次结构后,我认为解决问题的最佳方法是使用共享服务在组件之间共享数据:

// Create the shared service like below:

import { Injectable } from '@angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Observable } from 'rxjs';

@Injectable()
export class SharedService {

    private articleCountSubject: BehaviorSubject<any> = new BehaviorSubject({});    

    setArticleCount(count: any): void {
        this.articleCountSubject.next(count);
    }
    getArticleCount(): Observable<any> {
        return this.articleCountSubject.asObservable();
    }
}

// myPage.ts:

constructor(private sharedService: SharedService){}
ngOnInit() {
    this.sharedService.getArticleCount().subscribe(count=> {
        this.offlineArticlesCount = count;
    });
}

// myPage.html:

<picks *ngFor="let pick of pickData" [data]="pick"></picks>
<p> {{offlineArticlesCount }} </p>

// download-offline-article.ts:

//...
this.getAllMyPurchasedOfflineArticles().subscribe((res: any) => {
    this.sharedService.setArticleCount(res.count);
},
//...

【讨论】:

    【解决方案2】:

    两个答案都是正确的,这取决于您想在哪里使用子组件中的该属性。

    如果您只想在视图中显示它,您可以使用模板引用变量

    <picks *ngFor="let pick of pickData" [data]="pick"></picks>
    <p>{{ offlineArticle.offlineArticlesCount }}</p>
    <download-offline-article #offlineArticle ..."></download-offline-article>
    

    这样您就不会在父组件代码中创建子组件的实例。

    如果您想在组件代码中使用该属性,最好的方法是使用ViewChild 获取子组件的实例:

    // Parent component code...
    
    @ViewChild(DownloadOfflineArticleComponent) childComponent: DownloadOfflineArticleComponent; 
    

    当然你可以像这样在视图中使用它:

    <!-- Your view -->
    {{ childComponent?. offlineArticlesCount}}
    

    编辑

    由于您拥有更复杂的组件层次结构,我认为解决此问题的最佳方法是使用共享服务,您可以在其中存储所有共享信息。这样一来,您就可以更轻松地从应用中的任何组件(或页面)访问这些数据。

    【讨论】:

    • 它不起作用是什么意思?有什么错误吗?
    • 没有错误。但它似乎没有更新 UI。我可以调试它并将该变量作为值。但 UI 没有显示。请查看我添加的最新代码。跨度>
    • 第一次在视图中显示0,还是没有初始化?
    • 你的第一种方法对我不起作用。因为我有一个内在的孩子。即这个download-offline-article 组件在这个picks 组件里面。请看我的代码。
    • 是的,因为它只是一个属性,所以每次发生更改时,Angular 都会从服务中读取值。很高兴听到它终于奏效了 :) :) :)
    【解决方案3】:

    只有一行:

    @ViewChild(DownloadOfflineArticleComponent) childComp: DownloadOfflineArticleComponent;
    

    然后使用{{ childComp?.offlineArticlesCount }}访问所有公共属性

    【讨论】:

    • 由于复杂的组件层次结构,这不起作用。请参阅我的代码。
    • @Sampath ,好吧,你已经修改了一点代码,是否可以创建它的 plunker,这将非常有用。
    【解决方案4】:

    使用模板引用变量:

    <picks *ngFor="let pick of pickData" [data]="pick"></picks>
    <p>{{ offlineArticle.offlineArticlesCount }}</p>
    <download-offline-article #offlineArticle [data]="data" (onDownloadOfflineArticle)="downloadOfflineArticle($event)"></download-offline-article>
    

    【讨论】:

    • 我的组件层次结构不是这样的。请看我的代码。
    • 在这种情况下,您应该使用服务在组件之间共享数据
    猜你喜欢
    • 1970-01-01
    • 2021-12-06
    • 1970-01-01
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多