Skip to the content.

Working with a Constants Folder

TL;DR

You can define constants used throughout the site with the following methods:

1. Create a Separate Constants File (constants.ts)

Creating a constants.ts file to collect constants in a centralized location helps maintain project organization and provides easy access to constants.

For example, you can create a constants.ts file in the src folder of your project:

// src/constants.ts
export const MYCONSTANTS = {
  API_BASE_URL: 'https://api.example.com',
  MAX_SEAT_SELECTION: 4,
  SEAT_TYPES: {
    AVAILABLE: 'available',
    TAKEN: 'taken',
    RESERVED: 'reserved'
  },
  GENDER: {
    MALE: 'male',
    FEMALE: 'female'
  }
};

2. Using Constants in the Project

You can import the constants.ts file wherever needed:

// For example, in a Vue component or any TypeScript file
import { MYCONSTANTS } from 'src/constants';

console.log(`API URL: ${MYCONSTANTS.API_BASE_URL}`);
console.log(`Max seat selection: ${MYCONSTANTS.MAX_SEAT_SELECTION}`);
console.log(`Available seat type: ${MYCONSTANTS.SEAT_TYPES.AVAILABLE}`);

3. Using types.ts for Type Definitions

If you want to define types for the constants, you can add them to a types.ts file:

// src/types.ts
export type EGender = 'male' | 'female';
export type ESeatType = 'available' | 'taken' | 'reserved';

4. Using Constants in Pinia Store

You can use constants in a Pinia store by importing them similarly:

import { defineStore } from 'pinia';
import { MYCONSTANTS } from 'src/constants';

export const useSeatStore = defineStore('seat', {
  state: () => ({
    maxSeats: MYCONSTANTS.MAX_SEAT_SELECTION,
    seatTypes: MYCONSTANTS.SEAT_TYPES
  })
});

5. Using .env for Environmental Constants

For environment-specific constants like API URLs, you can use the .env file in a Quasar project:

VUE_APP_API_URL=https://api.example.com
console.log(process.env.VUE_APP_API_URL);

Summary

This approach helps keep your constants organized, easily accessible, and manageable.