【问题标题】:Jhipster dynamically add button based on user roleJhipster 根据用户角色动态添加按钮
【发布时间】:2021-03-22 11:57:35
【问题描述】:

我对使用 react 很陌生,并且一直在努力根据当前用户分配的角色动态地将按钮添加到列表中。

场景是我希望用户只查看一些数据,但如果用户是“管理员”,那么他们可以更新和删除它。如果用户没有正确的角色,我希望没有按钮可以更新和删除。

我尝试过的大多数解决方案都指向用户无法检查详细信息,所以我假设我错过了某个地方的导入,但不确定它会在哪里

<div className="btn-group flex-btn-group-container">
  <Button tag={Link} to={`${match.url}/${product.id}`} color="info" size="sm">
    <FontAwesomeIcon icon="eye" /> <span className="d-none d-md-inline">View</span>
  </Button>
  <Button
    tag={Link}
    to={`${match.url}/${product.id}/edit?page=${paginationState.activePage}&sort=${paginationState.sort},${paginationState.order}`}
    color="primary"
    size="sm"
  >
    <FontAwesomeIcon icon="pencil-alt" /> <span className="d-none d-md-inline">Edit</span>
  </Button>
  <Button
    tag={Link}
    to={`${match.url}/${product.id}/delete?page=${paginationState.activePage}&sort=${paginationState.sort},${paginationState.order}`}
    color="danger"
    size="sm"
  >
    <FontAwesomeIcon icon="trash" /> <span className="d-none d-md-inline">Delete</span>
  </Button>
</div>

这是我要隐藏/显示的代码。

【问题讨论】:

    标签: reactjs spring-boot jhipster


    【解决方案1】:

    我假设您有一种方法可以检查用户是否是像 isAdmin(user) 这样的管理员。如果是这种情况,你可以这样做{ isAdmin(user) &amp;&amp; &lt;button&gt;Admin Only Button&lt;/button&gt; }

    【讨论】:

    • 不,这是我的问题所在,我有一个用于用户管理的减速器文件,但无论我尝试导入什么,我都无法在我的 product.tsx 文件中看到任何用户信息
    • 你能举一个你的 Redux 存储状态的例子吗?
    猜你喜欢
    • 2016-07-30
    • 2012-02-11
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    • 2013-11-11
    • 2014-07-16
    • 2012-06-17
    • 1970-01-01
    相关资源
    最近更新 更多