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

  • 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.

主站蜘蛛池模板: 通江县| 石首市| 西青区| 疏勒县| 乐安县| 内江市| 陈巴尔虎旗| 黑河市| 桦南县| 石楼县| 二连浩特市| 罗甸县| 金堂县| 札达县| 德安县| 平潭县| 闸北区| 大姚县| 霍城县| 淮滨县| 丁青县| 南岸区| 襄垣县| 石楼县| 古交市| 牡丹江市| 南溪县| 绵阳市| 鹤庆县| 德保县| 鄂温| 灯塔市| 阳西县| 海林市| 黑龙江省| 榕江县| 贞丰县| 读书| 新昌县| 尚义县| 隆化县|