2017-08-03 45 views
0

我試圖讓該瀏覽器進入一個網頁被按下時後退按鈕的網址登陸網址,我在組件中添加該代碼角4沒有得到來自onpopstate

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 
    } 

} 

這是檢測後退按鈕並在控制檯中顯示參數。但是,控制檯正在顯示按下後退按鈕的頁面,而不是其着陸的頁面。我想根據着陸頁動態更新數據。

有沒有辦法通過任何方法檢測登陸頁面參數? 請讓我知道如果我失去了一些東西。

謝謝!

+0

這是什麼方法做:'this.getURLParams()'我沒有看到它在所提供的代碼示例。 – DeborahK

+0

錯過了那個,現在更新了代碼!它只是獲取URL參數,基於此我更新了網頁的數據部分。 –

回答

1

此代碼:

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

它看爲更改當前路線的參數。試試看this.router.url

或訂閱路由器事件:

router.events.subscribe((url) => console.log(url)); 
+0

非常感謝@DeborahK router.events.subscriibe完成了這項工作。 –

相關問題