最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vuetify v-alert not showing when reactive property is updated - Stack Overflow

programmeradmin4浏览0评论

I want to conditionally render a v-alert if the login response code equals 401 Unauthorized. I have the alert defined below:

<v-alert v-if="this.show" type="error">Invalid email and/or password.</v-alert>

In the data section of my ponent, I define the property show which is by default set to false to not show the v-alert:

export default {
  name: "loginponent",
  data() {
    return {
      user: {},
      show: false,
    };
  },

In the methods section of my ponent, I attempt user authentication and if the status code equals 401, I update this.show = true:

methods: {
    login: function() {
      let user = {
        email: this.user.email,
        password: this.user.password
      };
      authService.login(user).then(auth => {
        if (auth.response.status === 401) {
          this.show = true;
        }
      });
    }
  }

I would have thought this to work as show is reactive since it's defined in the data() section of my ponent.

Any idea what I can do to get this conditionally rendering properly?

Many thanks in advance,
Chappie Johnson

I want to conditionally render a v-alert if the login response code equals 401 Unauthorized. I have the alert defined below:

<v-alert v-if="this.show" type="error">Invalid email and/or password.</v-alert>

In the data section of my ponent, I define the property show which is by default set to false to not show the v-alert:

export default {
  name: "loginponent",
  data() {
    return {
      user: {},
      show: false,
    };
  },

In the methods section of my ponent, I attempt user authentication and if the status code equals 401, I update this.show = true:

methods: {
    login: function() {
      let user = {
        email: this.user.email,
        password: this.user.password
      };
      authService.login(user).then(auth => {
        if (auth.response.status === 401) {
          this.show = true;
        }
      });
    }
  }

I would have thought this to work as show is reactive since it's defined in the data() section of my ponent.

Any idea what I can do to get this conditionally rendering properly?

Many thanks in advance,
Chappie Johnson

Share edited Aug 25, 2019 at 8:34 Boussadjra Brahim 1 asked Aug 24, 2019 at 22:16 Joe B.Joe B. 8203 gold badges12 silver badges28 bronze badges 7
  • please share how do you're calling login method – Boussadjra Brahim Commented Aug 24, 2019 at 22:29
  • i am calling the login method from the methods section. – Joe B. Commented Aug 24, 2019 at 22:33
  • please show how you're calling it like this.login() or @click="login" – Boussadjra Brahim Commented Aug 24, 2019 at 22:36
  • I am doing @click="login" – Joe B. Commented Aug 24, 2019 at 22:37
  • try this.$forceUpdate(); after this.show = true; – Boussadjra Brahim Commented Aug 24, 2019 at 22:42
 |  Show 2 more ments

1 Answer 1

Reset to default 5

Try to remove the this keyword from template :

 <v-alert v-if="show" type="error">Invalid email and/or password.</v-alert>

if this doesn't work try a show puted property based on status :

new Vue({
  el: '#app',
  vuetify: new Vuetify(),
  data() {
    return {
      status: null
    }
  },
  puted: {
    show() {
      return this.status === 401;
    }
  },
  methods: {
    login: function() {
      setTimeout(() => {
        this.status = 401;
        console.log(this.show)
      }, 3000)

    }
  }
})
<script src="https://cdn.jsdelivr/npm/babel-polyfill/dist/polyfill.min.js"></script>
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/vue.js"></script>
<script src="https://cdn.jsdelivr/npm/[email protected]/dist/vuetify.js"></script>
<link href="https://cdn.jsdelivr/npm/@mdi/[email protected]/css/materialdesignicons.min.css" rel="stylesheet">
<link href="https://cdn.jsdelivr/npm/[email protected]/dist/vuetify.min.css" rel="stylesheet">

<div id="app" data-app>
  <v-btn small color="primary" @click="login">Login</v-btn>
  <v-alert v-if="show" type="error" >
    Invalid email and/or password
  </v-alert>
</div>

发布评论

评论列表(0)

  1. 暂无评论