Part 1: Perform simple addition [Beginner Tutorial: Learn Vue, CSS Grid and Flexbox]
Step 1: Add an element to your page and set it as the root element of a new Vue instance
- In the HTML pane, add an empty
div
element that has anid
ofapp
- In the JS pane, assign to the variable
vm
the initialization of anew
Vue
instance - Pass a single argument to this new Vue instance a value of type
object
that has a singlekey: value
pair: the key isel
and thevalue
is astring
of the CSS selector to target an element with theid
ofapp
Step 2: Add data to your Vue instance and bind to it in your template
- Still in your JS pane, add another
key: value
pair to yourobject
. Itskey
isdata
and its value is anotherobject
. This nested object value has a singlekey: value
pair. Itskey
isfirst
and itsvalue
is the number1
. Don’t forget to add the necessary comma,
after the firstkey: value
pair in yourdata
object. - Back in your HTML pane, inside the
div
, add aninput
element. It will have twokey="value"
attributes: the first is a native HTML attribute calledtype
— assign it the value"number"
; the second is a Vuedirective
. Generally speaking, all Vue directives are written as an all-lowercase word prefixed withv-
. For thisinput
element, you will use Vue’smodel
directive (so, writev-model
). Much like thetype
attribute from a moment ago was assigned the value"number"
, you will assign thisv-model
directive the value"first"
. - Since Codepen auto-refreshes your page after any paused period of typing, you should notice that your input field somehow has the value
1
in it. I encourage you to replace the1
in your JS pane with a new number, like3
. You should now see a3
in your input field. This is proof that you completed the last few steps successfully. Congrats! Before proceeding, change this back to1
. - Before we move on, let’s add a
modifier
to thev-model
directive that ensures we capture and display a value who’s type is anumber
and not astring
. The syntax for adding amodifier
to a Vue directive is as such:v-directive.modifier
. In this case, we want to add thenumber
modifier.
Step 3: More of the same from step 2
- In your JS pane, add another
key: value
pair to yourdata
object (which already has the pair offirst: 1
). This time, thekey
issecond
and thevalue
is1
. Don’t forget the comma,
after the firstkey: value,
pair. - In your HTML pane, add another
input
element that has identical attributes as the one already there. - For the second of the two identical
input
elements, change thev-model
directive’s value fromfirst
tosecond
. You should now see two input fields on the page, each with a 1 as their value. Like earlier, try changing your JS so thatsecond
has the value3
. You should see the page instantly update and the second input field showing the value 3. Congrats again! Changesecond
back to1
before proceeding. - Lastly, in the HTML and between both
input
elements, type a+
character. That way, on the page you should see in a single line:1 + 1
.
Step 4: Computing and displaying the answer
This instruction may seem long, but I encourage you to read it once completely, then try writing the code on a second read-through. If you get stuck, an image is below of the full code.
- In your JS, the
object
passed in to thenew Vue
instance currently has twokey: value
pairs: one whosekey
isel
and another whosekey
isdata
. There should also be a comma,
afterel
'svalue
. - You need to add a third
key: value
pair, afterdata
. Thekey
iscomputed
and the value is itself anotherobject
. - This
object
will have onekey: value
pair. Thekey
isanswer
and the value is…well, afunction
. Thisfunction
receives no parameters. In the body of thefunction
, you will write a single line. Thefunction
shouldreturn
the result of adding two values together:this.first
andthis.second
. - Double-check that you didn’t forget to put a comma
,
after the closing curly brace},
for theobject
that is the value to thedata
key. - Back in your HTML pane, after the second
input
element, add aspan
element. Between the opening and closing tags, type:{{answer}}
. If you typed it right, you should see the number2
after the second input on the page. If you seeanswer
with one or zero curly braces on either side, you didn’t type it correctly. - Lastly, still your HTML pane, between the second
input
field and the newspan
element, type an=
character. This way, the equation appears correctly as1 + 1 = 2
in the browser.
To explain, in all three cases (the inputs and now this span), where you typed first
second
and answer
, Vue is evaluating those as expressions and displaying the corresponding values for each similarly named key
that you added in your JS pane. first
and second
come from the object assigned to data
. answer
comes from the function
assigned to the computed
object’s answer
key
.
In the first two cases, the value in your template is being displayed in the context of an HTML attribute. In Vue, the double curly brace syntax {{ }}
is not allowed here. However, in the third case, answer
is being displayed not as an attribute but as text content. This context requires the double curly brace syntax {{ }}
in order to perform what’s called text interpolation. Whatever appears between the double curly braces must be a valid JavaScript expression, and will be evaluated. The result will display in-place.
This marks the end of Part 1
In Part 2, we will expand the application’s capability to perform subtraction, multiplication and division.