{"id":3804,"date":"2023-07-18T02:11:19","date_gmt":"2023-07-18T02:11:19","guid":{"rendered":"https:\/\/nmi.cool\/advweb-rev\/?page_id=3804"},"modified":"2026-11-02T15:14:26","modified_gmt":"2026-11-02T15:14:26","slug":"exercise-vue-part-iv","status":"publish","type":"page","link":"https:\/\/nmi.cool\/advweb\/unit-three-javascript-frameworks\/week-eight\/exercise-vue-part-iv\/","title":{"rendered":"Exercise: Vue Part IV"},"content":{"rendered":"<p>In Vue, a component combines a template, data, and behavior into a reusable unit. In this exercise, you\u2019ll 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.<\/p>\n<ol start=\"1\">\n<li class=\"nohighlight\">To begin,\u00a0download and unzip the <a href=\"https:\/\/nmi.cool\/advweb\/wp-content\/uploads\/sites\/22\/2026\/11\/toast-start-vue-3.zip\">starter file<\/a>\u00a0(<code>toast-start-vue-3.zip<\/code>). Move <code>component.html<\/code> and <code>component.js<\/code> into your work folder, keeping them together.<\/li>\n<li>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.<\/li>\n<li>In the HTML,\u00a0<code>{{ toastState }}<\/code>\u00a0displays the current state of some toast. The button calls\u00a0<code>checkToast()<\/code>\u00a0when clicked.<\/li>\n<li>In the JavaScript,\u00a0<code>Vue.createApp()<\/code>\u00a0creates the application named\u00a0<code>co<\/code>.\u00a0\u00a0It uses <code>co.mount(\"#co\")<\/code>\u00a0to connect <code>co<\/code> to\u00a0<code>&lt;div id=\"co\"&gt;<\/code> in the HTML.<\/li>\n<li>In the preview or the browser, click the button. The heading should change from \u201cThe toast is burnt\u201d to \u201cThe toast is perfect.\u201d<\/li>\n<li>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\u00a0<code>toast<\/code>\u00a0component.<\/li>\n<li>In\u00a0<code>component.js<\/code>, register the new component using the code below. Place it after\u00a0<code>Vue.createApp(...)<\/code>\u00a0and before\u00a0<code>co.mount(\"#co\");<\/code>:<br \/>\n<\/p>\n<pre><code>co.component(\"toast\", { });<\/code><\/pre>\n<p>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 (<code>{ }<\/code>).<\/li>\n<li class=\"nohighlight\">Cut the button and heading from inside <code>&lt;div id=\"co\"&gt;<\/code> in <code>component.html<\/code>. Paste them into the component\u2019s <code>template<\/code> property, enclosed in backticks, as shown below. Leave the empty <code>&lt;div id=\"co\"&gt;&lt;\/div&gt;<\/code> in the HTML. The page will remain blank until Step 10.<br \/>\n<\/p>\n<pre><code>co.component(\"toast\", {\n    template: `\n        &lt;button v-on:click=\"checkToast\"&gt;Check the toast&lt;\/button&gt;\n        &lt;h3&gt;The toast is {{ toastState }}&lt;\/h3&gt;\n    `\n});<\/code><\/pre>\n<\/li>\n<li>\n<p>Move <code>data()<\/code> and <code>methods<\/code> from the application into the <code>toast<\/code> component.  Leave the application\u2019s options object empty: <code>Vue.createApp({})<\/code>.  Inside the template, wrap the button and heading in a <code>&lt;div&gt;<\/code> to group them.  This wrapper is optional in Vue 3.  Your complete JavaScript should match the example below:<\/p>\n<pre><code>const co = Vue.createApp({});\n\nco.component(\"toast\", {\n    template: `\n        &lt;div&gt;\n            &lt;button v-on:click=\"checkToast\"&gt;Check the toast&lt;\/button&gt;\n            &lt;h3&gt;The toast is {{ toastState }}&lt;\/h3&gt;\n        &lt;\/div&gt;\n    `,\n\n    data() {\n        return {\n            toastState: \"burnt\"\n        };\n    },\n\n    methods: {\n        checkToast() {\n            this.toastState = \"perfect\";\n        }\n    }\n});\n\nco.mount(\"#co\");<\/code><\/pre>\n<\/li>\n<li>\n<p>In <code>component.html<\/code>, insert <code>&lt;toast&gt;&lt;\/toast&gt;<\/code> inside <code>&lt;div id=\"co\"&gt;<\/code>.  Reload and test the button.  Then add a second <code>&lt;toast&gt;<\/code>, as shown below, and confirm that each button changes only its own toast.<\/p>\n<pre><code>&lt;div id=\"co\"&gt;\n    &lt;toast&gt;&lt;\/toast&gt;\n    &lt;toast&gt;&lt;\/toast&gt;\n&lt;\/div&gt;<\/code><\/pre>\n<\/li>\n<li>\n<p>Props supply values to a component from outside it.  Add the string prop <code>toastopinion<\/code> and display it in the template, as shown below.  Each toast keeps its own internal <code>toastState<\/code> and receives its opinion through a prop.<\/p>\n<pre><code>const co = Vue.createApp({});\n\nco.component(\"toast\", {\n    props: {\n        toastopinion: String\n    },\n\n    template: `\n        &lt;div&gt;\n            &lt;button v-on:click=\"checkToast\"&gt;Check the toast&lt;\/button&gt;\n            &lt;h3&gt;The toast is {{ toastState }}&lt;\/h3&gt;\n            &lt;h3&gt;{{ toastopinion }}&lt;\/h3&gt;\n        &lt;\/div&gt;\n    `,\n\n    data() {\n        return {\n            toastState: \"burnt\"\n        };\n    },\n\n    methods: {\n        checkToast() {\n            this.toastState = \"perfect\";\n        }\n    }\n});\n\nco.mount(\"#co\");<\/code><\/pre>\n<\/li>\n<li>\n<p>In <code>component.html<\/code>, replace the existing toast elements with the examples below.  Keep them inside <code>&lt;div id=\"co\"&gt;<\/code>.  Reload the page and confirm that each toast displays its supplied opinion.<\/p>\n<pre><code>&lt;div id=\"co\"&gt;\n    &lt;toast toastopinion=\"I hate toast\"&gt;&lt;\/toast&gt;\n    &lt;toast toastopinion=\"I like toast\"&gt;&lt;\/toast&gt;\n&lt;\/div&gt;<\/code><\/pre>\n<\/li>\n<li>\n<p>Add the string prop <code>bgcolor<\/code> and bind it to the heading\u2019s <code>backgroundColor<\/code> style.  Your complete JavaScript should now look like this:<\/p>\n<pre><code>const co = Vue.createApp({});\n\nco.component(\"toast\", {\n    props: {\n        toastopinion: String,\n        bgcolor: String\n    },\n\n    template: `\n        &lt;div&gt;\n            &lt;button v-on:click=\"checkToast\"&gt;Check the toast&lt;\/button&gt;\n            &lt;h3 :style=\"{ backgroundColor: bgcolor }\"&gt;\n                The toast is {{ toastState }}\n            &lt;\/h3&gt;\n            &lt;h3&gt;{{ toastopinion }}&lt;\/h3&gt;\n        &lt;\/div&gt;\n    `,\n\n    data() {\n        return {\n            toastState: \"burnt\"\n        };\n    },\n\n    methods: {\n        checkToast() {\n            this.toastState = \"perfect\";\n        }\n    }\n});\n\nco.mount(\"#co\");<\/code><\/pre>\n<p>In <code>component.html<\/code>, replace the existing toast elements inside <code>&lt;div id=\"co\"&gt;<\/code> with the following:<\/p>\n<pre><code>&lt;div id=\"co\"&gt;\n    &lt;toast toastopinion=\"I hate toast\" bgcolor=\"lightblue\"&gt;&lt;\/toast&gt;\n    &lt;toast toastopinion=\"I like toast\" bgcolor=\"yellow\"&gt;&lt;\/toast&gt;\n&lt;\/div&gt;<\/code><\/pre>\n<p>Reload the page.  Confirm that the backgrounds differ, both opinions appear, and each button changes only its own toast.<\/p>\n<\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>In Vue, a component combines a template, data, and behavior into a reusable unit. In this exercise, you\u2019ll 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,\u00a0download and unzip the &hellip; <a href=\"https:\/\/nmi.cool\/advweb\/unit-three-javascript-frameworks\/week-eight\/exercise-vue-part-iv\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Exercise: Vue Part IV<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":3698,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-3804","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3804","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/comments?post=3804"}],"version-history":[{"count":43,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3804\/revisions"}],"predecessor-version":[{"id":5014,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3804\/revisions\/5014"}],"up":[{"embeddable":true,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3698"}],"wp:attachment":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/media?parent=3804"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}