問題描述:
如果搜索到的wifi是中文名稱,那么就會顯示特殊字符比如(甿ªç”Ÿè迪),亂碼的問題。
這里需要使用 escape方法對特殊字符編碼,然后使用 decodeURIComponent解碼
具體代碼如下
創(chuàng)建一個管道,在需要的地方使用即可。
import { Pipe, PipeTransform, Injectable } from "@angular/core";// escape在管道中需要定義一下,要不然編譯報錯。declare function escape(s: string): string;@Pipe({ name: "ascPipe"})@Injectable()export class AscIIToGBKPipe implements PipeTransform { transform(value: any) { console.log(value); if (value) { let str = escape(value); let dec = decodeURIComponent(str); return dec; } else { return value; } }}補充:angular2/angular4地址欄中文亂碼
有時候我們需要在url傳遞中文參數(shù),但是獲取時會出現(xiàn)亂碼。網(wǎng)上找了好多方法,但是從前端解析的話能夠真正生效的還沒有找到。病急亂投醫(yī),竟然被我試出來了。話不多說,方法就是用encodeURIComponent轉(zhuǎn)義兩次。即:encodeURIComponent(encodeURIComponent(你要轉(zhuǎn)換的對象))。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持錯新站長站。
新聞熱點
疑難解答
圖片精選