S
S
Saint7392019-05-30 15:11:42
Vue.js
Saint739, 2019-05-30 15:11:42

How to pass an object to all components (global visibility)?

To understand the essence of the issue, I created a sandbox.
How to make the "client" object from main.js visible in the App.vue component, its descendants and other js files
https://codesandbox.io/s/vue-template-qmdr7

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Victor L, 2019-05-30
@Saint739

const createClient = XMPP.createClient({
  jid: "[email protected]",
  password: "1",
  transport: "websocket",
  wsURL: "ws://example.com/xmpp-websocket"
});
Vue.prototype.$createClient = createClient;

export default {
  name: "App",
  created: function() {
    console.log(this.$createClient)
  }
};
</script>

E
E, 2019-05-30
@aylo

option 1 (correct): in the Mounted() method, make a request

export default {
    data() {
        return {
            client: {},
        }
    },
    mounted() {
        var client = XMPP.createClient({
            jid: "[email protected]",
            password: "1",
            transport: "websocket",
            wsURL: "ws://example.com/xmpp-websocket"
        });

        this.client  = client 
    }
};

option 2 (not quite right): https://codesandbox.io/s/vue-template-ey7rv

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question