首页 > 解决方案 > 将 crypto-js 与 Angular 5 项目一起使用时出现意外令牌错误

问题描述

我需要加密从 angular-5 发送到 php 脚本的数据并在 php 中解密然后处理它。我对这些东西很陌生。

首先,我安装了 crypto-js 库:

npm install --save crypto-js

然后我将它导入app.module.ts

import * as crypto from 'crypto-js';

并将其添加到导入中:

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    crypto

在我的app.component.ts文件中,我试图加密一个变量并测试结果:

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'app';
  this.crypto...
}

我有一个错误this.

意外的标记。需要构造函数、方法、访问器或属性。

标签: angularencryptionangular5cryptojs

解决方案


而是从 crypto-js 导入 *,我的团队只是将 crypto-js 依赖项放在 .angular-cli.json 的脚本数组中。

一旦您在 ts 文件的顶部完成此操作,您计划使用加密,您可以添加声明 const CryptoJS;然后正常使用。

此方法也可以用于任何其他非 es6 或 ts 打包的依赖项。


推荐阅读