Estoy tratando de codificar una cadena que es bastante compleja, para poder incluirla en un mailto:
Componente:
<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')Cuando trato de usar encodeValue, aparece "no se puede encontrar el nombre encodeValue.
¿Cómo sería mejor codificar la URL del cuerpo?
HttpParameterCodec : es un códec para codificar y decodificar parámetros en URL (usado por HttpParams).
Si necesita codificar Url, puede usar lo siguiente:
encodeURI asume que la entrada es un URI completo que puede tener algunos caracteres que necesitan codificación.
encodeURIComponent codificará todo con un significado especial, por lo que lo usa para componentes de URI, como:
var textSample= "A sentence with symbols & characters that have special meaning?"; var uri = 'http://example.com/foo?hello=' + encodeURIComponent(textSample);Tanto encodeURI() como encodeURIComponent() funcionarán, aunque existen algunas diferencias:
var set1 = ";,/?:@&=+$"; // Reserved Characters var set2 = "-_.!~*'()"; // Unescaped Characters var set3 = "#"; // Number Sign var set4 = "ABC abc 123"; // Alphanumeric Characters + Space console.log(encodeURI(set1)); // ;,/?:@&=+$ console.log(encodeURI(set2)); // -_.!~*'() console.log(encodeURI(set3)); // # console.log(encodeURI(set4)); // ABC%20abc%20123 (the space gets encoded as %20) console.log(encodeURIComponent(set1)); // %3B%2C%2F%3F%3A%40%26%3D%2B%24 console.log(encodeURIComponent(set2)); // -_.!~*'() console.log(encodeURIComponent(set3)); // %23 console.log(encodeURIComponent(set4)); // ABC%20abc%20123 (the space gets encoded as %20)Referencia: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent