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

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.

主站蜘蛛池模板: 吉木乃县| 乐都县| 格尔木市| 邹平县| 新泰市| 青田县| 山丹县| 清远市| 大港区| 岳阳县| 略阳县| 江孜县| 万荣县| 秀山| 特克斯县| 江阴市| 连平县| 清涧县| 府谷县| 海晏县| 中超| 雷波县| 乌什县| 化隆| 玛曲县| 冀州市| 会宁县| 乌鲁木齐市| 吉木萨尔县| 新和县| 彭阳县| 贞丰县| 大足县| 乌兰县| 林口县| 衢州市| 定州市| 正蓝旗| 察雅县| 凭祥市| 宁都县|