index.js
644 Bytes
// 针对template模板中使用可选链操作符
const chaining = {
install(vue) {
const optionalChaining = (obj, ...rest) => {
let tmp = obj
for (const key in rest) {
const name = rest[key]
tmp = tmp?.[name]
}
return tmp || ""
}
// const test = (obj, ...rest) => {
// let tmp = obj
// for (const key in rest) {
// const name = rest[key]
// tmp = tmp?.[name]
// }
// return tmp || ''
// }
// 添加实例方法
vue.prototype.$$ = optionalChaining
// vue.prototype.$sy = test
}
}
export default chaining