Translate

Saturday 11 November 2023

Anatomy of a component in vue.js

 Anatomy of a component in vue.js


The anatomy of a Vue.js component consists of three parts:

  • Template: The template is the HTML code that defines the component's user interface. It can contain HTML elements, directives, and other components.

  • Script: The script is the JavaScript code that defines the component's behavior. It can contain data properties, methods, computed properties, and watchers.

  • Style: The style is the CSS code that defines the component's appearance. It can contain CSS rules that target the component's HTML elements.

Example:


HTML

<template>
  <div class="my-component">
    <h1>{{ title }}</h1>
    <p>{{ message }}</p>
    <button @click="incrementCounter">Increment</button>
    <p>Count: {{ counter }}</p>
  </div>
</template>

<script>
export default {
  name: "MyComponent",
  data() {
    return {
      title: "My Component",
      message: "This is my component.",
      counter: 0,
    };
  },
  methods: {
    incrementCounter() {
      this.counter++;
    },
  },
};
</script>

<style>
.my-component {
  border: 1px solid black;
  padding: 10px;
}
</style>

This component displays a heading, a paragraph, and a button. The button increments the component's counter state, which is displayed in another paragraph.

Components can be nested inside other components to create complex UIs. For example, the following component nests the MyComponent component inside a Header component:


HTML

<template>
  <header>
    <my-component />
  </header>
</template>

<script>
import MyComponent from "./MyComponent.vue";

export default {
  name: "Header",
  components: {
    MyComponent,
  },
};
</script>

<style>
header {
  background-color: #ccc;
  padding: 10px;
}
</style>

This component will display the MyComponent component inside a header element.

Components can also communicate with each other using props and events. Props are data properties that are passed from a parent component to a child component. Events are emitted from a child component to a parent component.

For example, the following component emits an incrementCounter event when the button is clicked:


HTML

<template>
  <div class="my-component">
    <button @click="emitIncrementCounterEvent">Increment</button>
  </div>
</template>

<script>
export default {
  name: "MyComponent",
  methods: {
    emitIncrementCounterEvent() {
      this.$emit("incrementCounter");
    },
  },
};
</script>

This component can be used in a parent component like this:


HTML

<template>
  <div>
    <my-component @incrementCounter="incrementCounter" />
    <p>Count: {{ counter }}</p>
  </div>
</template>

<script>
import MyComponent from "./MyComponent.vue";

export default {
  name: "App",
  components: {
    MyComponent,
  },
  data() {
    return {
      counter: 0,
    };
  },
  methods: {
    incrementCounter() {
      this.counter++;
    },
  },
};
</script>

When the button in the MyComponent component is clicked, the incrementCounter event will be emitted. The parent component will then call its incrementCounter method, which will increment the counter state.

Components are a powerful feature of Vue.js that can help you write more reusable, maintainable, and scalable code.


Interview questions and answers for the anatomy of a Vue.js component:

Q: What are the three parts of a Vue.js component?

A: The three parts of a Vue.js component are:

  • Template: The template is the HTML code that defines the component's user interface.

  • Script: The script is the JavaScript code that defines the component's behavior.

  • Style: The style is the CSS code that defines the component's appearance.

Q: What is the purpose of the template in a Vue.js component?

A: The purpose of the template in a Vue.js component is to define the component's user interface. The template can contain HTML elements, directives, and other components.

Q: What is the purpose of the script in a Vue.js component?

A: The purpose of the script in a Vue.js component is to define the component's behavior. The script can contain data properties, methods, computed properties, and watchers.

Q: What is the purpose of the style in a Vue.js component?

A: The purpose of the style in a Vue.js component is to define the component's appearance. The style can contain CSS rules that target the component's HTML elements.

Q: What is the difference between a global component and a local component in Vue.js?

A: A global component can be used anywhere in the application. It is registered globally using the Vue.component() method. A local component can only be used within the scope in which it is defined. It is registered locally using the components option in a Vue component.

Q: What are some best practices for writing Vue.js components?

A: Some best practices for writing Vue.js components include:

  • Keep your components small and focused. This will make them more reusable and maintainable.

  • Use descriptive names for your components. This will make your code more readable and understandable.

  • Document your components. This will help other developers to understand how your components work and how to use them.

  • Test your components. This will help to ensure that your components work as expected.

I hope this helps!

No comments:

Post a Comment

Note: only a member of this blog may post a comment.