7 Ways To Declare A Component in Vue - Js
7 Ways To Declare A Component in Vue - Js
7 Ways To De ne A Component
Template in VueJS
Anthony Gore Follow
May 4, 2017 · 3 min read
There’s plenty of choice when it comes to defining component templates in Vue. By my count there
are at least seven different ways:
String
Template literal
X-Templates
Inline
Render functions
JSX
Single page components
And maybe more!
In this article we’ll go through examples of each and address the pros and cons so you know which
one is the best to use in any particular situation.
Note: this article was originally posted here on the Vue.js Developers blog on 2017/03/27
1. Strings
By default a template will be defined as a string in your JS file. I think we can all agree that
templates in a string are quite incomprehensible. This method doesn’t have much going for it other
than the wide browser support.
1 Vue.component('my-checkbox', {
2 template: `<div class="checkbox-wrapper" @click="check"><div :class="{ checkbox: true,
3 data() {
4 return {
5 checked: false,
6 title: 'Check me'
7 }
8 },
9 methods: {
10 check() {
11 this.checked = !this.checked;
12 }
13 }
14 });
. . .
Take A Free Vue.js Introduction Course! Learn what Vue is, what kind of apps you can build with it,
how it compares to React & Angular, and more in this 30-minute video introduction. Enroll for free! Upgrade
2.9K claps
WRITTEN BY
More from Vue.js Developers More from Vue.js Developers More from Vue.js Developers
Top highlight
How I made it easy to develop How to build a Reusable Vuejs Creating a todo -app using
on Vue.js with server-side modal component VueJS and Framework7
rendering Jakz Aizzat in Vue.js… Natalia Sulkama in Vue.js…
Jul 7 · 3 min read 69 Jul 1 · 8 min read 81
Georgy Perepecho in…
235
Jun 28 · 5 min read