网址的编码字符串(角度) [英] Encode string for URL (angular)
问题描述
我正在尝试编码一个非常复杂的字符串,以便可以将其包含在mailto中:
I'm trying to encode a string that's pretty complex, so that I can include it in a mailto:
组件:
<a href="mailto:test@example.com?subject='Hello'&{{body}}">
TS:
import { HttpParameterCodec } from "@angular/common/http";
let body = encodeValue('This is the example body\nIt has line breaks and bullets\n\u2022bullet one\n\u2022bullet two\n\u2022bullet three')
当我尝试使用encodeValue时,出现找不到名称encodeValue.
When I try to use encodeValue, I get "cannot find name encodeValue.
如何对主体进行url编码?
How would be best to url-encode body?
推荐答案
HttpParameterCodec
:是用于对URL中的参数进行编码和解码的编解码器(由HttpParams使用).
HttpParameterCodec
: is a codec for encoding and decoding parameters in URLs( Used by HttpParams).
如果您需要对网址进行编码,则可以使用以下代码:
If you need to encode Url you can use the below:
encodeURI
假定输入是一个完整的URI,其中可能包含一些需要编码的字符.
encodeURI
assumes that the input is a complete URI that might have some characters which need encoding in it.
encodeURIComponent
将对具有特殊含义的所有内容进行编码,因此可将其用于URI的组件,例如:
encodeURIComponent
will encode everything with special meaning, so you use it for components of URIs, such as:
var textSample= "A sentence with symbols & characters that have special meaning?";
var uri = 'http://example.com/foo?hello=' + encodeURIComponent(textSample);
这篇关于网址的编码字符串(角度)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!