javascript - Gatsby Config 在 Javascript 文件中给出错误??操作员
问题描述
我正在尝试运行 Gatsby 网站,但是当我这样做时npm run develop
,我收到了这个错误:
> gatsby develop
ERROR #10123 CONFIG
We encountered an error while trying to load your site's gatsby-config. Please fix the error x try again.
Error: D:\Coding\web-blog\src\infra\feed.js:28
description: post.intro ?? post.description,
^
代码:
const item = {
title: sanitizeTitle(post.title),
description: post.intro ?? post.description,
url: `${site.siteMetadata.siteUrl}/${post.slug}`,
guid: `${site.siteMetadata.siteUrl}/${post.slug}`,
categories: post.tags,
author: site.siteMetadata.author,
date: post.date,
}
什么是JavaScript 中的??
运算符?它是一种新的语言功能吗?
解决方案
nullish 合并运算符 ( ??
) 它是 ECMAScript 2020 中的一项功能(请参阅 TC39 提案)。您可以通过修改 babel 配置或安装gatsby-plugin-nullish-coalescing-operator
插件来添加它。
如果您选择第一个版本,.babelrc
请在项目的根目录中添加 a 并使用以下内容填充它:
{
"plugins": [
["@babel/plugin-proposal-nullish-coalescing-operator"]
],
"presets": [
[
"babel-preset-gatsby",
{
"targets": {
"browsers": [">0.25%", "not dead"]
}
}
]
]
}
注意:您需要安装@babel/plugin-proposal-nullish-coalescing-operator
依赖项。
Javascript 中的这个运算符是什么?
如果 left 是or , nullish 合并运算符 ( ??
) 使用正确的值。OR 运算符 ( ) 之间的主要区别在于假值,在,或值中使用时可能会导致一些错误(还有更多的假值,例如,等,但这些是常见的)。所以:null
undefined
||
""
0
false
-0
NaN
description: post.intro ?? post.description,
post.intro
只要它存在,您就会使用,否则(如果post.intro
是null
或undefined
),您将使用post.description
.
推荐阅读
- network-programming - NAT 中的 STUN 打孔不能随机工作
- elasticsearch - sql查询翻译查询DSL kibana
- gradle - 在多平台 js 和 jvm 项目中使用 kotlin-dce-js
- r - 在 R Shiny 界面中渲染 YouTube 链接
- python - 如何从文本中删除或分离表情符号(以“utf-8”编码),如下所示
- c# - 使用 WriteAllText 如何将 DataTable 列名写入 CSV 文件
- reactjs - useCallback 和 useMemo react hook 的缓存大小是多少,为什么?
- phaser-framework - Phaser 3:为什么我的 this.cameras.main.fadeOut 效果没有立即启动?
- amazon-web-services - AWS MediaConvert CMAF 格式 HLS 不起作用
- terraform - 控制 Compute Engine 实例名称