需要更新VueJS页面才能更新ui

时间:2018-08-03 09:59:09

标签: vue.js

当用户登录时,我想显示一个注销按钮,但这需要我在登录后重新加载页面,即使它是一个单页应用程序,例如下面的代码:

当用户登录时,我想显示一个注销按钮,但这需要我在登录后重新加载页面,即使它是一个单页应用程序,例如下面的代码:

App.vue:

<template>
    <v-app>
  <v-toolbar class="yellow darken-1">
    <v-toolbar-title>Check data</v-toolbar-title>
    <v-spacer></v-spacer>
    <v-toolbar-side-icon class="hidden-md-and-up"></v-toolbar-side-icon>
    <v-toolbar-items class="hidden-sm-and-down">
      <v-btn small primary v-if="isLoggedIn" @click="logout()">Logout</v-btn>
    </v-toolbar-items>
  </v-toolbar>
  <v-content>
    <router-view>
      <v-container fluid></v-container>
    </router-view>
  </v-content>
    </v-app>
</template>

<script>
import * as Auth from '@/components/Authentication'
import Authentication from '@/components/Authentication'

export default {
  name: 'App',
  data() {
    return {
      isLoggedIn: Auth.default.user.authenticated,
    }
  },
  methods: {
    logout() {
      Authentication.logOutUser(this);
    }
  }
}
</script>

我在其中登录用户并存储令牌的身份验证代码,如下所示:

import Axios from 'axios'
import router from '@/router'

export default {
  user: { authenticated: false },

  authenticate (context, credentials, redirect) {
        var _this = this;
        Axios.post(`${url}`, credentials)
        .then(function (response) {
          // handle success
          console.log(response.data.data.data.message.token);

          let token = response.data.data.data.message.token;
          context.$cookie.set('token', token, '1D')
          context.validLogin = true
          _this.user.authenticated = true
          if (redirect) router.push(redirect)

        })
        .catch(function (error) {
          // handle error
          console.log(error);
          context.snackbar = true
          context.message = 'Login failed, please try again.'
        })
  },

  checkAuthentication () {
    const token = document.cookie

    if (token) this.user.authenticated = true
    else this.user.authenticated = false
  },

  logOutUser(context) {
    context.$cookie.delete('token');
    context.validLogin = false
    this.user.authenticated = false
    router.push('/login')
  },

  getAuthenticationHeader (context) {
    return `Bearer ${context.$cookie.get('token')}`
  }
}

0 个答案:

没有答案