问题描述:
如果搜索到的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(你要转换的对象))。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。
如对本文有疑问, 点击进行留言回复!!
解决三元运算符 报错“SyntaxError: can''t assign to conditional expression”
解决angular 使用原生拖拽页面卡顿及表单控件输入延迟问题
使用 Angular RouteReuseStrategy 缓存(路由)组件的实例代码
分享Angular http interceptors 拦截器使用(推荐)
网友评论