{"id":3800,"date":"2023-07-18T02:09:42","date_gmt":"2023-07-18T02:09:42","guid":{"rendered":"https:\/\/nmi.cool\/advweb\/?page_id=3800"},"modified":"2026-04-01T14:49:02","modified_gmt":"2026-04-01T14:49:02","slug":"exercise-vue-part-ii","status":"publish","type":"page","link":"https:\/\/nmi.cool\/advweb\/unit-three-javascript-frameworks\/week-eight\/exercise-vue-part-ii\/","title":{"rendered":"Exercise: Vue Part II"},"content":{"rendered":"\n<p>For the first part of this lesson we will be working with data encased in an array of objects and, once again, that data is all about caf\u00e9 drinks.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Load all the data<\/h3>\n\n\n\n<p>Create a new html doc. Add the Vue JS CDN link between your &lt;head&gt;&lt;\/head&gt; tags.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">&lt;script src=\"https:\/\/unpkg.com\/vue@3\/dist\/vue.global.js\"&gt;&lt;\/script&gt;<\/pre>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Create a div in your html with the id of &#8216;drink&#8217; .<\/li>\n\n\n\n<li><a href=\"https:\/\/nmi.cool\/advweb\/wp-content\/uploads\/sites\/12\/2025\/10\/drinkorder.js_.zip\" data-type=\"attachment\" data-id=\"4630\">Download your data<\/a>&nbsp;and unzip it. A file named drinkorder.js should appear. Drag drinkorder.js into the same folder as your new html file and connect it to your html file under your Vue link. Add the word &#8220;defer&#8221; (shown here) to load the scripts sequentially. <pre class=\"wp-block-code\"><code> &lt;script src=\"drinkorder.js\" defer&gt;&lt;\/script&gt;<\/code><\/pre><\/li>\n\n\n\n<li>Open drinkorder.js and look inside. At this point all that is in there is one big object with data about, well, drinks.<\/li>\n\n\n\n<li>Create a new Vue object in drinkorder.js. Set el&nbsp; to &#8216;#drink&#8217; . Add data to your vue, create a variable of data named drink, and set its value to the big data object with all of your info. <pre class=\"wp-block-code\"><code>const drinkdata = {<br>    data() {<br>    return {<br>        drink: <br>    [{<\/code><\/pre><\/li>\n\n\n\n<li>Add the following line to the end of the data set: <pre class=\"wp-block-code\"><code>Vue.createApp(drinkdata).mount(\"#drink\");<\/code><\/pre><\/li>\n\n\n\n<li>For our first performance, we will use v-for to output each drink&#8217;s name. Put this inside your #drink div. <pre class=\"wp-block-preformatted\">&lt;div v-for=\"eachDrink in drink\"&gt;<br>            &lt;h2&gt; {{eachDrink.drink_name}} &lt;\/h2&gt;<br>&lt;\/div&gt;<\/pre><\/li>\n\n\n\n<li>At this point you should see names for each drink. Try adding the description!<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Use a Vue method to load data sequentially<\/h3>\n\n\n\n<p>Bringing all of that data into webpage at once was kind of fun, and potentially useful. In this exercise, we are going to use a method (AKA function) to bring in just one drink&#8217;s description at a time when a button is clicked.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Remove or comment out everything inside of the #drink div.<\/li>\n\n\n\n<li>Add two new variables to the Vue&#8217;s data: drinkname and counter, as shown below. They will go at the end of the drink object. We will use each of those variables in the method that we are about to construct.<br><pre class=\"wp-block-code\"><code>{<br>    \"drink_name\": \"Mocha Latte\",<br>    \"fact\": \" Espresso and steamed milk with a rich chocolate flavor \u2014 a cozy favorite.\",<br>    \"section\": \"115\",<br>    \"counter\": \"229\"<br>}], <br><br>        <strong>drinkname: \"\",<br>        counter: 0<\/strong><br><br>    };<br>}<br><br>};<br><br>Vue.createApp(drinkdata).mount(\"#drink\");<\/code><\/pre><\/li>\n\n\n\n<li>Add&nbsp;<strong>placeholders<\/strong>&nbsp;to the #drink div tag. Create a button to connect a &#8220;showDrink&#8221; method that we are about to build.<pre class=\"wp-block-code\"><code>&lt;div id=\"drink\"&gt;<br>    &lt;h2&gt;{{ drinkname }}&lt;\/h2&gt;<br>    &lt;p&gt;{{ fact }}&lt;\/p&gt;<br>    &lt;button @click=\"showDrink\"&gt;Show Next Drink&lt;\/button&gt;<br>  &lt;\/div&gt;<\/code><\/pre><pre class=\"wp-block-code\"><\/pre><\/li>\n\n\n\n<li>It&#8217;s time to build out the method in the space shown below:<img decoding=\"async\" src=\"https:\/\/apweb.quest\/wp-content\/uploads\/2019\/03\/img_5c9e5cd9bf905.png\" alt=\"\"> <pre class=\"wp-block-code\"><code> }<br>      ],<br>      \/\/ variables to hold what\u2019s displayed<br>      drinkname: \"\",<br>      fact: \"\",<br>      counter: 0<br>    };<br>  },<br>  <strong>methods: {<br>    <br>\/\/build method here<br><br><br>      }<\/strong><br>    }<br>  }<br>};<br><br>Vue.createApp(drinkdata).mount(\"#drink\");<\/code><\/pre><\/li>\n\n\n\n<li>First, fetch the current drink using the counter. <pre class=\"wp-block-code\"><code>showDrink() {<br>  const current = this.drink[this.counter];<br>  \/\/ guard in case array is empty<br>  if (!current) return;<br>}<\/code><\/pre><\/li>\n\n\n\n<li>Update variables:<img decoding=\"async\" src=\"https:\/\/apweb.quest\/wp-content\/uploads\/2019\/03\/img_5c9e620682308.png\" alt=\"\"> <pre class=\"wp-block-code\"><code>showDrink() {<br>  const current = this.drink[this.counter];<br>  if (!current) return;<br><br>  this.drinkname = current.drink_name;<br>  this.fact = current.fact;<br>}<\/code><\/pre><\/li>\n\n\n\n<li>Advance the loop: <pre class=\"wp-block-code\"><code>this.counter++;<br>if (this.counter &gt;= this.drink.length) {<br>  this.counter = 0;<br>}<\/code><\/pre><img decoding=\"async\" src=\"https:\/\/apweb.quest\/wp-content\/uploads\/2019\/10\/img_5db79a149e248.png\" alt=\"\"><\/li>\n\n\n\n<li>Finally, finish it off with a mounted method. At this point, you should see a different drink name and description each time you click the button. <pre class=\"wp-block-code\"><code>}], <br><br>        drinkname: \"\",<br>        fact: \"\",<br>        counter: 0<br><br>    };<br>},<br>methods: {<br>    showDrink() {<br>      const current = this.drink[this.counter];<br>      this.drinkname = current.drink_name;<br>      this.fact = current.fact;<br>     this.counter++;<br>if (this.counter &gt;= this.drink.length) {<br>  this.counter = 0;<br>}}},mounted() {<br>    this.showDrink();<br>  }<br>};<br><br>Vue.createApp(drinkdata).mount(\"#drink\");<br>    <\/code><\/pre><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">While we&#8217;re at it: How about a dropdown list?<\/h3>\n\n\n\n<p>We worked a bit with dropdown lists in a previous lesson or two where we used&nbsp; plain, unadorned javascript to populate the &lt;select&gt; and &lt;option&gt; tags with data from Javascript objects. This time we are going to build, and access values from a dropdown list with Vue data, methods and syntax. Will it be any easier? You can be the judge.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>First, add a new method to the methods section. This one is ready to copy paste before the showDrink() method. Methods can exist in the same spot as long as they are separated by a comma. <pre class=\"wp-block-code\"><code>    displayDrink() {\n      const chosen = this.drink.find(d =&gt; d.drink_name === this.selectedDrink);\n      if (chosen) {\n        this.drinkname = chosen.drink_name;\n        this.fact = chosen.fact;}},<\/code><\/pre><\/li>\n\n\n\n<li>Next, use v-model and v-on:change to dynamically load the drink based on your selection.<pre class=\"wp-block-code\"><code>  &lt;select v-model=\"selectedDrink\" v-on:change=\"displayDrink\"&gt;\n      &lt;option disabled value=\"\"&gt;-- Choose a Drink --&lt;\/option&gt;\n      &lt;option v-for=\"eachDrink in drink\" :value=\"eachDrink.drink_name\"&gt;\n        {{ eachDrink.drink_name }}\n      &lt;\/option&gt;\n    &lt;\/select&gt;<\/code><\/pre> <\/li>\n\n\n\n<li>You should now have a button that allows you to cycle through all of the drink options and a dropdown menu that allows you to select a specific drink. <\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>For the first part of this lesson we will be working with data encased in an array of objects and, once again, that data is all about caf\u00e9 drinks. Load all the data Create a new html doc. Add the Vue JS CDN link between your &lt;head&gt;&lt;\/head&gt; tags. &lt;script src=&#8221;https:\/\/unpkg.com\/vue@3\/dist\/vue.global.js&#8221;&gt;&lt;\/script&gt; Use a Vue method to &hellip; <a href=\"https:\/\/nmi.cool\/advweb\/unit-three-javascript-frameworks\/week-eight\/exercise-vue-part-ii\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Exercise: Vue Part II<\/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-3800","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3800","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=3800"}],"version-history":[{"count":46,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3800\/revisions"}],"predecessor-version":[{"id":4838,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3800\/revisions\/4838"}],"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=3800"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}