I am wondering what the cleanest way to expose data properties on a single-file Vue component without polluting the global namespace is.
In my main entry file(app.js) for Vue I am setting up Vue like so:
import components from './components/components';
window.app = new Vue({
el: '#vue',
store,
// etc.
});
My components.js
imports all the components that I want to use as HTML snippets. Some of these components are import components on their own that are not directly set as components on my root
instance.
What would be the best way to expose some data properties of certain single-file Vue components?
For instance I have a Search.vue
component where I would like to send my first 3 objects from an array of results to Google Analytics:
// Expose this on `Search.vue`:
data: {
// Filled with data from ajax request.
results: []
}
My Vue instance is available globally. Is there an easy way to access a certain component perhaps by name or something?
Edit
So far the best option currently looks like accessing my property(which I have available inside my store) through a getter like so:
this.$store.getters.propertyINeed
Any suggestions on how to improve on this are welcome.
Vuex
, can you put the data in the Vuex store instead? – kevguyapp.$store
and calling my getters property on this object? – Stephan-v