I am learning Vue.js 2. I want to create a custom component like this: <bs-container fluid="true"></bs-container> and I would like Vue.component() to take care of the bootstrap 3 container classes behind the scenes based on the boolean value passed in the fluid prop, because <bs-container fluid="true"></bs-container> looks much cleaner than div class="container-fluid></div>. This is my attempt so far, but it's not working:
HTML:
<div id="app" >
<bs-container fluid="false">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</bs-container>
JS:
Vue.component('bs-container',{
props: ['fluid'],
template: '<div class="setClass"><slot></slot></div>',
computed: {
setClass: function() {
console.log('setClass has been called');
if (this.fluid == true) {
return 'container-fluid';
} else{
return 'container';
}
}
}
});
new Vue({
el: '#app'
});
The setClass method is not being invoked. What am I missing?
v-bind:class="setClass"or:class="setClass"on your templatev-bind:fluid="false", otherwise you're binding to a string, not a boolean. With these 2 edits it will work fine.<bs-container>to know anything about the VueJS. I am trying to keep the html side as much user-friendly as possible. How about this: I change the if-statement from thisif (this.fluid == true)toif (this.fluid == 'true')this?