Skip to content

Commit bb40080

Browse files
committed
Add component name questions
1 parent d249d0e commit bb40080

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

README.md

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,9 +232,9 @@ List of 300 VueJS Interview Questions
232232
|223| [Can I use computed property in another computed property?](#can-i-use-computed-property-in-another-computed-property)|
233233
|224| [How can I use imported constant in template section?](#How-can-i-use-imported-constant-in-template-section)|
234234
|225| [Is recommended to use async for computed properties?](#is-recommended-to-use-async-for-computed-properties)|
235-
|226| [](#)|
236-
|227| [](#)|
237-
|228| [](#)|
235+
|226| [What happens if you use duplicate field names?](#what-happens-if-you-use-duplicate-field-names)|
236+
|227| [Why the component data must be a function?](#why-the-component-data-must-be-a-function)|
237+
|228| [What is the reason for recommendation for multi-word component names?](#what-is-the-reason-for-recommendation-for-multi-word-component-names)|
238238
|229| [](#)|
239239
|230| [](#)|
240240

@@ -4723,11 +4723,32 @@ List of 300 VueJS Interview Questions
47234723
47244724
**[⬆ Back to Top](#table-of-contents)**
47254725
4726-
227. ### ?
4726+
227. ### Why the component data must be a function?
4727+
The component data must be a function instead directly providing the object. This is because each instance needs to maintain an independent copy of the returned data object. Otherwise one component instance data changes will impact the data of all other instances.
4728+
For example, the below code snippets gives an idea on correct approach,
4729+
```js
4730+
data: { // Bad
4731+
message: 'Hello'
4732+
}
4733+
data: function () { //Good
4734+
return {
4735+
message: 'Hello'
4736+
}
4737+
}
4738+
```
47274739
47284740
**[⬆ Back to Top](#table-of-contents)**
47294741
4730-
228. ### ?
4742+
228. ### What is the reason for recommendation for multi-word component names?
4743+
Component names should always be multi-word, except for root level or built-in vue components(such as <transition> or <component> etc). This recommendation is to prevents conflicts with existing and future HTML elements, since all HTML elements are a single word.
4744+
```js
4745+
Vue.component('user', { //bad approach
4746+
// ...
4747+
})
4748+
Vue.component('user-profile', { //good approach
4749+
// ...
4750+
})
4751+
```
47314752
47324753
**[⬆ Back to Top](#table-of-contents)**
47334754

0 commit comments

Comments
 (0)