- Vue.js 2.x by Example
- Mike Street
- 247字
- 2021-07-02 20:00:30
Creating and initializing your component
Vue components are registered using the Vue.component(tagName, options) syntax. Each component must have an associated tag name. The Vue.component registration must happen before you initialize your Vue instance. As a minimum, each component should have a template property—denoting what should be displayed when the component is used. Templates must always have a single wrapping element; this is so the custom HTML tag can be replaced with the parent container.
For example, you couldn't have the following as your template:
<p>Hello</p><p>Goodbye</p>
If you do pass a template of this format, Vue will throw an error in the browser's JavaScript console warning you.
Create a Vue component yourself, with a simple fixed template:
Vue.component('my-component', {
template: '<p>hello</p>'
});
const app = new Vue({
el: '#app',
// App options
});
With this component declared, it would now give us a <my-component></my-component> HTML tag to use in our view.
You can also specify components on the Vue instance itself. This would be used if you had multiple Vue instances on one site and wished to contain a component to one instance. To do this, create your component as a simple object and assign the tagName within the components object of your Vue instance:
let Child = {
template: '<p>hello</p>'
}
const app = new Vue({
el: '#app',
// App options
components: {
'my-component': Child
}
});
For our app though, we are going to stick with the Vue.component() method of initializing our components.
- 自制編譯器
- iOS 9 Game Development Essentials
- Python Network Programming Cookbook(Second Edition)
- Swift細(xì)致入門與最佳實(shí)踐
- 執(zhí)劍而舞:用代碼創(chuàng)作藝術(shù)
- Terraform:多云、混合云環(huán)境下實(shí)現(xiàn)基礎(chǔ)設(shè)施即代碼(第2版)
- 速學(xué)Python:程序設(shè)計(jì)從入門到進(jìn)階
- 一塊面包板玩轉(zhuǎn)Arduino編程
- OpenGL Data Visualization Cookbook
- Java程序員面試筆試寶典(第2版)
- R語言數(shù)據(jù)可視化:科技圖表繪制
- Illustrator CS6設(shè)計(jì)與應(yīng)用任務(wù)教程
- 從零開始學(xué)Android開發(fā)
- Continuous Delivery and DevOps:A Quickstart Guide Second Edition
- Test-Driven iOS Development with Swift