angular - 将 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.
:
意外的标记。需要构造函数、方法、访问器或属性。
解决方案
而是从 crypto-js 导入 *,我的团队只是将 crypto-js 依赖项放在 .angular-cli.json 的脚本数组中。
一旦您在 ts 文件的顶部完成此操作,您计划使用加密,您可以添加声明 const CryptoJS;然后正常使用。
此方法也可以用于任何其他非 es6 或 ts 打包的依赖项。
推荐阅读
- javascript - 使用 IntelliJ 启动时,Spring Boot 不提供资源文件夹中的资产
- gradle - Grails 3 项目为各种文件获取“无法解析 IntelliJ IDEA 中的符号”
- python - 尝试在此游戏中切换子弹图案
- javascript - 未捕获的 SyntaxError:意外的标识符 - Webshare api
- powershell - 新对象无法识别
- php - 使用 Apache 安装 PHP 5.6.x 源代码,但出现 openssl 错误,取消引用指向不完整类型的指针
- php - laravel 5.7 spatie/permission 如何处理用户类和角色类之间的关系
- python - Pygame 不解释键盘输入
- c# - 如何将此功能添加到我的按钮?
- c# - 网格不会在 StackPanel 内延伸