optional-chaining链式取值

示例

const obj = {
  foo: {
    bar: {
      baz: 42,
    },
  },
};

const baz = obj?.foo?.bar?.baz; // 42

const safe = obj?.qux?.baz; // undefined

// Optional chaining and normal chaining can be intermixed
obj?.foo.bar?.baz; // Only access `foo` if `obj` exists, and `baz` if
                   // `bar` exists

// Example usage with bracket notation:
obj?.['foo']?.bar?.baz // 42
npm install --save-dev @babel/plugin-proposal-optional-chaining

babel.config.js中添加plugins: ["@babel/plugin-proposal-optional-chaining"]

module.exports = {
  presets: [
    '@vue/app'
  ],
  plugins: ["@babel/plugin-proposal-optional-chaining"]
}

目前只能在js中使用(编辑器报错不用管),template中还是会报错。

参考文章

https://babeljs.io/docs/en/next/babel-plugin-proposal-optional-chaining

你可能感兴趣的:(optional-chaining链式取值)