- Vue.js 2.x by Example
- Mike Street
- 563字
- 2021-07-02 20:00:31
Creating a repeatable component
The beauty of components is being able to use them multiple times in the same view. This gives you the ability to have one single "source of truth" for the layout of that data. We're going to make a repeatable component for our people list and a separate component for the filtering section.
Open your people listing code you created in the last couple of chapters and create a new component titled team-member. Don't forget to define the component before your Vue app is initialized. Add a prop to the component to allow the person object to be passed in. For validation purposes, only specify that it can be an Object:
Vue.component('team-member', {
props: {
person: Object
}
});
We now need to integrate our template into the component, which is everything inside (and including) the tr in our View.
The template variable in the component just accepts a normal string without new lines, so we need to do one of the following:
- inline our HTML template—great for small templates but in this case will sacrifice readability
- add new lines with the + string concatenation—great for one or two lines, but would bloat our JavaScript
- create a template block—Vue gives us the option to use external templates that are defined in the view using the text/x-template syntax and an ID
As our template is quite big, we are going to choose the third option of declaring our template at the end of our view.
In your HTML, outside of your app, create a new script block and add a type and ID attribute:
<script type="text/x-template" id="team-member-
template">
</script>
We can then move our person template into this block and remove the v-for attribute—we'll still use that in the app itself:
<script type="text/x-template" id="team-member-
template">
<tr v-show="filterRow(person)">
<td>
{{ person.name }}
</td>
<td>
<a v-bind:href="'mailto:' + person.email">{{
person.email }}</a>
</td>
<td v-bind:class="balanceClass(person)">
{{ format(person, 'balance') }}
</td>
<td>
{{ format(person, 'registered') }}
</td>
<td v-bind:class="activeClass(person)">
{{ format(person, 'isActive') }}
</td>
</tr>
</script>
We now need to update the view to use the team-member component instead of the fixed code. To make our view cleaner and easier to understand, we are going to utilize the <template> HTML attribute mentioned earlier. Create a <template> tag and add the v-for loop we had before. To avoid confusion, update the loop to use inpidual as the variable for each person. They can be the same, but it makes the code easier to read if the variables, components, and props have different names. Update the v-for to be v-for="inpidual in people":
<table>
<template v-for="inpidual in people">
</template>
</table>
Inside the template tags of your view, add a new instance of the team-member component, passing the inpidual variable to the person prop. Don't forget to add v-bind: to the person prop, otherwise, the component will interpret it as a fixed string with the value of the inpidual:
<table>
<template v-for="inpidual in people">
<team-member v-bind:person="inpidual"></team-
member>
</template>
</table>
We now need to update the component to use the template we have declared using the template property and the ID of the script block as the value:
Vue.component('team-member', {
template: '#team-member-template',
props: {
person: Object
}
});
Viewing the app in the browser will create several errors in the JavaScript console. This is because we are referencing several methods that are no longer available - as they are on the parent Vue instance, not on the component. If you want to verify that your component is working, change the code to only output the name of the person, and press refresh:
<script type="text/x-template" id="team-member-
template">
<tr v-show="filterRow()">
<td>
{{ person.name }}
</td>
</tr>
</script>
- Power Up Your PowToon Studio Project
- 自己動手寫Java虛擬機
- 數據結構(Python語言描述)(第2版)
- Mastering Scientific Computing with R
- 你不知道的JavaScript(中卷)
- Visual FoxPro程序設計習題集及實驗指導(第四版)
- Django實戰:Python Web典型模塊與項目開發
- Data Science Algorithms in a Week
- Applied Deep Learning with Python
- Java編程指南:語法基礎、面向對象、函數式編程與項目實戰
- Elastix Unified Communications Server Cookbook
- Learning Google Apps Script
- PHP從入門到精通(微視頻精編版)
- 軟件工程實用教程 (第3版)
- C#程序開發教程