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:

Nx Adoption Guide: Overview, Examples, And Alternatives

Nx adoption guide: Overview, examples, and alternatives

Let’s explore Nx features, use cases, alternatives, and more to help you assess whether it’s the right tool for your needs.

Andrew Evans
Mar 28, 2024 ⋅ 9 min read
Understanding Security In React Native Applications

Understanding security in React Native applications

Explore the various security threats facing React Native mobile applications and how to mitigate them.

Wisdom Ekpotu
Mar 27, 2024 ⋅ 10 min read
Warp Adoption Guide: Overview, Examples, And Alternatives

warp adoption guide: Overview, examples, and alternatives

The warp web framework for Rust offers many enticing features. Let’s see when and why you should consider using warp in your projects.

Ukeje Goodness
Mar 26, 2024 ⋅ 8 min read
Integrating Next Js And Signalr For Enhanced Real Time Web App Capabilities

Integrating Next.js and SignalR to build real-time web apps

In this tutorial, you’ll learn how to integrate Next.js and SignalR to build an enhanced real-time web application.

Clara Ekekenta
Mar 25, 2024 ⋅ 8 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