背景
vue3 引用 vue-i18n 报错 Refused to evaluate a string as JavaScript because ‘unsafe-eval‘
环境
- vue: ^3.2.13
- vue-i18n: ^9.9.0
错误信息
Uncaught (in promise) EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self' 'wasm-unsafe-eval' 'inline-speculation-rules' http://localhost:* http://127.0.0.1:*".
解决方案
新增 Webpack 配置 :__INTLIFY_JIT_COMPILATION__
配置为 true
vue.config.js 具体配置方式:
...
const plugins = [
new CopyWebpackPlugin({
patterns: copyFiles
}),
// 解决 引用 vueI18n EvalError: Refused to evaluate a string as JavaScript because 'unsafe-eval' is not an allowed source of script in the following Content Security Policy directive: "script-src 'self
new webpack.DefinePlugin({
__INTLIFY_JIT_COMPILATION__: JSON.stringify(true) // 设置为true启用即时编译
})
];
module.exports = {
...
configureWebpack: {
...
plugins,
}
...
}
具体原因
https://vue-i18n.intlify.dev/guide/advanced/optimization#jit-compilation