html AdminLTE3侧边栏treeview的React问题

5jdjgkvh  于 2023-11-15  发布在  React
关注(0)|答案(2)|浏览(166)

所以我想做一个React侧边栏,里面有一个项目“Employee”,当点击它时,它会展开三个子项目。
这里是代码

import React, { Component } from "react";

export default class Sidebar extends Component {

  constructor(props) {
    super(props);

    this.state = {
      user: {}
    }
  }

  componentDidMount() {
    let userData = JSON.parse(localStorage.getItem('user'))
    this.setState({user: userData})
  }

  render() {
    return (
      <aside className="main-sidebar sidebar-dark-primary elevation-4" >
        {/* Brand Logo */}
        <a href="/" className="brand-link">
          <span className="brand-text font-weight-light ml-1">HRMS</span>
        </a>
        {/* Sidebar */}
        <div className="sidebar">
          {/* Sidebar user panel (optional) */}
          <div className="user-panel mt-3 pb-3 mb-3 d-flex">
            <div className="image">
              <img
                src={process.env.PUBLIC_URL + '/dist/img/user2-160x160.jpg'}
                className="img-circle elevation-2"
                alt="User Image"
              />
            </div>
            <div className="info">
              <a href="#" className="d-block">
                {this.state.user.fullname}
              </a>
            </div>
          </div>
          {/* Sidebar Menu */}
          <nav className="mt-2">
            <ul
              className="nav nav-pills nav-sidebar flex-column"
              data-widget="treeview"
              role="menu"
              data-accordion="false"
            >
              {/* Add icons to the links using the .nav-icon class
         with font-awesome or any other icon font library */}
              <li className="nav-item">
                <a href="/" className="nav-link active">
                  <i className="nav-icon fas fa-tachometer-alt" />
                  <p>
                    Dashboard
                    <span className="right badge badge-success">Home</span>
                  </p>
                </a>
              </li>
              <li className="nav-item">
                <a href="pages/widgets.html" className="nav-link">
                  <i className="nav-icon fas fa-th" />
                  <p>
                    Widgets
                    <span className="right badge badge-danger">New</span>
                  </p>
                </a>
              </li>
              <li className="nav-item has-treeview">
                <a href="#" className="nav-link">
                  <i className="nav-icon fa fa-user" />
                  <p>
                    Employee
                    <i className="right fas fa-angle-left" />
                  </p>
                </a>
                <ul className="nav nav-treeview">
                  <li className="nav-item">
                    <a href="/employee-add" className="nav-link">
                      <i className="fa fa-user-plus nav-icon" />
                      <p>Add Employee</p>
                    </a>
                  </li>
                  <li className="nav-item">
                    <a href="/employee-list" className="nav-link">
                      <i className="fas fa-users nav-icon" />
                      <p>Employee List</p>
                    </a>
                  </li>
                  <li className="nav-item">
                    <a href="/employee-add" className="nav-link">
                      <i className="far fa-circle nav-icon" />
                      <p>Employee Award</p>
                    </a>
                  </li>
                </ul>
              </li>

字符串
问题是登录后重定向到这个页面,我点击员工项目,href激活,只是重新加载页面(什么href="#”通常)。但是,如果我删除<i></i>标记或href的css得到所有混乱。
我使用的是AdminLTE3,所以这里是文档:https://adminlte.io/docs/3.0/javascript/treeview.html

xbp102n0

xbp102n01#

我找到了答案,因为这是一个问题,许多人。问题发生时,你从另一个页面重定向(即登录页面)
以下是答案的链接:https://github.com/ColorlibHQ/AdminLTE/issues/1570#issuecomment-615841382

r3i60tvu

r3i60tvu2#

试试这个

index.html
将此代码添加到body标记后的脚本中

<script>
      $(document).ready(function(){
        $('.nav-item ').click(function(){

          if($(this).hasClass('menu-open')){
            $(this).removeClass("menu-open");
            $(this).children().removeClass("active");
          }else{
            $(this).addClass("menu-open");
            $(this).children().addClass("active");
          }
          $('.nav-item').children().removeClass('active').not(this);
          $(this).children().addClass("active");

        });
      }); 
</script>

字符串

相关问题