avatarMateusz Rybczonek

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

523

Abstract

f a component.</p><p id="a716">In our example we assigned our component’s data to a const declared before. By doing this every instance of the component references the same data object. Changing the message in one component changes the message of all other component instances.</p><p id="6d77">We want to reuse this component, allowing users to have multiple items. To achieve that, each instance must generate a unique data object. We do that by <code>return</code>-ing a fresh object in <code>data</code> property.</p><div

Options

id="e1fb"><pre><span class="hljs-title function_">data</span>(<span class="hljs-params"></span>) { <span class="hljs-keyword">return</span> { <span class="hljs-attr">message</span>: <span class="hljs-string">''</span>, <span class="hljs-attr">completed</span>: <span class="hljs-literal">true</span> } }</pre></div><p id="ca28">Want to try another challenge? <a href="https://medium.com/@m.rybczonek/vue-js-interview-challenges-d928a1de9a2a">Take me to the list of available challenges</a>.</p></article></body>

Vue.js Interview Challenge — #1 — ToDo Component — Solution

Solution

CodeSandbox

Explanation

When the value of data is an object, it’s shared across all instances of a component.

In our example we assigned our component’s data to a const declared before. By doing this every instance of the component references the same data object. Changing the message in one component changes the message of all other component instances.

We want to reuse this component, allowing users to have multiple items. To achieve that, each instance must generate a unique data object. We do that by return-ing a fresh object in data property.

data() {
  return {
    message: '',
    completed: true
  }
}

Want to try another challenge? Take me to the list of available challenges.

Vuejs
Interview
Interview Questions
Interview Tips
Interview Preparation
Recommended from ReadMedium