2023-04-26
1763
#vue
Nwose Lotanna
4801
Apr 26, 2023 ⋅ 6 min read

Accessing Vue.js properties globally with globalProperties

Nwose Lotanna Web Developer and Writer

Recent posts:

Rxjs Adoption Guide: Overview, Examples, And Alternatives

RxJS adoption guide: Overview, examples, and alternatives

Get to know RxJS features, benefits, and more to help you understand what it is, how it works, and why you should use it.

Emmanuel Odioko
Jul 26, 2024 ⋅ 13 min read
Decoupling Monoliths Into Microservices With Feature Flags

Decoupling monoliths into microservices with feature flags

Explore how to effectively break down a monolithic application into microservices using feature flags and Flagsmith.

Kayode Adeniyi
Jul 25, 2024 ⋅ 10 min read
Lots of multi-colored blue and purplish rectangles.

Animating dialog and popover elements with CSS @starting-style

Native dialog and popover elements have their own well-defined roles in modern-day frontend web development. Dialog elements are known to […]

Rahul Chhodde
Jul 24, 2024 ⋅ 10 min read
Using Llama Index To Add Personal Data To Large Language Models

Using LlamaIndex to add personal data to LLMs

LlamaIndex provides tools for ingesting, processing, and implementing complex query workflows that combine data access with LLM prompting.

Ukeje Goodness
Jul 23, 2024 ⋅ 5 min read
View all posts

4 Replies to "Accessing Vue.js properties globally with <code>globalProperties</code>"

  1. The prototype member is not provided by Vue, it’s a basic JS language pattern. What this post suggests is generally called “monkey patching”. It’s quite convenient, but might break future implementations of the Vue object.

    If you really needed this you could consider namespacing: Extend the prototype by an object with a unique name which is unlikely to get implemented by others. Then add your extensions inside that object.

  2. Fully agree with this. Rather than saturating vue’s prototype with a heap of clutter, choose a single $ prefixed namespace and then dump all your extensions under that.

    EG:
    vue.prototype.$myextension = {}
    vue.prototype.$myextension.$axios = …
    etc

    Otherwise, even with namespacing if you saturate the top level of the prototype, you’re bound to run into a collision eventually.

  3. Hi, this is very interesting. I have a question. I am developing dynamic content via templates at runtime.

    What this does is to receive any template, however those with bindings don’t work as it gives a ref error. How can I make this be able to see the ‘global’ state in the ‘$data’ attribute. For example my template might have {{ number }}, currently im getting number undefined. So I just want the component to have access to the global state to pick this up.

    Vue.component(“renderstring”, {
    props: {
    string: {
    required: true,
    type: String
    }
    },
    render(h) {
    const self = this
    console.log(this.$data)
    const render = {
    template: “” + this.string + “”,
    }
    return h(render)
    }
    })

    Thanks a lot. #FoundThisCodeOnTheNet

Leave a Reply