vuejs
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
6. Components<br />
6.1 What are Components?<br />
Components are one of the most powerful features of Vue.js. They help you extend basic HTML<br />
elements to encapsulate reusable code. At a high level, Components are custom elements that Vue.js’<br />
compiler would attach specified behavior to. In some cases, they may also appear as a native HTML<br />
element extended with the special is attribute.<br />
It is a really clever and powerful way to extend HTML to do new things. In this chapter we are going<br />
to start out with an extremely simple example and next we are going to see how Components can<br />
help us improve the code we have created, in some of the previous chapters.<br />
6.2 Using Components<br />
We are going to start with a simple Component. In order to use a component we have to register it<br />
first.<br />
One way to register a component is to use the Vue.component method and pass in the tag and the<br />
constructor. Think of the tag as the name of the Component and the constructor as the options.<br />
In our occasion, we’ll name the Component story and we’ll define the property story (again). The<br />
option template (how we would like our story to be displayed), is inside the constructor where<br />
other options will be added as well.<br />
Our story component will be registered like this<br />
1 Vue.component('story', {<br />
2 template: 'My horse is amazing!'<br />
3 });<br />
Now that we have registered the component we will make use of it. We will add the custom element<br />
inside the HTML to display the story.<br />
63