vue.js - 何不在开发中加载头脚本
问题描述
在我的 nuxt.config.js 中,我在 html 头配置中有一个脚本。
export default {
// Target: https://go.nuxtjs.dev/config-target
target: 'static',
// Global page headers: https://go.nuxtjs.dev/config-head
head: {
title: 'My cool Website !',
script: [
{
hid: "thesemetrics",
src: "https://unpkg.com/thesemetrics@latest",
async: true,
type: "text/javascript",
},
{ src: 'https://identity.netlify.com/v1/netlify-identity-widget.js'}
]
},
// ...
}
当我处于开发模式时,有没有办法不加载此脚本?
解决方案
使用 的函数形式head
,并基于 有条件地添加这些脚本process.env.NODE_ENV
:
export default {
head() {
const productionScripts =
process.env.NODE_ENV === 'production'
? [
{
hid: "thesemetrics",
src: "https://unpkg.com/thesemetrics@latest",
async: true,
type: "text/javascript",
},
{ src: 'https://identity.netlify.com/v1/netlify-identity-widget.js'}
]
: []
return {
title: 'My cool Website !',
script: [
// other scripts to always load here
...productionScripts
]
}
},
// ...
}
推荐阅读
- javascript - 在 gadgets.io.MethodType.PUT 中出现错误,出现错误 No content to map to Object 由于输入结束
- sql - 如何在合并条件“WHEN NOT MATCHED BY Source”中使用“Or”
- javascript - 用负数和正数对字符串数组进行排序
- java - 如果列表为空,它会在应用格式化日期时抛出异常。
- jenkins - Jenkins xUnit 插件的控制参数
- php - 循环分配
- ios - 无需注册的 Firebase 唯一 ID
- chef-infra - 资源和提供者之间的厨师区别
- r - 如何同时在 R (ggplot2) 中创建条形图和折线图
- javascript - 如果 Firebase 数据使用 Javascript 发生更改,如何调用函数