+1 vote
in VueJS by
What are single file components?

1 Answer

0 votes
by

Single File Components are an easy concept to understand. Earlier you might heard about all three parts(HTML, JavaScript and CSS) of your application kept in different components. But Single File Components encapsulate the structure, styling and behaviour into one file. In the beginning, it seems strange to have all three parts in one file, but it actually makes a lot more sense. Let's take an example of Singile File Components

<template>

    <div>

        <h1>Welcome {{ name }}!</h1>

    </div>

</template>

<script>

    module.exports = {

       data: function() {

           return {

               name: 'John'

           }

       }

    }

</script>

<style scoped>

    h1 {

        color: #34c779;

        padding: 3px;

    }

</style>

Is Single File Components violating separation of concerns?

As for the latest modern UI development, separation of concerns is not equal to separation of file types. So it is preferred to divide codebase layers into loosely-coupled components and compose them instead of dividing the codebase into three huge layers that interweave with one another. This way makes Single File Components more cohesive and maintainable by combining template, logic and styles together inside a component. You can also still maintain javascript and CSS files separately with hot-reloading and pre-compilation features. For example,

<template>

  <div>This section will be pre-compiled and hot reloaded</div>

</template>

<script src="./my-component.js"></script>

<style src="./my-component.css"></style>

...