I'm trying to use lodash with Nuxt2 Composition API (https://composition-api.nuxtjs.org/)
However, with useContext()
I'm not able to find any global Vue prototype functions. So when I tried it by creating a module with nuxt modules. And I got the $lodash
in useContext()
. But when I call $lodash.all([true, false])
it says $lodash.all is not a function.
The only trick it made the $lodash available in nuxt context was using inject
. I'm sharing my setup.
// modules/lodash/index.js
const path = require('path')
module.exports = function nuxtLodash(moduleOptions) {
const lodashOptions = Object.assign({}, this.options.lodash, moduleOptions)
// Register plugin
this.addPlugin({
src: path.resolve(__dirname, 'plugin.js'),
ssr: false,
fileName: 'lodash.js',
options: {
lodashOptions
}
})
}
// modules/lodash/plugin.js
import Vue from 'vue'
import VueLodash from 'vue-lodash'
import lodash from 'lodash'
Vue.use(VueLodash, { name: '$_', lodash })
export default function(ctx, inject) {
inject('lodash', lodash)
}
// nuxt.config.js
modules: [
// ... other modules
'~/modules/lodash'
],
Now, when I'm using this in my components with composition api, I'm getting an error.
<script>
import {
defineComponent,
useContext
} from '@nuxtjs/composition-api'
export default defineComponent({
setup() {
const { $lodash } = useContext()
console.log($lodash)
console.log($lodash().all([true, false]))
}
})
</script>
Any proper way to use this? What am I missing here?