> For the complete documentation index, see [llms.txt](https://devworkshops.gitbook.io/masteringvuejs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://devworkshops.gitbook.io/masteringvuejs/getting-started/components.md).

# Components

### Remaining items component

In the top of the **main.js** file we're going to create a brand new component. Notice that the template property contains almost a copy of the remaining items area in the root template.

{% code title="main.js" %}

```javascript
Vue.component("remaining-items", {
  props: ["remaining"],
  template: `
    <div class="alert alert-danger" v-if="remaining>10">
      You've got a long day ahead of you!!!
    </div>
    <div class="alert alert-secondary" v-else-if="remaining>0">
      {{ remaining }} item(s) remaining.
    </div>
    <div class="alert alert-success" v-else>
      Hooray!!! You're all done, go to the beach!!!
    </div>`
});
...
```

{% endcode %}

Now update the **index.html** and replace the 3 `div` elements responsible for showing the remaining items message by the below

{% code title="index.html" %}

```javascript
...
<remaining-items :remaining="todos.filter(t => !t.done).length"></remaining-items>
...
```

{% endcode %}

Save your changes and ensure the app is behaving normally:

![](/files/-Lfs9PocGEUYIKfsejGF)

### Add item component

This component will encapsulate the add item functionality.&#x20;

{% code title="main.js" %}

```javascript
...
Vue.component("add-item", {
  template: `
        <form class="mb-2" v-on:submit.prevent="add">
            <input
            class="form-control"
            placeholder="Add todo..."
            v-model="newTodo" />
        </form>
          `,
  data() {
    return {
      newTodo: undefined
    };
  },
  methods: {
    add() {
      this.$emit("submitted", this.newTodo);
      this.newTodo = undefined;
    }
  }
});
...
```

{% endcode %}

We'll now replace the form in the root template using the component

{% code title="index.html" %}

```markup
...
<add-item @submitted="addTodo($event)"></add-item>
...
```

{% endcode %}

We're also going to update the `addTodo` method in the root component to handle the data coming from the child component

{% code title="main.js" %}

```javascript
...
addTodo(todo) {
  this.todos.push({
    id: this.nextId++,
    title: todo,
    created: new Date(),
    done: false
  });
}
...
```

{% endcode %}

Save your changes and ensure that you can add new items.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://devworkshops.gitbook.io/masteringvuejs/getting-started/components.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
