【问题标题】:Converting on click event to a router将点击事件转换为路由器
【发布时间】:2020-03-31 06:22:23
【问题描述】:

我在旧的 ionic 3 中有以下代码,目前我必须迁移到 ionic 5。我已经 google 了它,但仍然觉得它令人困惑。

在我的 HTML 中

(click)="onAllergiesDetailsClicked(medicalAlertGroup , 'Medical Alert')"

在我的 TS 文件中

onAllergiesDetailsClicked(allergiesGroupLists, pageTitle) {
  this.navCtrl.push(AllergiesGroupListPage, { allergiesGroupList: allergiesGroupLists, pageTitle: pageTitle });
}

在我的 AllerhiesGroupList 页面中

let allergiesGroupList: PatientAllergiesGroup = this.navParams.get('allergiesGroupList');
this.pageTitle = this.navParams.get('pageTitle');
this.patientAllergiesLists = (allergiesGroupList.patientAllertList.sort(this.sortingDate));

【问题讨论】:

  • 那么你的问题到底是什么?
  • 如何将 HTML 点击转换为路由器命令。

标签: ionic-framework ionic4 ionic5


【解决方案1】:

首先创建一个名为data的服务:

export class dataService {
setvalue:any;
constructor(){}
setDestValue(param){
     this.setvalue = param;
}

getDestValue(){
    return this.setvalue;
}
}

您需要从页面中发送数据的第二件事:

import { Router } from '@angular/router';
import {DataService} from 'put service path here';
constructor(private dataService:DataService,private router:Router...)

onAllergiesDetailsClicked(allergiesGroupLists, pageTitle){ 
    this.dataService.setDestValue({ allergiesGroupList: allergiesGroupLists, pageTitle: pageTitle });
    this.router.navigate(["name of page to navigate to as it is named in app-routing"]);
}

在页面中它被路由到:

import {DataService} from 'put service path here';

constructor(private dataService:DataService...){
    let data = this.dataService.getDestValue();
    this.pagetitle = data.pageTitle;
    this.grouplist = data.allergiesGroupList;
}

就是这样。

【讨论】:

  • 我按照给出的示例,当我运行应用程序时出现此错误错误错误:未捕获(承诺):NullInjectorError:StaticInjectorError(AppModule)[AllergiesPage -> AllergiesService]:StaticInjectorError(平台:核心) [AllergiesPage -> AllergiesService]:NullInjectorError:没有 AllergiesService 的提供者! NullInjectorError: StaticInjectorError(AppModule)[AllergiesPage -> AllergiesService]
  • 好吧,我做的例子没有要添加的服务,但是对于 AllergiesService ,他们说它应该在 app.module.ts 的提供者中
猜你喜欢
  • 2015-02-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多