Vue.js Flashcards
What is reactivity? (In a nutshell)
Vue watches the ‘data’ object for changes and updates the DOM appropriately.
What Vue function runs when the app is initiated?
created()
What is two-way data binding?
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.
What caveat to remember when using the v-model directive on form elements?
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.
Say you’re calling an API that returns HTML; how would you display that within a div using Vue?
Vue automatically escapes values, so it will not display as intended - you need to use the special directive:
< div v-html=”yourHtml”>< /div>
How to make functions available in your HTML templates?
You can add functions by storing them as properties of the ‘methods’ object (in the Vue instance data).
Within functions on the methods object, how to access properties on the data object?
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 to add computed properties to your template?
Add them as functions on the computed object. computed: { myValue() { return this. /// etc.
How do you call computed properties in your template?
Just like a property - without “()”.
What is the difference between running a computed property and a method?
Computed properties are cached - their code will be run again only when a dependency of the method changes.
How to create a getter and setter for a computed property?
computed: { numberProperty: { get() { return x; }, set(newValue) { this.numbers.push(newValue); } } }
What are watchers? (In a nutshell.)
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 to set a watcher on a regular property?
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!
If you have a whole object stored in your data, how to watch just a single property of that object?
Use dot syntax on the watcher definition, enclosed in single quotes: data: { formData: { username: '', }, watch: { 'formData.username'() { // this.formData.username has changed! } }
What arguments are passed to watchers when a property is changed?
The new value and the old value (in that order): watch: { inputValue(newVal, oldVal) { // can also use this.inputValue in place of newVal } }
How to watch an entire object for changes?
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 } }
What is a component?
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.