compositon and option api


In Vue.js, there are two main ways to define and organize the code for a component: the Options API and the Composition API. Let's explore each of them:

  1. Options API: The Options API is the traditional way of defining Vue components. It uses a configuration object with various options to define the component's behavior. Each option corresponds to a specific aspect of the component, such as data, methods, computed properties, lifecycle hooks, and more. Here's an example of a component defined using the Options API:
// Options API component
Vue.component('OptionsComponent', {
  data() {
    return {
      message: 'Hello, Options API!'
    };
  },
  methods: {
    showMessage() {
      console.log(this.message);
    }
  },
  mounted() {
    this.showMessage();
  },
  template: `
    <div>
      <p>{{ message }}</p>
      <button @click="showMessage">Show Message</button>
    </div>
  `
});

In this example, the component has options for data to define reactive properties, methods to define functions, mounted lifecycle hook for initialization, and a template for rendering the HTML structure.

  1. Composition API: The Composition API is an alternative way of organizing and reusing code in Vue components. It provides a set of functions that can be used inside a component's setup function. The setup function is responsible for defining the component's reactive state, methods, computed properties, and other logic. Here's an example of a component defined using the Composition API:
// Composition API component
const CompositionComponent = {
  setup() {
    const message = Vue.ref('Hello, Composition API!');

    const showMessage = () => {
      console.log(message.value);
    };

    Vue.onMounted(() => {
      showMessage();
    });

    return {
      message,
      showMessage
    };
  },
  template: `
    <div>
      <p>{{ message }}</p>
      <button @click="showMessage">Show Message</button>
    </div>
  `
};

Vue.createApp(CompositionComponent).mount('#app');

s

In this example, the component uses functions like Vue.ref to define reactive state, Vue.onMounted to perform actions on component mount, and returns the reactive state and methods from the setup function.

The Composition API promotes better code organization, reusability, and code separation based on logic, rather than options. It allows for composition of logic using functions, making it easier to manage complex components and share functionality between them.

Both the Options API and the Composition API are valid ways to define components in Vue.js. The Composition API is introduced in Vue 3 and provides additional flexibility and scalability, while the Options API is still supported and widely used, especially in Vue 2 projects. The choice between the two depends on the project requirements and personal preference.



options api -> in my regualar defining

optiion api

<script>
export default {
    data() {
        return {
            name: '',
            age: 0,
            aboveAge:false
        }
    },
    computed: {
        displayProfile() {
         return `My name is ${this.name} and i am ${this.age}`;
        }
    },
    methods: {
         verifyUser() {
         if(this.age < 18){
         this.aboveAge = false
        } else {
        this.aboveAge = true    
           }
        },   
    },
    mounted() {
        console.log('Application mounted');
    },
}
</script>


in vue3 compositon api

it is in es6



<script setup>
import {ref, reactive, onMounted } from 'vue'

const profile = reactive({name:'', age:''})
const aboveAge = ref(false)

const verifyUser = () => age.value < 18 ? aboveAge = false : aboveAge = true

const displayProfile = computed(() => {
  return `My name is ${this.name} and i am ${this.age}`;
})

onMounted (() => console.log('Application mounted'))
</script>

re is a table that summarizes the key differences between the Options API and the Composition API:

FeatureOptions APIComposition API
SyntaxUses a set of options to define the component's behavior and stateUses a functional, reactive programming style to define the component's behavior and state
ReusabilityCan be difficult to reuse code between componentsCode can be easily reused between components
FlexibilityLess flexible than the Composition APIMore flexible than the Options API
PerformanceCan be less performant than the Composition APICan be more performant than the Options API




No comments:

Post a Comment

Event listening in react

 How we can listen to som eevents some envents fire like click or automatically user enters into input button , that is event on word type i...