官术网_书友最值得收藏!

  • Vue.js Quick Start Guide
  • Ajdin Imsirovic
  • 239字
  • 2021-06-24 18:23:57

Understanding components, templates, and props

To begin, let's look at how to make a component in Vue. First, we specify the component, like this:

Vue.component('custom-article', {
template: `
<article>
Our own custom article component!<span></span>
</article>`
})
new Vue({
el: '#app'
})

A component is a block of code that we give a custom name. This custom name can be anything we come up with, and it's a single label for that entire block of code in the form of a custom HTML tag. In the previous example, we grouped the article and span tags and gave that custom tag the name of custom-article

Components are named using kebab-case.

The code for this component is available as a Codepen at https://codepen.io/AjdinImsirovic/pen/xzpOaJ.

Now, to create an instance of our component, we simply use our <custom-article> opening and closing tags in our HTML, like this:

<main id="app">
<custom-article></custom-article>
</main>
Our custom-article component is referred to as the child component.

The parent is the actual Vue instance.

Note that you can use string templates even without a component. You simply add the template option to your Vue instance, like this:

//HTML
<main id="app"></main>
//JS
new Vue({
el: '#app',
template: '<article>A string template without a component!<span></span></article>'
})

The example code for the previous example is available here: https://codepen.io/AjdinImsirovic/pen/RJxMae.

Next, we'll see how we can improve our component with the help of the props and data options.

主站蜘蛛池模板: 蕲春县| 南京市| 南澳县| 平阳县| 兴宁市| 响水县| 万荣县| 巴彦淖尔市| 察哈| 英吉沙县| 青铜峡市| 开阳县| 景谷| 浏阳市| 西华县| 遂昌县| 扎兰屯市| 昌吉市| 夹江县| 孟州市| 曲阳县| 剑阁县| 承德市| 莱芜市| 哈密市| 枞阳县| 陆良县| 长丰县| 北票市| 武夷山市| 洛扎县| 乌什县| 伊春市| 福安市| 仁布县| 墨竹工卡县| 土默特左旗| 游戏| 大田县| 闽清县| 莆田市|