【问题标题】:Nativescript queryParamsMap not working while navigating backNativescript queryParamsMap 在返回时不起作用
【发布时间】:2020-10-10 04:19:32
【问题描述】:

实际上我处于有两个屏幕的场景中。一个是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'
    })

据我了解,这将更新我路线中的查询参数。每当我回到 listcomponent 屏幕时,我的 queryParamMap observable 都会被触发。但是当我第一次在 ListComponent 上导航时,我的 queryParamMap 只会触发一次。

下面是我的路线。

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

【问题讨论】:

    标签: angular nativescript angular-routing angular2-nativescript nativescript-angular


    【解决方案1】:

    创建一个 Observable 并从屏幕 1 监听它,然后通过屏幕 2 的 observable 推送任何更新并关闭它。

    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);
        }
    }
    }
    

    第二个组件.ts

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

    第一个组件.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
        });
    }
    

    【讨论】:

    • 谢谢@Rajdeo Das。我最终做了一些与此类似的事情来实现它。但我认为应该有更好的方法在 nativescript 中执行此操作,而不是由我们自己处理。
    猜你喜欢
    • 2010-11-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 2019-12-25
    • 1970-01-01
    • 2018-02-25
    相关资源
    最近更新 更多