M
M
Michael R.2018-10-18 19:41:27
JavaScript
Michael R., 2018-10-18 19:41:27

VueJS - how to properly make a modular component?

Greetings!
Through WebPack 4 + VueJS, I build the application and get an error.
Let's take a simple application example:

<template>
  <div id="hello" class="class_123">
    <h2>{{message}}</h2>
  </div>
</template>

<script>
  export default {
    name: 'app',
    data () {
      return {
        message: 'Welcome to Vue.js'
      }
    }
  }
</script>

<style>
  .class_123 {
    font-family: Verdana;
    color: navy;
  }
</style>

I break it into modules (template, scripts, styles) with the following structure:
5bc8b5371a77c499821761.pngmain.vue (component's main file):
<template src="./modal.html"></template>
<style src="./modal.css"></style>
<script src="./modal.js"></script>

modal.js:
export default {
  name: 'app',
  data () {
    return {
      message: 'Welcome to Vue.js'
    }
  }
}

modal.css:
.class_123 {
  font-family: Verdana;
  color: navy;
}

modal.html:
<div id="app" class="class_123">
  <h2>{{message}}</h2>
</div>

I get an error:
5bc8b653918fb578268567.pngReturning back to main.vue:
<template>
  <div id="app" class="class_123">
    <h2>{{message}}</h2>
  </div>
</template>

<style src="./modal.css"></style>
<script src="./modal.js"></script>

- works...
That is . he does not like what <template>is in a separate file?
Questions:
1. How to solve this problem?
2. Is it right to split the application in parts like this?
Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Klein Maximus, 2018-10-20
@kleinmaximus

try

<template src="./modal.html" lang="html"></template>

In general, there are different options for the design of the code, since "all felt-tip pens have different tastes and colors." For example, I'm starting to do everything in one file. As soon as the volume exceeds "one screen", I take out the most voluminous part (template, style or script) in a separate file. Although it is the template that, conceived, remains in the single-file component file itself (well, it's just more informative than putting it in another file).
PS: Maybe you have some kind of "linter" configured, and because of this, errors are lying around?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question