我正在尝试运行一个盖茨比网站,但是当我这样做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,
}
什么是??
运营商Javscript?这是新的语言功能吗?
无效的合并运算符(??
)是ECMAScript 2020的一项功能(请参阅TC39建议)。您可以通过修改babel配置或安装gatsby-plugin-nullish-coalescing-operator
插件来添加它。
如果选择第一个版本,请.babelrc
在项目的根目录中添加一个,并使用以下内容进行填充:
{
"plugins": [
["@babel/plugin-proposal-nullish-coalescing-operator"]
],
"presets": [
[
"babel-preset-gatsby",
{
"targets": {
"browsers": [">0.25%", "not dead"]
}
}
]
]
}
注意:您将需要安装@babel/plugin-proposal-nullish-coalescing-operator
依赖项。
Javascript中的此运算符是什么?
??
如果左为null
或,则空合并运算符()使用右值undefined
。OR运算符(之间的主要区别||
)处于falsy值,其中它可以导致一些错误,而在使用""
,0
或false
值(存在诸如多falsy值-0
,NaN
等,但这些都是共同的)。所以:
description: post.intro ?? post.description,
您将一直使用post.intro
它,否则(如果post.intro
是null
或undefined
)将使用post.description
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句