Vue.js Flashcards

1
Q

What is reactivity? (In a nutshell)

A

Vue watches the ‘data’ object for changes and updates the DOM appropriately.

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

What Vue function runs when the app is initiated?

A

created()

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

What is two-way data binding?

A

The ability of input to update Vue’s data. For example,

using the v-model directive on form input creates a two-way data biding.

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

What caveat to remember when using the v-model directive on form elements?

A

The value, checked and selected attributes set in HTML will be ignored. The Vue instance data (data object) will take priority, so set initial values in JS.

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

Say you’re calling an API that returns HTML; how would you display that within a div using Vue?

A

Vue automatically escapes values, so it will not display as intended - you need to use the special directive:
< div v-html=”yourHtml”>< /div>

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

How to make functions available in your HTML templates?

A

You can add functions by storing them as properties of the ‘methods’ object (in the Vue instance data).

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

Within functions on the methods object, how to access properties on the data object?

A

In a method, ‘this’ refers to the component that the method is attached to. Access simply with this.property. Access other functions on the methods object in the same way.

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

How to add computed properties to your template?

A
Add them as functions on the computed object.
computed: {
    myValue() {
        return this.
    /// etc.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How do you call computed properties in your template?

A

Just like a property - without “()”.

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

What is the difference between running a computed property and a method?

A

Computed properties are cached - their code will be run again only when a dependency of the method changes.

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

How to create a getter and setter for a computed property?

A
computed: {
    numberProperty: {
        get() {
            return x;
        },
        set(newValue) {
            this.numbers.push(newValue);
       }
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are watchers? (In a nutshell.)

A

They allow us to watch a property of the data object or a computed property for changes. However, Macrae advises that usually using a computed property with a setter is often a better way to do it.

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

How to set a watcher on a regular property?

A
Add the name of the property to watch into the watche object (as a function). eg:
data: { count: 0 },
watch: {
    count() {
        // this.count has changed!
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

If you have a whole object stored in your data, how to watch just a single property of that object?

A
Use dot syntax on the watcher definition, enclosed in single quotes:
data: {
    formData: {
        username: '',
    },
watch: {
    'formData.username'() {
        // this.formData.username has changed!
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What arguments are passed to watchers when a property is changed?

A
The new value and the old value (in that order):
watch: {
    inputValue(newVal, oldVal) {
        // can also use this.inputValue in place of newVal
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to watch an entire object for changes?

A
By default, a watcher won't fire if only a property in an object changes. Only if you replace the entire object. Watching the entire object is known as a deep watch, done like so:
watch: {
    formData: {
        handler() {
            // object has changed!
        },
        deep: true
    }
}
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is a component?

A

A self-contained piece of code that represents a part of a page. They have their own data, JS and ofter their own styling. They can contain other components, and can communicate with each other.

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

How to register and use a component globally (code example)?

A
< custom-button>< /custom-button>
< script>
Vue.component('custom-button', {
    template: '< button>Custom< /button>'
});
// start Vue...
< /script>
19
Q

Comment on the object used to define a component.

A

Similar to the object used to define the Vue instance, but the data property on components in a function (on the Vue instance it’s an object). This is so that multiple instances of the same component don’t share the same data.

20
Q

What’s the difference between v-if and v-show?

A

A falsy v-if element isn’t output to the DOM. A falsy v-show is added to the DOM with style=”display: none”.
Therefore, v-if is better for hiding stuff that hasn’t loaded yet, or data that doesn’t exist yet.
v-show has less performance cost on the DOM, also useful for downloading things in the background.

21
Q

Give a one-line code example of using a for-loop directive.

A

< li v-for=”dog in dogs”>{{ dog }}< /li>

22
Q

What if we want the index of the array while going through a v-for loop?

A

< li v-for=”(dog, i) in dogs>Number {{ i }} is {{ dog }}< /li>
Note the order of arguments is (value, key).

23
Q

Write a for loop that counts from 1 to 10

A

< span v-for=”n in 10”>{{ n }} < /span>

24
Q

What is : shorthand for?

A

v-bind:

25
Q

If you edit an element in an array and find that is isn’t behaving reactively, what could be wrong?

A

You must use either .splice() or Vue.set() to edit elements in an array, or Vue won’t know that the array was edited.

26
Q

What are the caveats / limitations to Vue’s reactivity? (name 3)

A

Adding new properties to an object (the getter/setter functions are added when the instance is initialized, new added properties won’t be reactive).

Setting items on an array. Using the index to set items will not work. Use either .splice() or Vue.set().

Setting the length of an array. Will not work as in JS (padding or truncating). You may truncate using .splice().

27
Q

How to add new properties to an object (after Vue is initialised) so that they’re reactive?

A

Use the Vue.set() function. Inside a component this is available as this.$set

28
Q

Write code for a simple function that returns your name.

A
new Vue({
    el: '#app',
    methods: {
        myName() {
            return 'your name';
        }
    }
});
29
Q

True/false: computed properties can accept arguments.

A

False, only methods can accept arguments.

30
Q

What are filters?

A

A convenient way of manipulating data in your templates (like formatting currency, dates, names, for example).
Usually filters are registered globally so can easily be reused across an entire app. Methods are for local component use only.

31
Q

Write code to define a simple filter.

A
// in html...
< p>Product two cost: {{ productTwoCost | formatCost }}< /p>
// in vue instance:
    filters: {
        formatCost(value) {
            return (value / 100).toFixed(2);
        }
    }
32
Q

How to chain multiple filters in the same expression?

A

{{ productCost | round | formatCost }}

Round is called first, then that value is passed to formatCost.

33
Q

Write code to define a filter that receives an argument.

A
// in html...
< p>Product two cost: {{ productTwoCost | formatCost('$') }}< /p>
// in vue instance:
    filters: {
        formatCost(value, symbol) {
            return symbol + (value / 100).toFixed(2);
        }
    }
34
Q

Where else can you use filters other than in interpolation?

A

You can use filters with v-bind when binding values to arguments:
< input type=”text” v-bind:value=”productCost | formatCost(‘$’)”>

35
Q

How to register a filter globally instead of on a per-component basis?

A

Vue.filter(‘filterName’, function(value) {
return whatever;
});

36
Q

What are the two caveats to using filters?

A

(1) In a filter, you cannot use this to refer to methods and data on the components. They are meant to be pure functions.
(2) You can only use filters in interpolations and v-bind directives (as opposed to being able to use them in any expression).

37
Q

What is v-on?

A

Event Binding.
It allows you to listen to DOM events, and trigger a method when the event happens.
It takes the name of the event as the argument, and the event listener as the passed value.

38
Q

Write a simple one-line event binding on a button.

A

< button v-on:click=”counter++”>Click to increase counter< /button>

39
Q

When binding events, describe a significant difference between specifying a method and putting the code inline.

A

Using a method, then event is passed in as the first argument. This is the native DOM event that you would get if you had added the event listener using JS .addEventListener().

40
Q

What is @ shortcut for?

A

v-on: event binding.

You can replace v-on:click with just @click

41
Q

How to pass data into components in your HTML?

A

Using props passed into the components as attributes on the HTML. (eg color=”red”), then list all the props in an array in the ‘props’ property of the component’s object. It’s value becomes available using this.color

42
Q

Must you pass an array into the ‘props’ property?

A

You can also pass an object containing information about the props, e.g. types, required, defaults and validation.

43
Q

How do you ‘case’ prop identifiers?

A

In the HTML, use kebab-case (my-prop=”…”) and in JS use camel-case (myProp). Vue handles this automatically.