system.import 脚本无法在 Angular 中执行



我在IntelliJ和Play Framework中创建了一个Angular2应用程序。以下代码给出错误

<script>
System.import('assets/app/main.js').catch(function(err){ console.error(err); });
</script>

错误:Refused to execute inline script because it violates the following Content Security Policy directive: "default-src 'self'". Either the 'unsafe-inline' keyword, a hash ('sha256-ol8t6QICwIETK9dhrEwBShFCgKGwmDHZW/nmyLH+rYs='), or a nonce ('nonce-...') is required to enable inline execution. Note also that 'script-src' was not explicitly set, so 'default-src' is used as a fallback.

内联 JavaScript 不允许在 html 块中使用。最好的办法是将该代码移动到新的.js文件中,然后从块中调用该文件。

<script src="newscript.js"></script>

在新脚本中.js

System.import('assets/app/main.js').catch(function(err){ console.error(err); });

查看此参考,了解有关不允许内联脚本的原因的更多详细信息。

最新更新