- 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.
- Go Web編程
- R語言數(shù)據(jù)分析從入門到精通
- 復(fù)雜軟件設(shè)計(jì)之道:領(lǐng)域驅(qū)動設(shè)計(jì)全面解析與實(shí)戰(zhàn)
- Python數(shù)據(jù)分析入門與實(shí)戰(zhàn)
- Arduino開發(fā)實(shí)戰(zhàn)指南:LabVIEW卷
- INSTANT OpenNMS Starter
- 小程序開發(fā)原理與實(shí)戰(zhàn)
- Unreal Engine 4 Shaders and Effects Cookbook
- C語言程序設(shè)計(jì)教程
- 精通Python自動化編程
- Terraform:多云、混合云環(huán)境下實(shí)現(xiàn)基礎(chǔ)設(shè)施即代碼(第2版)
- 軟件測試綜合技術(shù)
- Python:Deeper Insights into Machine Learning
- Arduino計(jì)算機(jī)視覺編程
- SCRATCH編程課:我的游戲我做主