微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

向后导航时,Nativescript queryParamsMap无法正常工作

如何解决向后导航时,Nativescript queryParamsMap无法正常工作

实际上,我处于有两个屏幕的情况。一个是listComponent,另一个是DetailComponent。

在ListComponent.ts上

this.activatedRoute.queryParamMap.subscribe((params) => {
        console.log('param:' + params.get('updatedindex'));
    })

当我点击列表中的任何项目时,我导航到ListDetails屏幕,并且在那里我对项目进行了一些更改。我希望每当我返回时,这些更改都能反映在ListComponent上。所以我在detailComponent中所做的是

this.routerExtension.navigate([],{
        relativeto: this.activatedRoute,queryParams: {
            updatedindex: this.listIndex
        },queryParamsHandling: 'merge'
    })

据我了解,这将更新我的路线中的queryparams。每当我返回listcomponent屏幕时,可观察到的queryParamMap都会触发。但是,当我第一次在ListComponent上导航时,我的queryParamMap仅触发一次。

下面是我的路线。

const routes: Routes = [
{ path: "list-details",component: ListDetailsComponent },{ path: "",component: MyListComponent },];

解决方法

创建一个可观察对象并从屏幕1监听它,然后通过屏幕2将所有更新推送到该可观察对象并关闭它。

notify.service.ts

import { Injectable } from '@angular/core';
import { Subject } from 'rxjs-compat/Subject';

@Injectable({
providedIn: 'root'
})
export class NotifyService {

private refreshDataForView = new Subject<any>();
refreshDataForParentViewObservable$ = this.refreshDataForView.asObservable();

public relaodDataForParentView(data: any) {
    if (data) {
    this.refreshDataForView.next(data);
    }
}
}

第二个componenet.ts

     constructor(
    private notifyService: NotifyService
  ) {  }

  goBack() {
    this.notifyService.relaodDataForParentView({ data: 'any data you wanrt to pass here ' });
    this.router.back();
  }

第一个component.ts

 reloadDataSubscription: any;

constructor(
    private notifyService: NotifyService
    ) {}
    ngOnInit() {
 this.reloadDataSubscription = this.notifyService.refreshDataForParentViewObservable$
 .subscribe((res) => {
    console.log('======',res);
    // do what you want to do with the data passed from second view
    });
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。