2017-07-18 131 views
1

我在IntelliJ和Play Framework中創建了一個Angular2應用程序。下面的代碼是給錯誤system.import腳本沒有在Angular中執行

<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.

回答

0

內嵌JavaScript是不是在一個HTML塊允許的。你最好的選擇是將代碼移動到一個新的.js文件中,然後從該塊中調用該文件。

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

而且在newscript.js

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

結帳this reference了爲什麼一個內嵌的腳本是不允許的更多細節。

+0

仍然是同樣的錯誤 - 錯誤:拒絕評估字符串爲JavaScript,因爲'unsafe-eval'不是下面內容安全策略指令允許的腳本源:「default-src'self'」。 實例化http:// localhost:9000/assets/app/main.js' –