In Vue, a component combines a template, data, and behavior into a reusable unit. In this exercise, you’ll convert a working application into a component, create multiple instances, and customize them using props. When a project is made up of components, it can be easier to understand, manage, and debug.
- To begin, download and unzip the starter file (
toast-start-vue-3.zip). Movecomponent.htmlandcomponent.jsinto your work folder, keeping them together. - Open both files side by side in your editor. Preview the HTML file in the editor or in a browser. Both files are very basic.
- In the HTML,
{{ toastState }}displays the current state of some toast. The button callscheckToast()when clicked. - In the JavaScript,
Vue.createApp()creates the application namedco. It usesco.mount("#co")to connectcoto<div id="co">in the HTML. - In the preview or the browser, click the button. The heading should change from “The toast is burnt” to “The toast is perfect.”
- Our task is to create a really basic component out of these equally basic materials. We are going to convert this Vue instance into a component. To do this, move the template, data, and method into a reusable
toastcomponent. - In
component.js, register the new component using the code below. Place it afterVue.createApp(...)and beforeco.mount("#co");:
co.component("toast", { });As you can see, the component is set up differently from a Vue object. The name is in quotes, followed by a comma and curly bracket. As in the Vue object, all of the work takes place within those brackets (
{ }). - Cut the button and heading from inside
<div id="co">incomponent.html. Paste them into the component’stemplateproperty, enclosed in backticks, as shown below. Leave the empty<div id="co"></div>in the HTML. The page will remain blank until Step 10.
co.component("toast", { template: ` <button v-on:click="checkToast">Check the toast</button> <h3>The toast is {{ toastState }}</h3> ` }); -
Move
data()andmethodsfrom the application into thetoastcomponent. Leave the application’s options object empty:Vue.createApp({}). Inside the template, wrap the button and heading in a<div>to group them. This wrapper is optional in Vue 3. Your complete JavaScript should match the example below:const co = Vue.createApp({}); co.component("toast", { template: ` <div> <button v-on:click="checkToast">Check the toast</button> <h3>The toast is {{ toastState }}</h3> </div> `, data() { return { toastState: "burnt" }; }, methods: { checkToast() { this.toastState = "perfect"; } } }); co.mount("#co"); -
In
component.html, insert<toast></toast>inside<div id="co">. Reload and test the button. Then add a second<toast>, as shown below, and confirm that each button changes only its own toast.<div id="co"> <toast></toast> <toast></toast> </div> -
Props supply values to a component from outside it. Add the string prop
toastopinionand display it in the template, as shown below. Each toast keeps its own internaltoastStateand receives its opinion through a prop.const co = Vue.createApp({}); co.component("toast", { props: { toastopinion: String }, template: ` <div> <button v-on:click="checkToast">Check the toast</button> <h3>The toast is {{ toastState }}</h3> <h3>{{ toastopinion }}</h3> </div> `, data() { return { toastState: "burnt" }; }, methods: { checkToast() { this.toastState = "perfect"; } } }); co.mount("#co"); -
In
component.html, replace the existing toast elements with the examples below. Keep them inside<div id="co">. Reload the page and confirm that each toast displays its supplied opinion.<div id="co"> <toast toastopinion="I hate toast"></toast> <toast toastopinion="I like toast"></toast> </div> -
Add the string prop
bgcolorand bind it to the heading’sbackgroundColorstyle. Your complete JavaScript should now look like this:const co = Vue.createApp({}); co.component("toast", { props: { toastopinion: String, bgcolor: String }, template: ` <div> <button v-on:click="checkToast">Check the toast</button> <h3 :style="{ backgroundColor: bgcolor }"> The toast is {{ toastState }} </h3> <h3>{{ toastopinion }}</h3> </div> `, data() { return { toastState: "burnt" }; }, methods: { checkToast() { this.toastState = "perfect"; } } }); co.mount("#co");In
component.html, replace the existing toast elements inside<div id="co">with the following:<div id="co"> <toast toastopinion="I hate toast" bgcolor="lightblue"></toast> <toast toastopinion="I like toast" bgcolor="yellow"></toast> </div>Reload the page. Confirm that the backgrounds differ, both opinions appear, and each button changes only its own toast.