I have created a component inside my index.js
(where my main Vue code is). Now I would like to modularize the project and put the component into a separate file. I don’t know how to do that because if I create e.g. optionalapm.js
the main index.js
returns an error that it can’t locate the vue component optionalapm
which I included.
How can I import the component into the index.js
?
Here’s my code of the component:
JavaScript
x
29
29
1
var optionalapm=Vue.component('optionalapm', {
2
props:['value'],
3
template: `<div class="col-l-12 col-m-12 col-s-12 col-xs-emphased" style="background-color: #f9f9f9"">
4
<p class="hidden-xl hidden-l hidden-m"></p>
5
<p class="hidden-xl hidden-l hidden-m"></p>
6
<h3 style="text-align: center">APM</h3>
7
<p> </p>
8
<div class="col-l-4">
9
<p style="text-align: center">Number of nodes</p>
10
<div class="form-input-set">
11
<select v-bind:value='value' v-on:input="$emit('input', $event.target.value)" v-on:change="$emit('calcapmprice')" class="form-select" style="background: white">
12
<option value="apmnodes0">
13
<p style="text-align: center">0</p>
14
</option>
15
<option value="apmnodes1">
16
<p style="text-align: center">1</p>
17
</option>
18
<option value="apmnodes2">
19
<p style="text-align: center">2</p>
20
</option>
21
<option value="apmnodes3">
22
<p style="text-align: center">3</p>
23
</option>
24
</select>
25
</div>
26
</div>
27
</div>`,
28
})
29
And that’s the relevant code in the index.js
:
JavaScript
1
14
14
1
var ececontent = new Vue({
2
el:'#ece-content',
3
data: {
4
5
},
6
methods: {
7
8
},
9
components: {
10
optionalapm: optionalapm,
11
}
12
13
});
14
Advertisement
Answer
You should use Single File Components, as described in the documentation. Next to it, read this useful blog post about using SFC’s.