avatarJohn Philip

Summary

The article provides guidance on adding global properties in a Vue.js 3 application for reuse across components.

Abstract

The article outlines the benefits of using global properties in Vue.js 3 applications, emphasizing the importance of reusability and consistency. It instructs developers on how to declare these properties using app.config.globalProperties, recommending prefixing them with a dollar sign to avoid conflicts. The global properties are then accessible throughout the application, enhancing code maintainability and reducing redundancy. The article also demonstrates how to access these properties within any component using getCurrentInstance(). Resources for further reading and encouragement for community engagement conclude the guide.

Opinions

  • The author emphasizes the convention of prefixing global properties with a dollar sign to prevent naming conflicts, suggesting a best practice in Vue.js development.
  • Accessing global properties through getCurrentInstance() is presented as a straightforward method, indicating the author's appreciation for Vue.js's API design.
  • The mention of Vue.js 2's End of Life Support implies a forward-looking perspective, advocating for the adoption of Vue.js 3.
  • Encouraging readers to share the guide and connect on social media reflects the

VueJS Tip: Add Global Properties to Your VueJS App

Photo by Fatos Bytyqi on Unsplash

Often at times when developing application, there are instances where certain properties need to be reused throughout the application. Declaring these variables is beneficial as they can be modified once and then utilized consistently across different parts of the application.

Vue.js offers convenient methods to declare global properties, making them reusable throughout the application. In this article, we will look at how to add and use these global properties.

It’s important to note that we’ll be working with Vue.js 3, as Vue.js 2 has reached its End of Life Support (EOL).

Vuejs Pun

Too lazy to react. I only vue !

How To Add Global Properties

It is conventionally recommended to prefix these global properties with a dollar sign or a chosen symbol to prevent potential naming conflicts with other variables within your application.

In the main entry file of your application mainly it could be main.ts or main.js file, you need to add the following code. We will leverage the app.config.globalProperties API.

app.config.globalProperties​

An object that can be used to register global properties that can be accessed on any component instance inside the application. Vuejs Documentation.

app.config.globalProperties.$AppName = 'ChiziKarogwaTena'

With the declaration above, $AppName becomes available in our application instance, allowing us to access it from any part of our application.

Accessing Global Properties

In your component file, you can access the global variable using the getCurrentInstance() API and accessing it by its name. Check how we access our global property in the code below.

<script setup>
import { getCurrentInstance } from "vue";
const { proxy } = getCurrentInstance();


console.log(proxy.$AppName) //ChiziKarogwaTena
</script>

The getCurrentInstance function gives us access to the low-level instance of the application, and with the proxy, we can access the global variable using the name we provided.

Resources

Before You Go

Thank you for taking the time to go through this guide. If you found it helpful, sharing it with others would be highly appreciated.

Whether you have questions, suggestions, or simply want to chat, feel free to connect with me on X via @amjohnphilip. Your engagement is valued!

More Reads

Technology
Software Development
Web Development
Progamming
JavaScript
Recommended from ReadMedium