Template Syntax Flashcards
Justify Vue.js’s template syntax
It allows you to use an HTML-based template syntax to declaritively bind the rendered DOM to the underlying Vue instance data.
Briefly describe template rendering under the hood.
- 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
Besides templates, what other options do you have?
You can directly write render functions with pure JavaScript, and optionally use JSX
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?
- Use
Object.freeze(<data-object>)
so that not updating or tracking is done to the managed data object</data-object> - Use
v-once
to ensure only one interpolation happens and no more changes will be made via data-binding
What is the most basic form of data binding?
Text interpolation using “mustache” syntax
Message: {{ message }}
How do you perform one-time interpolations?
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.
Describe rendering dynamic HTML.
- 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 do we deal with binding data to attributes?
- 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
, orfalse
- **
Can only simple properties be included in interpolations?
- 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
, andDate
What are directives?
- 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
What are arguments in relation to directives?
Give two examples?
- An argument is a value that comes diretly after a
:
, after a directive -
v-bind:href="messageId"
-
href
is the argument tov-bind
- This tells
v-bind
to bind the element’shref
attribute to the value of the expressionsmessageId
-
-
v-on:click="handleClick"
-
v-on
taking an argumentclick
, which will causev-on
to bindhandleClick
to the actual element’s click event
-
What are modifiers?
- 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 callevent.preventDefault
on the triggered event
Describe the shorthands?
- 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