- Vue.js 2 Web Development Projects
- Guillaume Chau
- 218字
- 2021-07-02 22:34:32
Defining and using the components
We will write our UI components in a new file:
- Create a components folder and a new ui.js file inside it. Include it in the main index.html page, just before the main script:
<!-- Scripts -->
<script src="utils.js"></script>
<script src="cards.js"></script>
<script src="state.js"></script>
<script src="components/ui.js"></script>
<script src="main.js"></script>
In this file, we will register our components, so it's important that the main Vue instance is created afterward, and not before. Else, we would get errors of components not being found.
To register a component, we can use the global Vue.component() function. It takes two arguments; the name under which we register the component, and its definition object, which is using the exact same options as the Vue instance that we already know.
- Let's create the top-bar component in the ui.js file:
Vue.component('top-bar', {
template: `<p class="top-bar">
Top bar
</p>`,
})
Now, we can use the top-bar component in our templates, just like any other HTML tags, for instance, <top-bar>.
- In the main template, add a new top-bar tag:
new Vue({
// ...
template: `<p id="#app">
<top-bar/>
</p>`,
})
This template will create a new top-bar component and render it inside the #app element, using the definition object we just defined. If you open the devtools, you should see two entries:

Each one is a Vue instance--Vue actually created a second instance using the definition we provided for the top-bar component.
- The Supervised Learning Workshop
- 微服務(wù)與事件驅(qū)動(dòng)架構(gòu)
- Web開(kāi)發(fā)的貴族:ASP.NET 3.5+SQL Server 2008
- 新一代SDN:VMware NSX 網(wǎng)絡(luò)原理與實(shí)踐
- Mastering React
- Learning Docker Networking
- Functional Python Programming
- Python計(jì)算機(jī)視覺(jué)與深度學(xué)習(xí)實(shí)戰(zhàn)
- Pandas 1.x Cookbook
- Getting Started with RethinkDB
- Scratch少兒編程高手的7個(gè)好習(xí)慣
- Illustrator CS6中文版應(yīng)用教程(第二版)
- Switching to Angular 2
- Flask Web開(kāi)發(fā)實(shí)戰(zhàn):入門、進(jìn)階與原理解析
- Instant SQL Server Analysis Services 2012 Cube Security