{"id":3802,"date":"2023-07-18T02:10:28","date_gmt":"2023-07-18T02:10:28","guid":{"rendered":"https:\/\/nmi.cool\/advweb\/?page_id=3802"},"modified":"2025-10-21T18:06:25","modified_gmt":"2025-10-21T18:06:25","slug":"exercise-vue-part-iii","status":"publish","type":"page","link":"https:\/\/nmi.cool\/advweb\/unit-three-javascript-frameworks\/week-eight\/exercise-vue-part-iii\/","title":{"rendered":"Exercise: Vue Part III"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\">Vue 3: A look at class and style binding, and computed properties<\/h1>\n\n\n\n<p>As the title suggests, this lesson focuses on style and class binding, as well as computed properties. By now you should have been introduced to all three of these methods in your homework at Vuemastery, but the concepts are challenging enough to merit reinforcement.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>To begin,&nbsp;<a href=\"https:\/\/nmi.cool\/advweb\/wp-content\/uploads\/sites\/12\/2025\/10\/VueIII_start.zip\" data-type=\"attachment\" data-id=\"4637\">download this zip file<\/a>, which basically contains a clean version of the first exercise in the previous lesson. When you open the html file, VueIII.html in your browser, all drink names and descriptions should appear.<\/li>\n\n\n\n<li>Unzip and drag everything into your folder. Open both files in your editor&#8217;s split view. <\/li>\n\n\n\n<li>To start our journey let&#8217;s start with something radical and use style binding to change the background color of our h2 tags. Warning: this is going to be ugly.<\/li>\n\n\n\n<li>Add bgColor: &#8220;red&#8221; to your data section. Next, bind bgColor to your &lt;h2&gt; tags as shown below and refresh your browser. Hideous long, thick red line, right?<pre class=\"wp-block-code\"><code><strong>const drinkdata<\/strong> = {<br>    data() {<br>    return {<br>        bgColor: \"red\",<br>        drink: <br>    [{<\/code><\/pre><br><pre class=\"wp-block-code\"><code><strong>&lt;h2 :style<\/strong>=\"{ backgroundColor: bgColor }\"&gt;{{ eachDrink.drink_name }}&lt;\/h2&gt;<\/code><\/pre><\/li>\n\n\n\n<li>Change the value of bgColor to something more subtle such as bgColor: &#8220;#d9d6d6&#8221; (a nice light gray) and move your style binding to the div tag instead. Now, each name and drink description has a gray background. <\/li>\n\n\n\n<li>You could add as many css values as you like simply by separating them with commas and creating the value in your data, but that could get pretty clunky. In your data, create a new variable named myDiv and populate it with values as shown below. Then add it to your html by modifying your :style= statement.<br> <pre class=\"wp-block-code\"><code>const drinkdata = {<br>    data() {<br>    return {<br>       <strong>myDiv: {<br>        backgroundColor: \"#d9d6d6\",<br>        width: \"400px\",<br>        fontFamily: 'Arial',<br>    }<\/strong>,<br>        drink: <br>    [{<\/code><\/pre> <br> <pre class=\"wp-block-code\"><code>&lt;div id=\"drink\"&gt;<br>    &lt;div :style=\"[myDiv]\" v-for=\"eachDrink in drink\" :key=\"eachDrink.counter\"&gt;<br>      &lt;h2&gt;{{ eachDrink.drink_name }}&lt;\/h2&gt;<br>      &lt;p&gt;{{ eachDrink.fact }}&lt;\/p&gt;<br>    &lt;\/div&gt;<br>  &lt;\/div&gt;<\/code><\/pre><\/li>\n\n\n\n<li>It is also possible to apply multiple values at once by creating and applying more than one object that contains multiple css properties. Delete myDiv from your vue and replace it with these two variables: divStructure and divText. Bind divStructure to your &lt;div&gt;&lt;\/div&gt; tag and divText to your &lt;h2&gt;&lt;\/h2&gt; element.<strong> <\/strong><br><pre class=\"wp-block-preformatted\"> divStructure: {<br>            backgroundColor: \"#f5cac3\",<br>            width: '400px'<br>        },<br>        divText: {<br>            fontFamily: 'Courier New',<br>            fontStyle: 'bold',<br>            color: 'black',<br>        },<br>        drink: <br>        [ <br><\/pre><\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Let&#8217;s compute something<\/h3>\n\n\n\n<p>This compute lesson will be brief but we may revisit compute later on. For now, we are going to use compute to cause one of the drinks to randomly appear in a header at the top of our document.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Add a computed section below data as shown below, with a value of title. <pre class=\"wp-block-code\"><code>\/\/drinks<br>},<br><br>  \/\/NEW SECTION<br>  computed: {<br>    title() {<br>      <br>    }<br>  }<br>};<\/code><\/pre><\/li>\n\n\n\n<li>Within the brackets { } add&nbsp;<strong>return this.drink[0].drink_name.&nbsp;<\/strong>Now add &lt;h1&gt; tags to the top of your document underneath &lt;div id=&#8221;drink&#8221; &gt;, and add {{title} between the h1 tags. Save everything and test.&nbsp; You should see the first drink in the list (Iced Matcha Latte). <pre class=\"wp-block-code\"><code>computed: {<br>  title() {<br>    return this.drink[0].drink_name;<br>  }<br>}<\/code><\/pre><\/li>\n\n\n\n<li>That was nice but we are equal opportunity around here so modify title to match the statement below to randomly select a drink name.<pre class=\"wp-block-code\"><code>computed: {\n  title() {\n    let whichDrink = Math.floor(Math.random() * this.drink.length);\n    return this.drink[whichDrink].drink_name;\n  }\n}<\/code><\/pre><\/li>\n\n\n\n<li>Now a random drink will appear every time you refresh the page. <\/li>\n<\/ol>\n","protected":false},"excerpt":{"rendered":"<p>Vue 3: A look at class and style binding, and computed properties As the title suggests, this lesson focuses on style and class binding, as well as computed properties. By now you should have been introduced to all three of these methods in your homework at Vuemastery, but the concepts are challenging enough to merit &hellip; <a href=\"https:\/\/nmi.cool\/advweb\/unit-three-javascript-frameworks\/week-eight\/exercise-vue-part-iii\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Exercise: Vue Part III<\/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-3802","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3802","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=3802"}],"version-history":[{"count":27,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3802\/revisions"}],"predecessor-version":[{"id":4714,"href":"https:\/\/nmi.cool\/advweb\/wp-json\/wp\/v2\/pages\/3802\/revisions\/4714"}],"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=3802"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}