Last active
August 7, 2020 14:21
-
-
Save kticka/b7555cadb39d15d10d26625c78edabe4 to your computer and use it in GitHub Desktop.
Init script to mount multiple vue instances in non-spa projects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vue from "vue"; | |
import Store from './some_vuex_store.js' | |
import Component1 from './some_comp.vue' | |
import Component2 from './some_comp2.vue' | |
window.addEventListener('load', () => { | |
const components = {Component1, Component2}; | |
Object.keys(components).forEach((key) => { | |
components[key] = Vue.extend(Object.assign(components[key], { | |
store: Store | |
})); | |
}); | |
const elements = document.querySelectorAll('[vue-component]'); | |
elements.forEach(e => { | |
const component = e.getAttribute('vue-component'); | |
new components[component]({ | |
el: e, | |
propsData: e.dataset || {} | |
}) | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Html:
<div vue-component="Component1" data-some_prop="some_prop_value"></div>