Answer the question
In order to leave comments, you need to log in
How to properly store and use individual Vue components on the site pages?
There is a project that uses Vue to implement complex forms, notifications, a comment system, and other elements (which are separate components and are only placed on a certain number of pages).
At the moment I have a vue.app.js file with something like this:
window.Vue = require('vue');
import './util/directives.js'
import store from './store/index.js'
import Vue from 'vue';
import Reviews from './components/Reviews.vue';
import Comments from './components/Comments.vue';
import Searchbox from './components/Searchbox.vue';
import CommentForm from './components/CommentForm.vue';
import ReviewForm from './components/ReviewForm.vue';
import NotificationsWidget from './components/NotificationsWidget.vue';
import Notifications from './components/Notifications.vue';
import AddResourceForm from './components/AddResourceForm.vue';
if ($('#reviews').length > 0) {
new Vue({
el: '#reviews',
store,
components: {
Reviews
},
});
}
// other code
<div id="reviews">
<reviews/>
</div>
if ($('#add-resource-widget').length > 0) {
new Vue({
el: '#add-resource-widget',
store,
components: {
'add-resource-form' : AddResourceForm
},
});
}
<div id="add-resource-widget">
<add-resource-form></add-resource-form>
</div>
Answer the question
In order to leave comments, you need to log in
Yes, you should not bother with separating components into separate files.
Collect one bundle. At least while it is definitely not worth less than 200-300kb.
Then you can use the built-in webpack code splitter / For example, through magic comments
I mount the components themselves like this
<div id="calculator"></div>
export function mountVueComponent(targetId, component) {
if (document.getElementById(targetId)) {
new (Vue.extend(component))().$mount(`#${targetId}`);
}
}
import Calculator from '....';
mountVueComponent('calculator', Calculator);
<div class="mini-calc"></div>
Array.from(document.querySelectorAll('.mini-calc')).forEach(el => {
let yandexGoal = el.dataset['goal'];
new Vue({
el : el,
render: createElement => createElement(Calculator, {
props: {
displayAs: CALC_DISPLAY_AS_MINI,
yandexGoal,
},
}),
});
});
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question