How to define common constants in Vue.js?

Sometimes, we want to define common constants in Vue.js.

In this article, we’ll look at how to define common constants in Vue.js.

How to define common constants in Vue.js?

To define common constants in Vue.js, we can create a module that export an object with the constant values.

For instance, in deliverMethods.js, we write

const DELIVERY = "Delivery";
const CARRIER = "Carrier";
const COLLATION = "Collation";
const CASH_AND_CARRY = "Cash and carry";

export default {
  DELIVERY,
  CARRIER,
  COLLATION,
  CASH_AND_CARRY,
};

to export an object with some constants.

Then in our components, we can import our constant file and use it by writing

import DELIVER_METHODS from "./deliverMethods";

console.log(DELIVER_METHODS.CARRIER);

Conclusion

To define common constants in Vue.js, we can create a module that export an object with the constant values.