Show bootstrap alert with condition (Vue.js): how can I access this variable in order to change its value?

Viewed 4013

First of all I would like to apologize if the answer to my question is obvious, however since I'm still pretty new to Vue.js, I'm getting really stuck here and I need help.

I got an authentication system and if the user wants to register without putting in an username, I would like to show an bootstrap alert. The code looks like this right now:

<template>
  <div class="container">
    <div class="row">
      <div class="col-md-6 mt-5 mx-auto">
        <form v-on:submit.prevent="register">
          <h1 class="h3 mb-3 font-weight-normal">Register</h1>
          <div class="form-group">
            <label for="username">Username</label>
            <input
              type="text"
              v-model="username"
              class="form-control"
              name="username"
              placeholder="Please choose your username"
            >
          </div>
          <div class="form-group">
            <label for="email">Email Address</label>
            <input
              type="email"
              v-model="email"
              class="form-control"
              name="email"
              placeholder="Please enter your email address"
            >
          </div>
          <div class="form-group">
            <label for="password">Password</label>
            <input
              type="password"
              v-model="password"
              class="form-control"
              name="password"
              placeholder="Please choose your password"
            >
          </div>
          <button class="btn btn-lg btn-primary btn-block" type="submit">Register</button>
        </form>
        <div>
          <b-alert variant="success" show>Example alert</b-alert>
        </div>
        <div>
          <b-alert variant="danger" :show="showAlert">Example Alert!</b-alert>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from "axios";
import router from "../router";

export default {
  data() {
    return {
      username: "",
      email: "",
      password: "",
      showAlert: false        };
  },
  methods: {
    register() {
      axios
        .post("/user/register", {
          username: this.username,
          email: this.email,
          password: this.password
        })
        .then(res => {
          if (!res.data.username) {
            // show an alert, I would like to do something similar to "showAlert = true;"
          } else {
            // redirect to login
          }
        })
        .catch(err => {
          console.log(err);
        });
    }
  }
};
</script>

<style scoped>
@import "../assets/css/reglog.css";
@import "../assets/css/modal.css";
</style>

However I'm not sure how to access the showAlert variable neither how to change its value in the if-statement. The only thing that I know here is that if I change the showAlert manually in the code (line 9 counting from the script tag) from false to true, the page does react and shows the alert when wanted.

I'm sorry if you need more information or if something is unclear, I'm a bit tired and stuck with this for some hours, not gonna lie.

1 Answers

You can access showAlert variable following: this.showAlert = true

.then(res => {
  if (!res.data.username) {
    this.showAlert = true;   // update showAlert
   } else {
    // redirect to login
   }
})

Related