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

Defining and using the components

We will write our UI components in a new file:

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

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

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

主站蜘蛛池模板: 互助| 赤壁市| 西和县| 安化县| 墨竹工卡县| 肇庆市| 达州市| 合水县| 阳曲县| 辉南县| 兴业县| 亳州市| 淳安县| 兴仁县| 清水河县| 济阳县| 诸城市| 通州区| 哈巴河县| 广灵县| 荆门市| 西昌市| 苏尼特右旗| 柘荣县| 临安市| 裕民县| 彰武县| 吉林省| 四川省| 卫辉市| 西乌珠穆沁旗| 盐山县| 丹巴县| 高州市| 沽源县| 宁津县| 南丹县| 洞口县| 辽源市| 铜梁县| 福安市|