javascript 在 AngularJS 应用程序中包含 CryptoJS - 找不到变量:CryptoJS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26482494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 06:01:21  来源:igfitidea点击:

Include CryptoJS in AngularJS application - Can't find variable: CryptoJS

javascriptangularjsencryptioncryptojs

提问by user3475602

I want to use CryptoJS in my AngularJS application, but I get this error: Can't find variable: CryptoJS.

我想在我的AngularJS应用程序中使用CryptoJS,但我得到这个错误:Can't find variable: CryptoJS

I included this in my index.html:

我把它包括在我的index.html

<script src="http://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/rc4.js"></script>

And tried to encrypt something:

并试图加密一些东西:

var encrypted = CryptoJS.RC4Drop.encrypt("Message", "Secret Passphrase");

Any help would be greatly appreciated.

任何帮助将不胜感激。

回答by Shark Party

Preface:

前言:

This one took me a bit to sort out. I'm using the SHA1 library, but the implementation should be the same. I'm also using bower to manage my dependencies, but that shouldn't change anything on your end.

这个花了我一点时间来整理。我正在使用 SHA1 库,但实现应该是相同的。我也在使用 bower 来管理我的依赖项,但这不会改变你的任何东西。

Solution:

解决方案:

In it's simplest implementation, you want to include the Crypto dependency after all your NG dependencies are wired (this is typically at the end of your index.html). For me I include

在最简单的实现中,您希望在连接所有 NG 依赖项之后包含 Crypto 依赖项(这通常在index.html. 对我来说,我包括

<script src="https://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha1.js"></script> 

after my last NG dependency which is

在我最后一个 NG 依赖之后

<script src="bower_components/angular-route/angular-route.js"></script>

Then I add all my angular scripts (controllers, services, etc).

然后我添加我所有的 Angular 脚本(控制器、服务等)。

If you're using Bower, you can install the crypto library via

如果您使用 Bower,则可以通过以下方式安装加密库

bower install https://crypto-js.googlecode.com/svn/tags/3.1.2/build/rollups/sha1.js --save

from there you can call CryptoJS.SHA1('some string');

从那里你可以打电话 CryptoJS.SHA1('some string');

note that the value you pass in has to be a string

请注意,您传入的值必须是字符串

You can call CryptoJS.SHA1('some string').toString();to get the hashed value.

您可以调用CryptoJS.SHA1('some string').toString();以获取散列值。

Pro tip:

专家提示:

You can also create a factory that you can inject into all your controls to better manage your dependencies. In my case, I went from MD5 to SHA-1 in about 20min, and this saved a ton of time.

您还可以创建一个工厂,您可以将其注入所有控件以更好地管理您的依赖项。就我而言,我在大约 20 分钟内从 MD5 转到了 SHA-1,这节省了大量时间。

angular.module('someApp')
.factory('crypt', function () {
    return {
        hash: function (value) {
            var str = JSON.stringify(value);
            return CryptoJS.SHA1(str).toString();
        }
    };
});

For testing:

供测试用:

If you're using karma and jasmine to test your app, don't forget to include the path of the crypto library to your karma.conffile in the filessection. Otherwise you'll get a persistent Can't find variable: CryptoJSerror.

如果您使用 karma 和 jasmine 来测试您的应用程序,请不要忘记karma.conf在该files部分中将加密库的路径包含到您的文件中。否则你会得到一个持续的Can't find variable: CryptoJS错误。