Vue.js directive to show text values from deep objects without any errors
This is meant to avoid this type of error when you are using deep objects, that usually can happen when you're expecting a deep object after a Promise
<span>{{ a.b.c.d }}</span>
[Vue warn]: Error in render: "TypeError: Cannot read property 'c' of undefined"
$ npm install v-safe --save
$ yarn add v-safe
You can import in you main.js
file
import Vue from 'vue'
import VSafe from 'v-safe'
Vue.use(VSafe)
You can import as a Nuxt.js plugin
~/plugins/v-safe.js
import Vue from 'vue'
import VSafe from 'v-safe'
Vue.use(VSafe)
and then import it in your nuxt.config.js
file
plugins: [
'~/plugins/v-safe.js'
]
<template>
<span v-safe="'a.b.c.d'"></span>
</template>
<script>
export default {
data:() => ({
a: {
b: {
c: {
d: 'safe'
}
}
}
})
}
</script>
Fork the project and enter this commands in your terminal
git clone https://github.com/YOUR_GITHUB_USERNAME/v-safe.git
cd v-safe
yarn
MIT © guastallaigor