Angular 4没有从onpopstate获取登陆URL

时间:2017-08-03 21:33:29

标签: angular

我正在尝试获取在网页中按下后退按钮时浏览器转到的URL,我已将此代码添加到我的组件中

import {Component, OnInit} from '@angular/core';
import {LocalDataSource} from 'ng2-smart-table';
import {TableComponent} from '../../../table/table.component';
import {PagerService} from '../../../services/pager/pager.service';
import {ActivatedRoute, Params, Router} from '@angular/router';
import {Subscription} from 'rxjs/Subscription';
import {PlatformLocation} from '@angular/common';

@Component({
  selector: 'app-test',
  templateUrl: './test.component.html',
  styleUrls: ['./test.component.css', '../../../app.component.css']
})
export class TestComponent implements OnInit {
  source: LocalDataSource;
  currentPage: number;
  totalCount: number;
  pager: any = {};
  paramsSubscription: Subscription;
  urlParams: Params;


  constructor(private router: Router, private location: PlatformLocation, private route: ActivatedRoute) {
    this.location.onPopState(() => {
      console.log('Back Pressed');
      this.getURLParams();
      // Giving the URL parameters of the page from where back button is clicked 
      console.log(this.urlParams['page']);
    });
  }


getURLParams() {
    this.paramsSubscription = this.route.params.subscribe(
      (params: Params) => {
        this.urlParams = params;
      }
    );
  }

  ngOnInit() {
    // Methods to populate data
  }

}

正在检测后退按钮并在控制台中显示参数。但是,控制台正在显示按下后退按钮的页面,而不是它所登陆的页面。我想根据目标网页动态更新数据。

有没有办法通过任何方法检测着陆页参数? 如果我错过了什么,请告诉我。

谢谢!

1 个答案:

答案 0 :(得分:1)

此代码:

this.router.url

它正在观察当前路线参数的变化。试着看router.events.subscribe((url) => console.log(url));

或订阅路由器事件:

differenceList