Send data through routing paths in Angular

前端 未结 6 1387
无人共我
无人共我 2020-11-22 08:52

Is there anyway to send data as parameter with router.navigate? I mean, something like this example, as you can see the route has a data parameter, but doing this it\'s not

6条回答
  •  情书的邮戳
    2020-11-22 09:41

    Best I found on internet for this is ngx-navigation-with-data. It is very simple and good for navigation the data from one component to another component. You have to just import the component class and use it in very simple way. Suppose you have home and about component and want to send data then

    HOME COMPONENT

    import { Component, OnInit } from '@angular/core';
    import { NgxNavigationWithDataComponent } from 'ngx-navigation-with-data';
    
    @Component({
     selector: 'app-home',
     templateUrl: './home.component.html',
     styleUrls: ['./home.component.css']
    })
    export class HomeComponent implements OnInit {
    
    constructor(public navCtrl: NgxNavigationWithDataComponent) { }
    
     ngOnInit() {
     }
    
     navigateToABout() {
      this.navCtrl.navigate('about', {name:"virendta"});
     }
    
    }
    

    ABOUT COMPONENT

    import { Component, OnInit } from '@angular/core';
    import { NgxNavigationWithDataComponent } from 'ngx-navigation-with-data';
    
    @Component({
     selector: 'app-about',
     templateUrl: './about.component.html',
     styleUrls: ['./about.component.css']
    })
    export class AboutComponent implements OnInit {
    
     constructor(public navCtrl: NgxNavigationWithDataComponent) {
      console.log(this.navCtrl.get('name')); // it will console Virendra
      console.log(this.navCtrl.data); // it will console whole data object here
     }
    
     ngOnInit() {
     }
    
    }
    

    For any query follow https://www.npmjs.com/package/ngx-navigation-with-data

    Comment down for help.

提交回复
热议问题