如何使用角色?

发布于 2025-02-12 19:32:11 字数 1742 浏览 0 评论 0原文

login = async (creds: UserFormValues) => {
        try {
            const user = await agent.Account.login(creds);
            store.commonStore.setToken(user.token);
            runInAction(() => this.user = user);
            history.push('/activities');
            store.modalStore.closeModal();
        } catch (error) {
            throw error;
        }
    }

我有这个“登录”,我想编辑它以扮演角色。喜欢:

login = async (creds: UserFormValues) => {
        try {
            const user = await agent.Account.login(creds);
            store.commonStore.setToken(user.token);
            runInAction(() => this.user = user);
            if(user.role = "admin"){
                history.push('/activities');
            }
            else{
                history.push('/home');
            }
            store.modalStore.closeModal();
        } catch (error) {
            throw error;
        }
    }

但是我得到了警告

期望有条件的表达式,而是看到了一个分配无核对

,并且该平台仍在运行,但它忽略了if语句。如何对此做出有条件的表达(如果您还可以向我展示有关它的代码)?另外,我有5个角色应该在不同的页面上进行。

login = async (creds: UserFormValues) => {
            try {
                const user = await agent.Account.login(creds);
                store.commonStore.setToken(user.token);
                runInAction(() => this.user = user);
                if(user.role === "admin"){
                    history.push('/activities');
                }
                else{
                    history.push('/home');
                }
                store.modalStore.closeModal();
            } catch (error) {
                throw error;
            }
        }

这是更新的代码,当我登录Admin时,它将我重定向到家而不是活动,例如不存在。

login = async (creds: UserFormValues) => {
        try {
            const user = await agent.Account.login(creds);
            store.commonStore.setToken(user.token);
            runInAction(() => this.user = user);
            history.push('/activities');
            store.modalStore.closeModal();
        } catch (error) {
            throw error;
        }
    }

I have this "login" that I want to edit it to make it with roles. Like:

login = async (creds: UserFormValues) => {
        try {
            const user = await agent.Account.login(creds);
            store.commonStore.setToken(user.token);
            runInAction(() => this.user = user);
            if(user.role = "admin"){
                history.push('/activities');
            }
            else{
                history.push('/home');
            }
            store.modalStore.closeModal();
        } catch (error) {
            throw error;
        }
    }

But I get the warning

Expected a conditional expression and instead saw an assignment no-cond-assign

and still the platform runs but it ignores the if statement. How can I make a conditional expression about this(If you can also show me the code about it)? Also I have 5 roles which should go all on different pages.

login = async (creds: UserFormValues) => {
            try {
                const user = await agent.Account.login(creds);
                store.commonStore.setToken(user.token);
                runInAction(() => this.user = user);
                if(user.role === "admin"){
                    history.push('/activities');
                }
                else{
                    history.push('/home');
                }
                store.modalStore.closeModal();
            } catch (error) {
                throw error;
            }
        }

This is the updated code, when I login as admin it redirects me to home and not into activities, like the if doesn't exist.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文