我想在标题组件中显示不同的按钮,基于用户身份验证

Header.vue

<template>
        <div class="utf_right_side">
          <div class="header_widget">
            <router-link :to="{name:'login'}" class="button border sign-in popup-with-zoom-anim" v-if="!isAuth"><i class="fa fa-sign-in"></i>Login</router-link>
            <a class="button border sign-in popup-with-zoom-anim" v-if="isAuth" href="" @click.prevent="logout" :key="componentKey"><i class="fa fa-sign-in"></i>Logout</a>
            <a href="dashboard_add_listing.html" class="button border with-icon"><i class="sl sl-icon-user"></i> Add Listing</a></div>
        </div>
</template>

<script>
    import {mapActions} from 'vuex'
    export default {
        name:"default-layout",
        data(){
            return {
                user:this.$store.state.auth.user,
                isAuth: this.$store.state.auth.authenticated,
            }
        },
        methods:{
            ...mapActions({
                signOut:"auth/logout"
            }),
            async logout() {
                await axios.post('/logout').then(({data})=>{
                    this.signOut();
                    this.$parent.forceRerender();
                })
            },
        },

    }

</script>

正如您所看到的,根据来自VUEX状态的变量isAuth,我想显示不同的按钮,但在登录后状态不变,它仍然显示旧的按钮(在身份验证之前).如果我手动刷新页面(F5),它会显示正确的按钮.

Login.vue:

<script>
import { mapActions } from 'vuex'
export default {
    name:"login",
    data(){
        return {
            auth:{
                email:"",
                password:""
            },
            validationErrors:{},
            processing:false
        }
    },
    methods:{
        ...mapActions({
            signIn:'auth/login'
        }),
        async login(){
            this.processing = true
            await axios.get('/sanctum/csrf-cookie')
            await axios.post('/login',this.auth).then(({data})=>{
                this.signIn()
            }).catch(({response})=>{
                if(response.status===422){
                    this.validationErrors = response.data.errors
                }else{
                    this.validationErrors = {}
                    alert(response.data.message)
                }
            }).finally(()=>{
                this.processing = false
            })
        },
    }
}
</script>

vuex auth.js包含在index.js vuex文件中:

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

export default {
    namespaced: true,
    state:{
        authenticated:false,
        user:{}
    },
    getters:{
        authenticated(state){
            return state.authenticated
        },
        user(state){
            return state.user
        }
    },
    mutations:{
        SET_AUTHENTICATED (state, value) {
            state.authenticated = value
        },
        SET_USER (state, value) {
            state.user = value
        }
    },
    actions:{
        login({commit}){
            return axios.get('/api/user').then(({data})=>{
                commit('SET_USER',data)
                commit('SET_AUTHENTICATED',true)
                router.push({name:'home'})
            }).catch(({response:{data}})=>{
                commit('SET_USER',{})
                commit('SET_AUTHENTICATED',false)
            })
        },
        logout({commit}){
            commit('SET_USER',{})
            commit('SET_AUTHENTICATED',false)
        }
    }
}

因此,当用户登录时,它在auth.js中进入登录方法,并设置正确的状态,如下所示:

        commit('SET_USER',data)
        commit('SET_AUTHENTICATED',true)|

之后,它重定向到名为Home的路由,但页眉仍然显示旧按钮,当我刷新页面时,显示正确的按钮.

推荐答案

代替存储状态isAuth: this.$store.state.auth.authenticated,try 导入获取器

import { mapGetters } from 'vuex'

并在计算(computed)属性中使用getter,它是react 性的,如下所示:

 computed: {
  ...mapGetters({ isAuth: 'auth/authenticated' }),
 },

Javascript相关问答推荐

获取加载失败:获取[.]添加时try 将文档添加到Firerestore,Nuxt 3

React存档iframe点击行为

如何为GrapesJS模板编辑器创建自定义撤销/重复按钮?

我试图实现用户验证的reduxstore 和操作中出了什么问题?

如何在不创建新键的情况下动态更改 map 中的项目?

有没有可能使滑动img动画以更快的速度连续?

切换时排序对象数组,切换不起作用

Chromium会将URL与JS一起传递到V8吗?

保持物品顺序的可变大小物品分配到平衡组的算法

未定义引用错误:未定义&Quot;而不是&Quot;ReferenceError:在初始化&Quot;之前无法访问';a';

使用领域Web SDK的Vite+Vue应用程序中的Web程序集(WASM)错误

第二次更新文本输入字段后,Reaction崩溃

扩展类型的联合被解析为基类型

删除元素属性或样式属性的首选方法

将范围 Select 器添加到HighChart面积图

如何使用抽屉屏幕及其子屏幕/组件的上下文?

Cherrio JS返回父div的所有图像SRC

我怎样才能得到一个数组的名字在另一个数组?

未捕获的不变违规:即使在使用DndProvider之后也应使用拖放上下文

如何在Java脚本中添加一个可以在另一个面板中垂直调整大小的面板?