"@babel/plugin-proposal-optional-chaining": "^7.16.7",
//vue插件
const chaining = {
install(vue) {
const variableOptional = (target) => {
return new Proxy(target, {
get: (target, propKey) => {
const proKeyArr = propKey.split('?.')
return proKeyArr.reduce((a, b) => a?.[b], target)
},
})
}
// 添加实例方法
vue.prototype.$chaining = variableOptional
},
}
export default chaining
//main.js中
import chaining from '@/plugins/chaining'
Vue.use(chaining)