【问题标题】:Routing with variable angular2?使用变量 angular2 路由?
【发布时间】:2020-04-07 17:12:09
【问题描述】:

我需要路由到另一个组件,正如您在路径中看到的那样,但我需要将变量 this.a 一起传递。我该如何做才能将此变量的值用于其他组件?

组件.ts

 import {Component, OnDestroy,OnInit, Input} from '@angular/core';
    import { ChangeDetectorRef } from '@angular/core';
    import { Router } from '@angular/router';
    import { analyzeAndValidateNgModules } from '@angular/compiler';
    import { ParkingService } from '../../../services/parking.service';
    import { Injectable } from '@angular/core';
    import { Subject, BehaviorSubject } from 'rxjs';



    @Component({
        selector: 'ngx-homepage',
        styleUrls: ['./homepage.component.scss'],
        templateUrl: './homepage.component.html',
        providers: [ParkingService]

      })

      @Injectable()
      export class HomepageComponent implements OnInit {


        a:string;
      constructor (public parkingService: ParkingService, public root:Router) { 


      }
      ngOnInit(){


      }

            pass(passa)
              {

              this.a= passa.value;
              window.alert(this.a);
              this.parkingService.setUserToken(this.a);

              this.root.navigate(['/pages/iot-dashboard']);

              }




    }

【问题讨论】:

    标签: angular


    【解决方案1】:

    在 Angular 文档中,您可以在 https://angular.io/guide/router#migrating-urls-with-redirects 中看到答案

    pass(passa) {
        this.a= passa.value;
        window.alert(this.a);
        this.parkingService.setUserToken(this.a);
        this.root.navigate(['/pages/iot-dashboard', { userToken: this.a }]);
    }
    

    您可以将对象中的userToken替换为您要传递给路由的变量的名称

    【讨论】:

    猜你喜欢
    • 2016-01-03
    • 2016-12-20
    • 1970-01-01
    • 2019-02-03
    • 2016-07-27
    • 1970-01-01
    • 2017-12-03
    • 2016-02-01
    • 2017-08-12
    相关资源
    最近更新 更多