Showing posts with label props. Show all posts
Showing posts with label props. Show all posts

Saturday, 25 June 2022

props vs emit in vue.js

 In my last article here , I tried to explain about data(), props, methods and components. Here I’m gonna explain about emit function of vue.js.

<template>
<div>
<input type="text" v-model="firstName"/>
<input type="text" v-model="lastName"/>
<br/>
<button @click='onButtonClick()'>
Click Me
</button>
</div>
</template>
<script>
export default {

props: ['firstName', 'lastName'],

emits: ['onClick'],

methods :{
onButtonClick($event){
this.$emit("onClick", { fName: this.firstName, lName:this.lastName});
}
},

}
</script>
<script>
import MyComp from './MyComponent.vue' //import the component
export default {

components: {
'my-comp' : MyCompl //define the component
},

data() {
return {
firstName: 'Binod',
lastName: 'Mahto',
fullName:'',
}
},

methods : {
mtdCalculateFullName(payload) {
this.$data.firstName = payload.fName;
this.$data.lastName = payload.lName;
this.$data.fullName = this.$data.firstName + ' ' + this.$data.lastName;
}
},
}
</script>
<template>
<div>
<my-comp :first-Name='firstName' :last-Name='lastName' @on-Click='mtdCalculateFullName'></my-comp>
</div>
<div>
<p>Full Name:</p><p>{{fullName}}</p>
</div>
</template>

Thursday, 23 June 2022

data vs props and methods vs computed in Vue.js

 Vue.js is a versatile & popular framework for building web user interfaces. Even I feel, compare to other UI frameworks, Vue framework seems to be easier to learn & code. There are four basic & major concepts of Vue framework which often seems to be confusing from each others, specially for beginners.

<script>
export default {
data() {
return {
firstName: 'Binod',
lastName: 'Mahto',
fullName:'',
methodcount:0,
computedcount:0
}
},

methods : {
mtdCalculateFullName() {
this.$data.methodcount++;
this.$data.fullName = this.$data.firstName + ' ' + this.$data.lastName;
}
},

computed : {
cmptCalculateFullName() {
this.$data.computedcount++;
this.$data.fullName = this.$data.firstName + ' ' + this.$data.lastName;
}
}

}
</script>
<template>
<input type="text" v-model="firstName">
<input type="text" v-model="lastName">
<div>
<button @click='mtdCalculateFullName()'>
Click Me
</button>
<p>Full Name:</p><p>{{fullName}}</p>
<p>count: {{methodcount}}</p>
</div>
<div>
<button @click='cmptCalculateFullName'>
Click Me
</button>
<p>Full Name:</p><p>{{fullName}}</p>
<p>count: {{computedcount}}</p>
</div>
</template>

data():

data() {
return {
firstName: 'Binod',
lastName: 'Mahto',
fullName:'',
methodcount:0,
computedcount:0
}
}
this.$data.{datamembername} 
//not necessary to use $data instead you do this.{datamembername} but i'm doing this for readability purpose.
<template>
<div>
<p>{{greetingMessage}}</p>
</div>
</template>
<script>
export default {
props: ['greetingMessage']
}
</script>
<script>

import MyComp from './MyComp.vue' //import it first
export default {
components: {
MyComp
//define the component
}
,
data() {
return {
message: 'Hi This is a message for props',
}
},


}
</script>
<template>
<MyComp greetingMessage ='Hi This is props'></MyComp>
<MyComp greeting-Message ='Hi This is props'></MyComp>
<MyComp :greeting-Message ='message'></MyComp>

</div>
</template>
<template>
<div>
<p>{{greetingMessage}}</p>
</div>
</template>
<script>
export default {
props: {
greetingMessage : string,
}

}
</script>
methods:{
mymethod1(payload: any){
//logic goes here
}

//syntax to define async methods
async mymethod2(payload: any){
//logic goes here
}
}
<button @click='mtdCalculateFullName()'>
export default {
data() {
return {
firstName: 'Binod',
lastName: 'Mahto'
}
},
computed: {
computedFullName: {
// getter
get() {
return this.$data.firstName + ' ' + this.$data.lastName
},

// setter
set(newValue) {
// Note: we are using destructuring assignment syntax here.
[this.$data.firstName, this.$data.lastName] = newValue.split(' ')

}
}
}
}
this.computedFullName = 'James Bond';//to set the new value
this.computedFullName; // to get the value.
//this is not allowed
computed:{
cmptCalculateFullName(payload : string)
{
}
}
computed: {
computedFullName: {
// setter
set(newValue) {
// Note: we are using destructuring assignment syntax here.
[this.$data.firstName, this.$data.lastName] = newValue.split(' ')
}
}
}