1.npm install --save-dev @babel/plugin-proposal-optional-chaining
装包
2. 在ucf.config.js全局配置文件中加入babel_plugins:["@babel/plugin-proposal-optional-chaining"]
项
3. 在VS Code的设置中, 配置 “javascript.validate.enable”: false
2019-12-14 vs code有个好消息
VS Code now ships with TypeScript 3.7.3. This major update brings some TypeScript language improvements - including optional chaining, nullish coalescing, and assertion functions - as well as some new tooling features for both JavaScript and TypeScript. As always, this release also includes a number of important bug fixes.
render() {
const { isFlag } = this.state;
const obj = {
a: 1, b: {
c: 2,
d: {
e: 3
}
}
}
return (
<div style={{ marginLeft: 10 }}>
<p>可选链式表达</p>
正确引用:<span>{obj?.b?.d?.e}</span> <br/>
错误引用:<span>{obj?.f?.d?.e}</span>
</div>
)
}
如果不使用optional-chaining,如【错误引用:{obj.f.d.e}】将是一个严重的Error,浏览器将停止解析并抛出错误,用户将会看到白屏。