I'm creating a loading bar plugin for VueJS and I want to control data of a VueJS component (part of the plugin) with the plugin.
So, in the end I want to do the following:
Include plugin in main.js
import VueLoadingBar from 'path-to-plugin';
Vue.use(VueLoadingBar);
Include plugin component in App.vue
<template>
<div id="app">
<vue-loading-bar><vue-loading-bar>
</div>
</template>
In various components I want to animate the progress bar (like e.g. Youtube) with this.$loadingBar.start().
My plugin consists of a plugin JavaScript file...
import LoadingBar from './LoadingBar.vue';
const vueLoadingBar = {
install () {
const loadingBarModule = {
start () {
// Start animating by setting the computed `progress` variable in the component, for simple
// demonstration with setInterval
setInterval(() => {
// How do I set `progress` that updates the component. Or is there an even better way to solve this?
}, 500);
}
}
Vue.component('vue-loading-bar', LoadingBar);
Vue.prototype.$loadingBar = loadingBarModule;
}
}
export default vueLoadingBar;
...and a .vue file
<template>
<div class="c-loading-bar" :style="style"></div>
</template>
<script>
export default {
computed: {
style () {
return {
transform: `translateX(${this.progress}%)`,
}
},
progress() {
return 0;
}
/* other computed data */
}
}
</script>
<style>
.c-loading-bar {
position: fixed;
top: 0;
z-index: 20;
height: 5px;
background: red;
}
</style>
What's the best way to "control" the LoadingBar component from within the plugin (e.g. this.$loadingBar.start())?
this.$loadingBar). How would you receive the prop for progress and fire events in a plugin?