Categories
JavaScript Answers

How to use Lodash in all Vue component templates?

Spread the love

Sometimes, we want to use Lodash in all Vue component templates.

In this article, we’ll look at how to use Lodash in all Vue component templates.

How to use Lodash in all Vue component templates?

To use Lodash in all Vue component templates, we can add Lodash into Vue‘s prototype.

For instance, we write

import _ from "lodash";

Object.defineProperty(Vue.prototype, "$_", { value: _ });

to call Object.defineProperty with Vue.prototype, "$_", and { value: _ } to add the $_ property into Vue.prototype.

Then we can access Lodash with this.$_ or $_ in the component methods and template respectively.

Conclusion

To use Lodash in all Vue component templates, we can add Lodash into Vue‘s prototype.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *