Skip to content

Instantly share code, notes, and snippets.

@abdumu
Created January 10, 2020 13:26
Show Gist options
  • Save abdumu/17eeca0bc3cb36fcc938ea73903c43c7 to your computer and use it in GitHub Desktop.
Save abdumu/17eeca0bc3cb36fcc938ea73903c43c7 to your computer and use it in GitHub Desktop.
optional helper for vue
Vue.mixin({ methods: {
optional(variable) {
return new Proxy(typeof variable === undefined || variable === null ? {} : variable, {
get(target, prop) { return prop in target ? target[prop] : null; }
});
},
}})
//usage: {{ optional(user).email }}
@abdumu
Copy link
Author

abdumu commented Jan 10, 2020

information about Proxy in javascript: https://javascript.info/proxy

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment