問題描述:
如果搜索到的wifi是中文名稱,那么就會顯示特殊字符比如(甿ªç”Ÿè迪),亂碼的問題。
這里需要使用 escape方法對特殊字符編碼,然后使用 decodeURIComponent解碼
具體代碼如下
創建一個管道,在需要的地方使用即可。
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傳遞中文參數,但是獲取時會出現亂碼。網上找了好多方法,但是從前端解析的話能夠真正生效的還沒有找到。病急亂投醫,竟然被我試出來了。話不多說,方法就是用encodeURIComponent轉義兩次。即:encodeURIComponent(encodeURIComponent(你要轉換的對象))。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。
新聞熱點
疑難解答