简体   繁体   English

使用Angular路由器在Ionic中无需导航即可传递数据

[英]passing data without navigation in Ionic with Angular router

I have a list which contains names of people, whenever I click to any of the items in the list, it will display to another page but not navigating to that path instead navigating to another page.我有一个包含人名的列表,每当我单击列表中的任何项目时,它都会显示到另一个页面,但不会导航到该路径,而是导航到另一个页面。

names.page.html:名称.page.html:

<ion-content>
  <ion-list *ngFor='let listNames of people'>
    <ion-card (click)="passData(listNames)">
      <ion-card-header>
       <ion-card-title>{{ listNames.firstname }}</ion-card-title>
      </ion-card-header>
    </ion-card>
  </ion-list>
</ion-content>

names.page.ts:名称.page.ts:

passData(listNames) {
  this.router.navigate(['/pages/details', listNames]);
}

details.page.ts:详细信息.page.ts:

export class DetailsPage implements OnInit {

 public fName;

 constructor(public route: ActivatedRoute) { }

 ngOnInit() {
  const data = this.route.snapshot.paramMap.get('firstname');
  this.fname= data;
 }
}

I wanted to navigate to another screen after sending a data to display in details.page.html, thanks.我想在发送数据以显示在 details.page.html 后导航到另一个屏幕,谢谢。

You can pass data using events class.您可以使用事件类传递数据。 It's used to pass data to any component/page without the navigation它用于在没有导航的情况下将数据传递到任何组件/页面

import { Events } from '@ionic/angular';


constructor(public events: Events) { }

//Apply this code in received component/page //在收到的组件/页面中应用此代码

ngOnInit(){
this.events.subscribe('flag', (data) => {
  console.log(data);
  console.log(JSON.stringify(res))
});

} }

//Use this code in data sender component/page //在数据发送器组件/页面中使用此代码

this.events.publish('flag', listNames);

Are your parameters in this.router.navigate() correct?您在this.router.navigate()参数是否正确? Below is the sample from Angular- Routing, and navigation下面是来自Angular-Routing 和导航的示例

this.router.navigate(['/heroes', { id: heroId, foo: 'foo' }]);

It should look something like this, depending on where you run it:它应该看起来像这样,具体取决于您运行它的位置:

localhost:4200/heroes;id=15;foo=foo

Check your one this.router.navigate(['/pages/details, listNames]);检查你的this.router.navigate(['/pages/details, listNames]); Looks like missing ' at the end of /pages/details看起来像 /pages/details 末尾缺少 '

Hope this helps希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM