當前位置:
首頁 >
【超实用】Angular如何修改当前页面网页浏览器url后面?param1=xxxparam2=xxx参数(多用于通过浏览器地址参数保存用户当前操作状态的需求),实现监听url路由切换、状态变化。
發布時間:2023/11/27
47
豆豆
生活随笔
收集整理的這篇文章主要介紹了
【超实用】Angular如何修改当前页面网页浏览器url后面?param1=xxxparam2=xxx参数(多用于通过浏览器地址参数保存用户当前操作状态的需求),实现监听url路由切换、状态变化。
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
app.component.html
<button
(click)="location.go('api/path','?id=1&pageIndex=2&pageSize=10#hashValue2');path1 = location.path(true);path2 = location.path(false)
"
>
【推薦使用】修改當前瀏覽器url路徑,參數為多參數,并往所屬平臺(如瀏覽器)的歷史堆棧中追加一個新條目
</button>
<br>
<button(click)="location.replaceState('', 'id=1#hashValue1');path1 = location.path(true);path2 = location.path(false)"
>修改當前瀏覽器url參數為單參數,并替換所屬平臺(如瀏覽器)的歷史堆棧的頂部條目
</button>
<br />
<button(click)="location.replaceState('api/path','?id=1&pageIndex=2&pageSize=10#hashValue2');path1 = location.path(true);path2 = location.path(false)"
>修改當前瀏覽器url路徑,參數為多參數,并替換所屬平臺(如瀏覽器)的歷史堆棧的頂部條目
</button>
<br />
<p style="color: red;">【含hash的url】{{ path1 }}</p>
<p style="color: red;">【不含hash的url】{{ path2 }}</p><hr /><button (click)="path3 = normalizeQueryParams('a=1&b=2&c=3')">參數“a=1&b=2&c=3”前面加?
</button>
<p>【返回值】{{ path3 }}</p><hr /><button (click)="path4 = joinWithSlash('path1', 'path2')">連接路徑path1和path2
</button>
<p>【返回值】{{ path4 }}</p><hr /><button (click)="path5 = stripTrailingSlash('/a/b/c/')">去掉“/a/b/c/”最后一個“/”
</button>
<p>【返回值】{{ path5 }}</p><hr /><button (click)="state_str = getState()">報告位置歷史記錄的當前狀態</button>
<p>{{ state_str }}</p><hr /><button(click)="isEqual = location.isCurrentPathEqualTo('/api/path','id=1&pageIndex=2&pageSize=10')"
>對指定的路徑進行標準化,并和當前的標準化路徑進行比較
</button>
<p>如果指定的 URL 路徑和標準化之后的路徑一樣,則返回 true,否則返回false。【返回值】{{ isEqual }}
</p><hr /><button (click)="path6 = location.normalize('/api/path/')">去掉“/api/path/”末尾斜杠
</button>
<p>【返回值】{{ path6 }}</p><hr /><button (click)="path7 = location.prepareExternalUrl('api/path/')">在“api/path/”前加斜杠
</button>
<p>【返回值】{{ path7 }}</p><hr /><button (click)=" location.forward()"> 前進 </button>
<button (click)=" location.back()"> 后退 </button> <hr /><p style="color: green;">【監聽路由變化】{{url}}</p>
<p style="color: green;">【監聽路由狀態】{{state}}</p>
app.component.ts
import { Component } from '@angular/core';
import {Location,LocationStrategy,PathLocationStrategy,
} from '@angular/common'; //引入獲取、修改當前頁面url相關參數的類@Component({selector: 'app-root',templateUrl: './app.component.html',styleUrls: ['./app.component.scss'],providers: [Location,{ provide: LocationStrategy, useClass: PathLocationStrategy },//引入獲取、修改當前頁面url相關參數的類],
})
export class AppComponent {constructor(public location: Location //引入獲取、修改當前頁面url相關參數的類) {}path1 = '';path2 = '';path3 = '';path4 = '';path5 = '';path6 = '';path7 = '';state_str: any = '';isEqual = false;url = '';state: any = '';// 靜態方法----------------------------------------//給定 URL 參數字符串,如果需要則增加 '?' 前綴,否則原樣返回。normalizeQueryParams(params: string) {return Location.normalizeQueryParams(params);}//給定 url 的兩個部分,把它們連接(join)在一起,如有必要則添加一個斜杠。joinWithSlash(start: string, end: string) {return Location.joinWithSlash(start, end);}//如果 url 具有結尾斜杠,則移除它,否則原樣返回。 該方法會查找第一個 #、? 之前的結尾 / 字符,之后的則不管。如果 url 中沒有 #、?,則替換行尾的。stripTrailingSlash(url: string) {return Location.stripTrailingSlash(url);}// 方法----------------------------------------/*
this.location.path() //返回標準化之后的 URL 路徑
// ----------------------------------------
path(includeHash: boolean = false): string
參數
includeHash boolean
路徑中是否包含一個錨點片段(Anchor fragment)。可選. 默認值是 `false`.
返回值
標準化之后的 URL 路徑。
*/// 報告位置歷史記錄的當前狀態。getState() {return JSON.stringify(this.location.getState(), null, 2);}ngOnInit() {console.log(this.location);this.location.onUrlChange((url: string, state: unknown) => {(this.url = url), (this.state = JSON.stringify(state, null, 2));console.log(url, state);});}
}
?瀏覽器渲染效果
總結
以上是生活随笔為你收集整理的【超实用】Angular如何修改当前页面网页浏览器url后面?param1=xxxparam2=xxx参数(多用于通过浏览器地址参数保存用户当前操作状态的需求),实现监听url路由切换、状态变化。的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设置VSCode刷新资源管理器快捷键Ct
- 下一篇: [推荐]Angular File Cha