There are eight lifecycle methods:
<template> <div ref="example-element">{{ propertyComputed }}</div> <h1>{{ subtotal }} <h1>{{ shipping }} <span>{{ author.books.length > 0 ? 'Yes' : 'No' }} <button @click="show()">Click me</button> <h2 id="view"></h2> </template> <script> import ExampleAnalyticsService from './example-analytics-service' export default { data() { return { property: 'Example property.', counter: 0, exampleLeakyProperty: 'This represents a property that will leak memory if not cleaned up.', value1: '', result: 0, qty: 1, price: 5, qty2: 1, price2: 5, showTotal: false, n:null, mess:'', pow: 0, n2: 0, books: [ 'Vue 2 - Advanced Guide', 'Vue 3 - Basic Guide', 'Vue 4 - The Mystery' ] } }, beforeCreate() { /*console.log('At this point, events and lifecycle have been initialized.');*/ this.$nextTick(function () { console.log(this.qty); alert(this.qty); }), this.hardData = { n: 21, mess: 'hard coded data is used' }; }, created() { console.log('At this point, this.property is now reactive and propertyComputed will update.'); this.property = 'Example property updated.'; setInterval(() => { this.counter++ }, 1000) }, beforeMount() { console.log(`At this point, vm.$el has not been created yet.`) }, mounted() { console.log(`At this point, vm.$el has been created and el has been replaced.`); console.log(this.$el.textContent) // Example component. this.$el.querySelector("input").value = "Enter name"; this.$el.querySelector("span").innerText = ' you clicked the button'; this.$el.querySelector("span").style.color = 'pink'; this.$el.querySelector("#bn").style.display = 'none'; this.$el.querySelector(".image").src = this.$el.querySelector(".urlinput").value; this.$el.querySelector("span").style.backgroundColor ="red"; this.$nextTick(function () { this.show(); // call method function }); }, beforeUpdate: function () { var data = this.$data; data.pow = Number(data.pow); if (data.pow + '' === 'NaN' || data.pow < 0) { data.pow = 0; } if (data.pow > 1023) { data.pow = 1023; } }, updated: function () { this.figure() }, beforeDestroy() { console.log(`At this point, watchers, child components, and event listeners have not been teared down yet.`) // Perform the teardown procedure for exampleLeakyProperty. // (In this case, effectively nothing) this.exampleLeakyProperty = null delete this.exampleLeakyProperty }, destroyed() { console.log(`At this point, watchers, child components, and event listeners have been torn down.`) console.log(this) ExampleAnalyticsService.informService('Component destroyed.') }, watch: { value1: function (val) { this.value1 = val; this.result = 2 * val; }, result: function (val) { this.result = val; } }, computed: { propertyComputed() { return this.property }, quantity: { get() { return `${this.qty}` }, set(theQuantity) { this.qty = theQuantity } }, priceCalc () { return this.qty * this.price }, quantity2: { get() { return `${this.qty2}` }, set(theQuantity) { this.qty2 = theQuantity } }, priceCalc2 () { return this.qty2 * this.price2 }, subtotal () { return this.priceCalc + this.priceCalc2 }, shipping () { return (2 * 10)/ this.subtotal || '' }, publishedBooksMessage() { return this.author.books.length > 0 ? 'Yes' : 'No' } }, methods : { show: function(){ // Setting text in element document.getElementById("view") .innerHTML = "Hi, This is Vue" // Hiding text after 2 seconds setTimeout(() => { document.getElementById("view") .innerHTML = "" }, 2000); }, figure: function () { this.$data.n2 = Math.pow(2, this.$data.pow); } } </script>
A Vue application consists of a root Vue instance created with new Vue, optionally organized into a tree of nested, reusable components. Whenever you create a new Vue project, the Vue instance gets activated by default in the main.js file by this code:
new Vue({ render: h => h(App), }).$mount(‘#app’)
Composition API is a set of APIs that allows us to author Vue components using imported functions instead of declaring options. It is an umbrella term that covers the following APIs:
Vue 2 use Option API
Total : 27273
Today :9
Today Visit Country :