大屏项目(一)
项目创建
构建工具:vite+ react-ts
创建项目:pnpm create vite chart-for-screen --template react-ts
安装图表类依赖:pnpm add echarts @jiaminghi/data-view-react
删除index.css和app.css文件里的根元素以外的样式
初始化背景页面布局
css
/* 去除边距 */
body {
margin: 0;
padding: 0;
}
#root {
height: 100vh;
width: 100vw;
background: url("./assets/bg.png");
background-size: 100% 100%;
background-repeat: no-repeat;
}
.app {
width: 1920px;
height: 1080px;
}布局 header 的开发和适配
解决 eslint 报错
从库引入组件的时候,库名报错如下:
Could not find a declaration file for module '@jiaminghi/data-view-react'
原因是该第三方库不支持 ts,所以需要添加//@ts-ignore@ts-ignore注释,但是添加注释后还是报错:
use "@ts-expect-error" instead of "@ts-ignore", as "@ts-ignore" will do nothing if the following line is error-free.
在 eslint 配置文件里 添加 rule:"@typescript-eslint/ban-ts-comment": "off"
Head 组件
tsx
// @ts-ignore
import { Decoration8, Decoration5 } from "@jiaminghi/data-view-react";
import "./index.less";
const Header = () => {
return (
<div className="header">
<Decoration8 style={{ width: "300px", height: "90px" }} />
<div className="head-title">
<span>趋势大屏-基础</span>
<Decoration5 style={{ with: "500px", height: "90px" }} />
</div>
<Decoration8 reverse={true} style={{ width: "300px", height: "90px" }} />
</div>
);
};
export default Header;less
.header {
width: 100%;
height: 90px;
display: flex;
justify-content: space-between;
.head-title {
width: 500px;
position: relative;
> span {
color: #fff;
position: absolute;
font-weight: 400;
font-size: 20px;
top: 5px;
width: 100%;
text-align: center;
}
}
}问题:标题文字无法居中,设置了width:100%就居中了
原因:这个 span 元素设置了绝对定位,脱离了文档流,不再参与行内排版,也不再受 inline 不能设置 width 的限制