Ok, I’ve got some Vue code and HTML.
For example:
JavaScript
x
9
1
Vue.component('area-selectors-box', {
2
template: `
3
<div class="area-selectors-box">
4
<area-selector v-for="(select, index) in selects" :select="select" :key="index"></area-selector>
5
</div>
6
`,
7
props:['selects']
8
});
9
And in my page:
JavaScript
1
2
1
<div is="area-selectors-box" v-bind:selects="selects"></div>
2
It all works, but if I read the page source, I see the v-bind:selects="selects"
portion, which is not standard compliant, I guess.
Similarly, other components have other object properties like:
JavaScript
1
5
1
Vue.component('area-option', {
2
template: `<option :area="area" :value="area.term_id">{{area.name}}<slot></slot></option>`
3
,props: ['area']
4
});
5
which on the page source evaluates to something like:
JavaScript
1
2
1
<option area="[object Object]" value="82">Europa</option>
2
which is obviously not desirable.
How do I bind these properties without them appearing in the DOM as attributes?
Advertisement
Answer
My own answer is: pass props when you call the component, not in the component itself. This code will work and produce clean html:
In-page HTML:
JavaScript
1
3
1
<!-- pass selects to area-selectors-box -->
2
<div is="area-selectors-box" :selects="selects"></div>
3
JS:
JavaScript
1
24
24
1
Vue.component('area-selectors-box', { // pass select to area-selector
2
template: `
3
<div class="area-selectors-box">
4
<area-selector v-for="(select, index) in selects" :select="select" :key="index"></area-selector>
5
</div>
6
`,
7
props:['selects']
8
});
9
10
Vue.component('area-selector', { // pass area to area-option
11
template: `
12
<select @change="selected" >
13
<option disabled selected value="">Seleziona continente</option>
14
<area-option v-for="area in select.areas" :area="area" :key="area.term_id"></area-option>
15
</select>
16
`
17
,props:['select']
18
});
19
20
Vue.component('area-option', { // pass nothing
21
template: `<option :value="area.term_id">{{area.name}}<slot></slot></option>`
22
,props: ['area']
23
});
24