Module 8 - Component communication Flashcards
How do you pass information from the parent (app) to the child (component) in Vue
You can pass data to reusable components by adding a section in the component called props where you specify the attributes that the component can receive using strings. Those strings can be used in the template and in other sections using the this.key word. The “props” are transferred from the app to the component using a syntax similar to html attributes on the vue element
What is a uni directional data flow
It is the term used to describe the fact that the attributes that are passed from the parent to the component should not be mutated by the component but only be transferred in a uni directional way from the parent to the component when we are using props. To change data that belong to the parent you should emit an event from the child to the parent and the parent should change the data that belongs to him
What do we need to do in order to bind our props to dynamic values
In order to bind our props to dynamic values we need to use v-bind:propName=”value”
how do you use a component with a v-for or a v-if
Our components are the same as html elements and therefor you can use v-for on them the same way you use them on elements and you must have a key value defined.
What is $emit for?
$emit() is a function that fires an event to allow a component to notify it’s parent that a value has changed
How should the event name that is emitted from the component be formatted
The events from the components should always be kebab-case formatted
How do you pass data from the event that is emitted from the component to the parent?
In order to emit additional data you need to add parameters to the $emit method and they will be considered as arguments in the method on the parent
How do we listen to the event that is emitted from the child component in the parent component?
We listen to the event emitted by the child component by adding an event handler v-on (or @) for the event name that was emitted by the child component and call a method that does the needed manipulation on the data that was bound and sent to the child component where it is updated in the UI. This completes the entire cycle from sending props from the parent to the child and emitting event from the child to the parent.
How do you document and let other developers know what are the events that the component emits?
You document the events that your component emits by adding a section called emits to the configuration area of the component with an array of strings with the names of the events that the component can fire. If you want to add other parameters to the documentation you can use an object instead of the array.
How would you document the events and params that are emitted by the component?
You can extend the emits section from an array with just the names of the events that are emitted to an object where each event is a key and the value is a function with each argument representing data that is included in the emitted event. you can also validate that data at that point inside the function and return true/false based on the validity of the arguments of the function.
What is included in main.js?
main. js contains:
1. import of createApp from vue
2. importing the components
3. creating the app and setting it in a const
4. adding the components to app app.component(…,…);
5. mounting the app
How do you put dummy text inside an input field?
You can use the placeholder attribute with the text you want displayed in the input. This is not the value of the input field
What is a reusable component?
A reusable component (or dynamic component) is a component that can receive data from its ancestor (or an external source) and therefor display different data each time it is reused in the application.
What are props?
props is short for properties and we can think about them as attributes that are placed in an html element. Those can be required or optional and are specified and can be configured in the component using a props section in the configuration. props can be primitive types, objects, arrays, functions, promises etc.
How to validate props?
Instead of just writing the names of the props passed inside an array with strings we can replace it with an object where each prop name is a key and the value is the type of it. If you want to be even more specific the value can itself be an object and we can specify the type, required , default, validation properties on that object for the prop.