Thursday 29 April 2021

Vue.js - Passing additional arguments with the Event through @click function.

So here what I would like to do is send some data that I have in my loop through to the function that I'm using in my @click to the method I'm using ( that interacts with Firebase ) . 

Originally my call to this method from 'v-btn' looked like this. 

<v-btn
color="deep-purple dark"
text
@click="makeAdmin"
>


The event here passes through without us having to add the arguments. It's implied.

async makeAdmin(e) {
const functions = firebase.functions();
e.preventDefault();

We could write @click="makeAdmin(event)"

if we wanted to be explicit.  

To pass through an argument from the loop we are in then the following example works. 

<v-list-item
v-for="(Profile) in profiles"
:key="Profile.id"
>
<v-avatar
color="primary"
size="32"
>
<v-img :src='Profile.image'
max-width='100'
overline
/>
</v-avatar>
<v-list-item-content>

<v-list-item-title >{{Profile.firstname}} {{Profile.surname}}</v-list-item-title>
</v-list-item-content>
<v-card-actions>
<v-form
ref="form"
id="makeAdmin"
>
<v-btn
color="deep-purple dark"
text
@click="makeAdmin(Profile.email, $event)"
>
Make Admin
</v-btn>

And the method will look like. 

async makeAdmin(user_email, e) {
const functions = firebase.functions();
e.preventDefault();
const addAdminRole = functions.httpsCallable('addAdminRole');
addAdminRole({email: user_email}).then(result => {
console.log('successful ! ' + result)
})
},

No comments: