问题
How to add directives in nuxt:
Note: this is only for client-side
1- /plugins/directives.js:
import Vue from 'vue'
Vue.directive('getelement', {
bind(el, { value: { index, items } }) {
el.addEventListener('click', (event) => {
event.stopPropagation()
console.log(el, index, items)
// do whatever
})
}
})
2- /nuxt.config.js
plugins: [
...
// DIRECTIVES : client ONLY
{ src: '~/plugins/directives.js', mode: 'client' }
]
3- component.vue
<img
v-for="(item, index) in items"
v-getelement="{ items, index}"
:key="index"
:src="item.src"
/>
来源:https://stackoverflow.com/questions/60544698/solved-add-vue-directives-in-nuxt-client-side-only