Home > Software design >  How to pass a variable from one method to another Vue?
How to pass a variable from one method to another Vue?

Time:11-19

Below is an example of my old code which was working. This code created a confirmation screen and on conformation deleted a user. However I want to use a toast element to help create a better confirmation screen. This toast element requires the methods to be split up into onConfirm and onReject. And im wondering if there is a way to pass the variable from my deleteUser method into the onConfirm method similar to how I pass it in the code below. (.then(this.onTeacherDeleted(user))

deleteUser(user) {
      if (confirm("Are you sure you want to delete: "   user.name   "?")) {
        this.working = true;
        axios.delete('/teachers/'   user.id)
          .then(this.onTeacherDeleted(user))}
      // this.$toast.add({severity: 'warn', summary: 'Are you sure?', detail: 'Are you sure you want to delete this placement?', group: 'bc'});
    
    },

onTeacherDeleted (user) {
      this.$toast.add({severity:'success', summary: user.name   'Deleted Successfully', detail:'The teacher was successfully delted from the database', life: 3000});
      window.location.reload()
      }

How do I pass the user variable into the onConfirm method from the deleteUser method?

    deleteUser(user) {
      this.$toast.add({severity: 'warn', summary: 'Are you sure?', detail: 'Are you sure you want to delete this placement?', group: 'bc'});
    
    },

    onConfirm(user) {
      this.$toast.removeGroup('bc');
      this.working = true;
        axios.delete('/teachers/'   user.id)
          .then(this.onTeacherDeleted(user))
    },

  onReject() {
      this.$toast.removeGroup('bc');
  },

The onConfirm and onReject are both on click events that happen earlier in the code (see below) :

<button  type="button" @click="onConfirm" this.onTeacherDeleted(user) aria-expanded="false">
<button  type="button" @click="onReject" aria-expanded="false">

CodePudding user response:

If you show more of your code maybe i can give a better solution, but from what i can see, you can just make a variable to track which user is being deleted at the moment and set its value in the:

deleteUser(user) {
  this.$toast.add({severity: 'warn', summary: 'Are you sure?', detail: 'Are you sure you want to delete this placement?', group: 'bc'});
  this.userToBeDeleted = user;
},

And then just use that variable in the onConfirm:

onConfirm() {
  this.$toast.removeGroup('bc');
  this.working = true;
    axios.delete('/teachers/'   this.userToBeDeleted.id)
      .then(this.onTeacherDeleted(this.userToBeDeleted))
},
  • Related