Module2-events Flashcards

1
Q

<p>How do you react to an event in html in Vue</p>

A

<p>You need to use the v-on:click for example to respond to the click event and connect it to a method in the methods section v-on:click="handleClick"</p>

a short hand instead of v-on=”click” is @click

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

<p>Should v-on:event point to ="add()" or ="add"?</p>

A

<p>Both syntax work. If there are no params you usually use the later syntax but you need the parenthesis version if you want to pass values to the function</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What is the default argument provided by js to an event handler

A

JS provide the event handler with an object that describes the event itself as an argument.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

<p>how do you get access to the element that the event occurred on?</p>

A

<p>once you get the default event object you can use event.target to receive the element that the event was triggered on.</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

<p>How do you keep the behavior of getting the default event object?</p>

A

<p>You keep the default behavior by not calling the method with () but just pointing to the method name.</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

<p>how do you get the value of an element that an event occurred on?</p>

A

<p>You connect the element and event by using v-on. you point the v-on to the name of the method you want to invoke so you don't override the default behavior of getting the event object. once you get the event object you need to call event.target.value to get the value.</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

<p>how do we send the event object to the method if we want to pass an additional parameter (ex. last name)?</p>

A

<p>In order to send the event object and another value to a method in the event handler we need to use the following syntax: v-on:click="add($event,lastName);</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

<p>how do you prevent a form from doing its default behavior of submit when we click a button in the form?</p>

A

<p>we can call a method on v-on:submit and then use the event object to call event.preventDefault(); OR
<br></br>We can use an event modifier on the event like v-on=submit.prevent</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are buttons event modifiers for

A

.left .middle .right (don’t need the .left since this is the default behavior)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are keys event modifiers

A
<p>.tab
<br>.delete
<br>.esc
<br>.space
<br>.up
<br>.down
<br>.left
<br>.right
<br>.ctrl
<br>.alt
<br>.shift
<br>.meta</p>
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you make an event fire just once

A

use the .once event modifier

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

<p>how do you stop an event from propagating</p>

A

<p>use event.stop event modifier</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

<p>how to Only trigger if event.target is element itself</p>

A

<p>use the event.self event modifier</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

<p>what does v-once do?</p>

A

<p>v-once is a directive that tells vue to evaluate the data binding only once .</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

<p>what does v-model directive do</p>

A

<p>v-model creates a 2 way binding. it replaces the need to write the following (if myField is the binded field to the input) v-bind:value=,v-on="input "=</p>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

<p>why using methods just to show calculated fields is not a good idea?</p>

A

<p>methods are rerun every time the page refreshes since Vue does not know if the method changes a value that is displayed. It is better to use a computed field where vue knows to recalculated the value based on the data fields it is based on which helps performance</p>

17
Q

<p>What is computed</p>

A

<p>computed is a reserved work in Vue that is an object containing methods that compute values based on fields in the data function. it only computes if one of the underlying fields in data change.</p>

18
Q

<p>what is watch</p>

A

<p>watch is another object inside the configuration of the Vue app. it is used to keep track of changes of fields in data (same function name in watch as the field to watch in data) and do operations based on those changes. Usually is suited for situations where the change in the data triggers some backend logic and does not require changes in the UI (watched fields are usually not part of the template) where computed fields might be a better match.</p>

19
Q

<p>What is the shorthand for v-on:hover=</p>

A

<p>@hover=</p>

20
Q

<p>What is the shorthand for v-bind:value=</p>

A

<p>:hover=</p>

21
Q

<p>can we watch computed fields?</p>

A

<p>yes we can watch computed methods by naming the method in watch the same as the computed method</p>

22
Q

<p>how do you run something in delay?</p>

A

<p>use setTimeOut(func, time)</p>

23
Q

How do you add multiple bound classes to the class variable

A

use an array to hold the different data elements and assign it to the bound class attribute

24
Q

how you can bind a style attribute

A

you can use the following syntax for example

:style={“hidden” : “isHidden”}

25
Q

how do you use v-model

A

in order to use v-model you need to have a field in the data object and then point v-model at that field

26
Q

How do you activate based on a condition several classes on a bound class attribute

A

you can use the following syntax
:class=”{demo:true, active:isActive}” - demo will always show because it is true and active will be active if isActive is true. Another example is <span>{{logItem.what}} {{logItem.value}}</span>

27
Q

how do you set inline styles?

A

Use the following syntax as example:

:style=”{borderColer:isBoxASelected ? red : blue}”

28
Q

Why avoid using inline styles

A

inline styles override everything else so unless this is the specific reason why you are using it, it is better to avoid using them.

29
Q

What should we use instead of inline styles.

A

use classes

30
Q

What is the special syntax that vue supports for class binding?

A
you can use the following syntax
\:class="{"demo:true, active:isActive}" - you bind to an object of key:values where the key is the class name and the value is a boolean value/expression and that will decide which classes will be active.
31
Q

Do we need to specify a class that is always true in the object we bind the class to

A

No, a class that should always appear can be omitted. only class that has logic related should be specified and those that are always true can be set in a regular class attribute. Vue will take the regular class and append to it the values in the bound objects that are truethy

32
Q

Can you inline style based on a computed field

A

yes
(p)Style me with computed!(/p)

computed: {
backgroundColorStyle() {
return this.backgroundColor;
},

33
Q

Can you bind class attribute to an array

A

You can bind a class attribute to an array where each object in the array is either a string with a name OR a class of an object with nameOfClass:isActive pair which will be calculated dynamically

34
Q

how do you add an element to the beginning of an array?

A

use the unshift method

35
Q

when do you use the lazy event modifier

A

When working with form elements and the v-model directive,
To make the model update when the change event occurs, and not any time the user presses a key, you can use v-model.lazy instead of just v.model.

36
Q

When do you use the trim event modifier

A

When working with form elements and the v-model directive

Working with input fields, v-model.trim is useful because it automatically removes whitespace.

37
Q

When do you use v-model.number

A

When working with form elements if you’re expecting only a number instead of a string, make sure you use v-model.number.