Template Syntax Flashcards

1
Q

Justify Vue.js’s template syntax

A

It allows you to use an HTML-based template syntax to declaritively bind the rendered DOM to the underlying Vue instance data.

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

Briefly describe template rendering under the hood.

A
  • Vue compiles the templates into Virtual DOM render functions
  • With the reactivity system, Vue intelligently figures out the minimal number of components to rerender
  • Applies the minimal amount of DOM manipulations when the app state changes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Besides templates, what other options do you have?

A

You can directly write render functions with pure JavaScript, and optionally use JSX

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

How 2 methods can you use to be assured initial data will not be changed if you want the component template to be completely static?

A
  1. Use Object.freeze(<data-object>) so that not updating or tracking is done to the managed data object</data-object>
  2. Use v-once to ensure only one interpolation happens and no more changes will be made via data-binding
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the most basic form of data binding?

A

Text interpolation using “mustache” syntax

Message: {{ message }}

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

How do you perform one-time interpolations?

A

Use the v-once directive on an element (node).

` {{ name }} is now {{ status }}`

Of course, v-once will affect all bindings on the node it’s placed on.

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

Describe rendering dynamic HTML.

A
  • You must use the v-html directive on an element
  • `

The code should be: * You can’t use v-html to create template partials

* Vue is not a string-based templating engine * Dynamically rendering arbitrary HTML can lead to XSS vulnerabilities
* Only use HTML interpolations on trusted content, and **never** on user-generated content
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do we deal with binding data to attributes?

A
  • Attributes can’t accept mustache syntax
  • We use the v-bind directive to bind data to an attribute
    • {{message}}
  • For boolean attributes, the following binded values will cause the attribute to not be rendered
    • **​**null, undefined, or false
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can only simple properties be included in interpolations?

A
  • No, we can use full JavaScript expressions
  • We can only use one single expression
  • They’re evaluated in the data scope of the owner Vue instance
  • These expressions are sandboxed, only have access to certain globals, especially libraries, such as Math, and Date
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are directives?

A
  • They are simply special attributes with the v- prefix
  • It’s job is to reactively apply side effects to the DOM when the expression changes
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What are arguments in relation to directives?

Give two examples?

A
  • An argument is a value that comes diretly after a :, after a directive
  • v-bind:href="messageId"
    • href is the argument to v-bind
    • This tells v-bind to bind the element’s href attribute to the value of the expressions messageId
  • v-on:click="handleClick"
    • v-on taking an argument click, which will cause v-on to bind handleClick to the actual element’s click event
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are modifiers?

A
  • Special postfixes denoted by a dot, indicating the directive should be bound in a certain way
    • <form v-on:submit.prevent="handleIt">...</form>
    • Tells the v-on directive to call event.preventDefault on the triggered event
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Describe the shorthands?

A
  • Shorthand for v-bind:id="messageId" is `:id=”messageId”
  • Shorthand for v-on:click="handleIt" is @click="handleIt"
  • : and @ are valid chars for attribute names and are parsed correctly in all Vue supported browsers
How well did you know this?
1
Not at all
2
3
4
5
Perfectly