Exercise: Vue Part IV

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.

  1. To begin, download and unzip the starter file (toast-start-vue-3.zip). Move component.html and component.js into your work folder, keeping them together.
  2. 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.
  3. In the HTML, {{ toastState }} displays the current state of some toast. The button calls checkToast() when clicked.
  4. In the JavaScript, Vue.createApp() creates the application named co.  It uses co.mount("#co") to connect co to <div id="co"> in the HTML.
  5. In the preview or the browser, click the button. The heading should change from “The toast is burnt” to “The toast is perfect.”
  6. 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 toast component.
  7. In component.js, register the new component using the code below. Place it after Vue.createApp(...) and before co.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 ({ }).

  8. Cut the button and heading from inside <div id="co"> in component.html. Paste them into the component’s template property, 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>
        `
    });
  9. Move data() and methods from the application into the toast component. 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");
  10. 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>
  11. Props supply values to a component from outside it. Add the string prop toastopinion and display it in the template, as shown below. Each toast keeps its own internal toastState and 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");
  12. 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>
  13. Add the string prop bgcolor and bind it to the heading’s backgroundColor style. 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.