以下是一个示例代码,演示如何从解码的base64字符串中提取值:
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
template: `
Decoded Value: {{ decodedValue }}
`,
})
export class AppComponent {
base64String = 'SGVsbG8gV29ybGQ='; // base64编码的字符串
decodedValue: string;
decodeBase64() {
const decodedString = atob(this.base64String); // 使用atob函数解码base64字符串
this.decodedValue = decodedString;
}
}
在上面的示例中,我们首先在组件中定义了一个base64编码的字符串 base64String 和一个用于存储解码后值的变量 decodedValue。在 decodeBase64 方法中,我们使用内置的 atob 函数对base64字符串进行解码,并将解码后的值赋给 decodedValue 变量。
在模板中,我们使用一个按钮来触发 decodeBase64 方法,并使用 *ngIf 指令在 decodedValue 有值时显示解码后的值。
请注意,atob 函数是原生JavaScript函数,可以直接在Angular应用程序中使用。