base64 encode and decode a string in angular (2+)

前端 未结 4 1995
被撕碎了的回忆
被撕碎了的回忆 2020-12-04 17:06

How to encode or decode a string in angular 2 with base64 ??? My front-end tool is Angular 2. I had a password string, before passing it to API I n

相关标签:
4条回答
  • 2020-12-04 17:41

    Use btoa("yourstring")

    more info: https://developer.mozilla.org/en/docs/Web/API/WindowBase64/Base64_encoding_and_decoding

    TypeScript is a superset of Javascript, it can use existing Javascript libraries and web APIs

    0 讨论(0)
  • 2020-12-04 17:44

    For encoding to base64 in Angular2, you can use btoa() function.

    Example:-

    console.log(btoa("stringAngular2")); 
    // Output:- c3RyaW5nQW5ndWxhcjI=
    

    For decoding from base64 in Angular2, you can use atob() function.

    Example:-

    console.log(atob("c3RyaW5nQW5ndWxhcjI=")); 
    // Output:- stringAngular2
    
    0 讨论(0)
  • 2020-12-04 17:49

    Use the btoa() function to encode:

    console.log(btoa("password")); // cGFzc3dvcmQ=

    To decode, you can use the atob() function:

    console.log(atob("cGFzc3dvcmQ=")); // password

    0 讨论(0)
  • 2020-12-04 17:55

    Use btoa() for encode and atob() for decode

    text_val:any="your encoding text";
    

    Encoded Text: console.log(btoa(this.text_val)); //eW91ciBlbmNvZGluZyB0ZXh0

    Decoded Text: console.log(atob("eW91ciBlbmNvZGluZyB0ZXh0")); //your encoding text

    0 讨论(0)
提交回复
热议问题