Vue.js uses Virtual Dom to render components, so if you need to have 10 cards on a page for example, it’s more practical to create only 1 card component and render them 10x using only one base component. In practice this saves the programmer trouble, makes the code cleaner and readable and makes its maintenance simpler and at the end of the accounts really, if you look at your html code in the browser, you will see the html code of the cards 10 times as it rendered the 10x in the view.
Let me give you an example in code:
This is your "card component":
<template>
<div class="card">
<h4 class="title">{{cardTitle}}</h4>
<img :src="cardImageUrl" />
<p class="text">
{{cardDescription}}
</p>
</div>
</template>
<script>
export default {
props: {
title: {
required: true,
type: String
},
description: {
required: true,
type: String
},
imgUrl: {
required: false,
type: String
}
},
data () {
return {
cardTitle: this.title,
cardDescription: this.description,
cardimgUrl: this.imgUrl
}
}
}
</script>
With this you have a simple component using props, to use it on other pages just import it into and instantiate it in the Components, thus:
<script>
import card from '../sharedComponents/card';
export default {
components: {
'my-card' : card,
}
},
</script>
and to use the same, use
<my-card :title="titulo card 1" :description="descrição card 1" :imgUrl="https://imagens/15163"></my-card>
Web Component and string templates in Vue are very different questions... I suggest separate. In relation to string templates it is no more than using multi lines for the code to be more readable. Instead of having a long single line template.
– Sergio
It seems that you are confusing things a little, the documentation uses the property
template
to create a component because the examples are short. In an actual application you are more likely to use Single File Components, but properties can also be usedtemplate
,el
andrender
. The quote you put in your question only says you’ll use String Templates so that the HTML code is more readable, because with string templates you can break line without problems. It has no big mystery.– fernandosavio
With Sergio mentioned, I recommend deleting the "Other doubt" part of your question... Another question is another question.
– fernandosavio