【问题标题】:Using Angular 2 in Chrome packaged app在 Chrome 打包应用程序中使用 Angular 2
【发布时间】:2016-06-25 14:29:05
【问题描述】:

我正在尝试使用 Angular 2 创建一个 chrome 打包的应用程序。但是当我尝试运行我的应用程序时出现以下错误:

EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not a allowed source of script in the following Content Security Policy directive: "default-src 'self' blob: filesystem: chrome-extension-resource

-> 评估 chrome-extension://aabbecghjjmmpbagelfmhllgaidcbnmn/app/boot.js

boot.js的内容是:

System.config({ packages: { app: { format: 'register', defaultExtension: 'js', "defaultJSExtensions": true, } } });
System.import('app/boot').then(null, console.error.bind(console));

我知道 AngularJS (angular 1) 有一个 ng-csp 指令来修复这个 Content Security Policy 错误。 Angular 2 有什么相似之处吗?

有没有办法在打包的应用程序中运行 Angular 2?

【问题讨论】:

  • @EricMartinez 我以前看过这两个 Github 主题。第一个提供了“一种方法”,可以通过使用bind(ChangeDetector)... 解决此问题,但从那时起情况发生了变化。我不确定如何在beta8 版本中做到这一点。第二个链接让我觉得 Angular 2 还没有为 Chrome 打包应用做好准备?

标签: angular google-chrome-app


【解决方案1】:

这是解决问题的简短答案。

将以下内容添加到您的 Chrome 扩展程序或 App Manifest.json

"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'"

tldr;

Chrome 开发者 - 扩展 内容安全策略 (CSP) https://developer.chrome.com/extensions/contentSecurityPolicy

这是来自 GitHub 的答案

chrome 应用程序中的 CSP 与 Angular 2 #5956 https://github.com/angular/angular/issues/5956#issuecomment-180135425

这是 AngularJS 中描述的问题 https://docs.angularjs.org/api/ng/directive/ngCsp

【讨论】:

  • 不,这仅适用于 chrome 扩展,问题是关于 PACKAGED APPS。
【解决方案2】:

所以您的错误消息显示eval 是问题所在。您是否在为您的 Angular 2 应用程序使用即时编译?我意识到这是一个老问题,但如果您使用 AOT(Ahead-of-Time)编译,则不需要为您的模板使用 eval

https://angular.io/guide/aot-compiler

更好的安全性

AOT 将 HTML 模板和组件编译成 JavaScript 文件很长 在将它们提供给客户之前。没有要阅读的模板,也没有 有风险的客户端 HTML 或 JavaScript 评估,更少 注入攻击的机会。

另一种选择是使用 <webview> 来托管您的 Angular 2 应用程序: https://developer.chrome.com/apps/tags/webview

这样,您的网页内容将不会被沙盒化,Chrome 应用程序的 CSP 要求将不适用。但是,您将无法直接从 Angular 应用程序访问 Chrome 应用程序 API。解决方案是在 Angular 应用和托管它的 Chrome 应用之间使用消息传递。 Angular 应用向主机发送消息,主机调用 Chrome 应用 API 并将结果发送回 Angular 应用页面:

https://developer.chrome.com/apps/app_external#postMessage

【讨论】:

    【解决方案3】:

    使用 SystemJS Build https://github.com/systemjs/builder 打包到一个应用程序文件中。

    然后将其添加到index.html中,这样

    <script src="angular2-polyfills.js"></script> 
    <script src="app.min.js" ></script>
    

    你可以修复

    EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not a allowed source of script in the following Content Security Policy directive: "default-src 'self' blob: filesystem: chrome-extension-resource

    在 manifest.json 中使用沙箱 https://developer.chrome.com/apps/manifest/sandbox

    【讨论】:

    • 您真的能够让 Angular 应用程序在无评估模式下运行吗?我尝试了许多不同的方法,但除非您以前取得过成功,否则不想走系统构建者的道路
    【解决方案4】:

    您可以在 Angular 2 中看到这个错误问题

    https://github.com/angular/angular/issues/5956

    【讨论】:

    • 这应该是评论,而不是答案。
    猜你喜欢
    • 2013-05-22
    • 1970-01-01
    • 2012-12-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-24
    相关资源
    最近更新 更多