react_15

动态菜单

图标要独立安装依赖

npm install @ant-design/icons

图标组件,用来将字符串图标 转换为标签图标

TypeScript 复制代码
import * as icons from "@ant-design/icons";
interface Module {
  [p: string]: any;
}
const all: Module = icons;
export default function Icon({ name }: { name: string }) {
  const Icon = all[name];
  return <Icon></Icon>;
}

登录页面

TypeScript 复制代码
import { Button, Form, Input, message } from "antd";
import RoutesStore from "../store/RoutesStore";
import { LoginReq } from "../model/Student";
import { observer } from "mobx-react-lite";
import { useNavigate } from "react-router-dom";
import { useEffect } from "react";

function A8Login() {
  function onFinish(values: LoginReq) {
    RoutesStore.login(values);
  }
  const nav = useNavigate();
  useEffect(() => {
    if (RoutesStore.state === "success") {
      //登陆成功,跳转到主页
      nav("/");
    } else if (RoutesStore.state === "error") {
      //登录失败
      message.error(RoutesStore.message);
    }
  }, [RoutesStore.state]);
  return (
    <div style={{ paddingTop: 200 }}>
      <Form
        name="basic"
        labelCol={{ span: 8 }}
        wrapperCol={{ span: 8 }}
        onFinish={onFinish}
      >
        <Form.Item
          label="用户名"
          name="username"
          rules={[{ required: true, message: "用户名必须" }]}
        >
          <Input autoComplete="off" />
        </Form.Item>
        <Form.Item
          label="密码"
          name="password"
          rules={[{ required: true, message: "密码必须" }]}
        >
          <Input.Password autoComplete="off" />
        </Form.Item>
        <Form.Item wrapperCol={{ offset: 8, span: 6 }}>
          <Button type="primary" htmlType="submit">
            登录
          </Button>
        </Form.Item>
      </Form>
    </div>
  );
}
export default observer(A8Login);
  • 用 useNavigate() 返回的函数跳转的代码不能包含在函数式组件的主逻辑中,只能放在

    • 其它事件处理函数中

    • 写在副作用函数 useEffect 之中

相关推荐
欣欣之王来了10 分钟前
原始HTML插值:v-html指令的使用与安全风险
前端·学习·架构·项目·vue3教程
IT_陈寒12 分钟前
Redis的LRU淘汰策略坑了我一整天
前端·人工智能·后端
计算机魔术师17 分钟前
砍半价格、参数再涨,国产旗舰追到 Opus 鼻尖了
前端
特立独行的猫A30 分钟前
10 MB 的桌面 AI 客户端:用仓颉 + 系统 WebView 替代 Electron
前端·harmonyos
修炼的dance1 小时前
白底图存JPG,sharp开mozjpeg背景杂点少近三成
前端
anOnion2 小时前
构建无障碍组件之Grid Pattern
前端·html·交互设计
乔旭伟2 小时前
Vben3 中后台表格封装实战:useVbenVxeGrid 的全局收敛与七个坑
前端·anti-design-vue·1024程序员节
鶴哥只手遮天2 小时前
jQuery 4.0 源码架构与工程实践-第 10 章 尺寸、几何与显隐
前端·javascript·jquery
Mia_墨黎2 小时前
2026 Windows 11 终端美化完全指南
前端·后端
用户1880687493522 小时前
戴着 Rokid 接成语:用 AIUI 做一局有提示、有复盘的“接着说”
前端