更新url參數(shù)以及根據(jù)參數(shù)來獲取資源的設(shè)定(Detecting url changes and updating content in component)

angular更新url參數(shù)和component中的內(nèi)容的方法

大概的介紹如圖。就是想點擊button更新url的部分參數(shù),然后在根據(jù)參數(shù)來獲取content中的內(nèi)容


routing檢測和content 更新.png

pagination component

setPage(clickedPage) {
        // back to the top of the window
        document.documentElement.scrollTop = 0;
        
        this.currentPage = clickedPage;
        if (this.currentPage - 2 <= 1) {
            this.start = 1;
            this.end = this.start + 4
        } else if (this.currentPage + 2 > this.totalPageNumber) {
            this.start = this.totalPageNumber - 4
            this.end = this.totalPageNumber;
        }
        else {
            this.currentPage = this.currentPage;
            this.start = this.currentPage - 2;
            this.end = this.currentPage + 2;
        }

        this.setPageArray();

        // this.currentPageToParentEvent.emit(this.currentPage);
        this.router.navigate([], { relativeTo: this.activatedRouter, queryParams:{page: clickedPage}, queryParamsHandling: "merge"});
    }

最關(guān)鍵的是下面的代碼

this.router.navigate([], { relativeTo: this.activatedRouter, queryParams:{page: clickedPage}, 

注意,activatedRouter是要import activatedRouter的

這里可以部分更新url

        this.activatedRoute.queryParamMap.subscribe(queryParams => {
            console.log(Number(queryParams['params']['page']));
            this.listtingService
                .retriveSearchListing(Number(queryParams['params']['page']))
                .subscribe((response: any) => {
                    console.log(response);
                    this.jobs = response.data;
                    this.totalPageNumber = response.totalPageNumber;
                });
        })

以上是content使用的,subscribe to the url changes, 根據(jù)changes來更新content中的內(nèi)容。
完美解決

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容