【山竹记账前端-vue】8.业务与轮子的关系
大纲链接 §
[toc]
1. 业务与轮子 ⇧
业务开发
- 完成余下的页面
- 在业务中寻找轮子来造
- 学会造轮子,用造轮子的思维看懂业界源码
- 可以基于业界轮子做二次封装满足公司要求
<Button /><FloatButton /><Layout /><Navbar /><Overlay /><Tabs /><EmojiSelect /><Form />
编程的阶段
- 新手学习怎么用轮子(消费者)
- 老手学习怎么造轮子(生产者)
什么样的轮子值得使用
- 至少一个贡献者持续三年以上的
2. 创建 Button 组件 ⇧
@/components/BarButton.tsx
|
|
- 除了
vue自动绑定的class和style属性,需要额外声明组件外部参数的类型XxxProps
在页面中引用,先创建开始记账路由
@/router/routes.ts
|
|
页面组件
@/views/StartAccountsView.tsx
|
|
- 子组件声明好内部的
Props,父组件使用时可以得到明确的类型提示onClick: ...
3. 创建 FloatButton 和 SvgIcon 组件 ⇧
复用已有的 SvgIcon 组件,添加Props类型 ⇧
iconfont给设计师的图标绘制标准
- 图标绘制
- 需要设计师按照标准设计图标,尽量使用矢量图
SVG格式 - 注意下载时,根据是否需要自定义填充色,选择去除填充色,即
svg中所有的fill属性
@/components/icons/SvgIcon.tsx
已单独声明图标名称枚举类型
SvgIconNames1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41import { defineComponent, type PropType } from 'vue' import { cn } from 'cn' import { svgIconNames, type SvgIconNames } from '@/components/icons/svgIconNames.ts' interface SvgIconProps { name: SvgIconNames } /** * @Description: 图标组件 * @Description: 默认使用插件 vite-plugin-svg-icons-ng * @Description: 替换使用插件 svgoStore * @FilePath: content/post/山竹记账/【山竹记账前端-vue】8.业务与轮子的关系.md */ export const SvgIcon = defineComponent<SvgIconProps>({ name: 'SvgIcon', props: { name: { type: String as PropType<SvgIconNames>, required: true, validator: (value: unknown) => typeof value === 'string' && (svgIconNames as readonly string[]).includes(value), }, }, setup(props, _ctx) { // 添加验证 if (!props.name || !(svgIconNames as readonly string[]).includes(props.name)) { console.error(`Invalid icon name: ${props.name}`) } return () => ( <svg class={cn( 'where:(h-1em w-1em align-[-0.15em] fill-[currentColor] overflow-clip)', )} aria-hidden="true"> <use xlinkHref={`#icon-${props.name}`}/> </svg> ) }, })
封装 FloatButton 组件 ⇧
- 添加了一个已去色的图标
add.svg作为按钮图标
@/components/FloatButton.tsx
|
|
在
@/views/StartAccountsView.tsx中使用
|
|
完成 StartAccountsView 页面组件 ⇧
支持
FloatButton.tsx组件图标可自定义修改为项目中已有的图标
|
|
@/views/StartAccountsView.tsx
|
|
继续 Navbar 与 Overlay 组件
4. 制作 Center 组件 ⇧
新增 PlaceCenter 居中布局组件
@/components/icons/PlaceCenter.tsx
|
|
添加居中图标组件
@/views/StartAccountsView.tsx
|
|
5. 制作 Navbar 组件 ⇧
@/components/NavBar.tsx
|
|
@/components/NavBar.module.scss
|
|
@src/views/StartAccountsView.tsx
|
|
- 使用具名插槽传参
icon和头部标题文字
6. 制作 Overlay 组件 ⇧
点击菜单按钮,左侧划出浮层
- 浮动层分为两个部分: 主要侧边内容 + 遮罩层
- 点击遮罩层关闭整个浮层
@src/components/FloatOverlay.tsx
|
|
- 这里可直接传外部参数
onClose: { type: Function as PropType<() => void>, } - 等效于使用
const {emit} = _ctx,然后调用const onClose = () => { emit('close') }- 发布
close事件,父组件使用<FloatOverlay onClose={onClose} />监听事件
- 发布
@/views/StartAccountsView.tsx
|
|
4. 完善 Overlay 样式,重构拆分组件 ⇧
- 拆分为几个部分:
OverlayMask、OverlayUserHeader、MenuLinkList、 - 使用 pinia 统一维护
overlayVisible状态
@/stores/useOverlayStore.ts
|
|
将
@/modules/overlay/FloatOverlay.tsx重构为
|
|
@/views/StartAccountsView.tsx
仅处理打开浮层菜单的功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42// import ... import { FloatOverlay } from '@/modules/overlay/FloatOverlay.tsx' import { storeToRefs } from 'pinia' import { useOverlayStore } from '@/stores/useOverlayStore.ts' export const StartAccountsView = defineComponent({ name: 'StartAccountsView', setup(/*props, ctx*/) { const { overlayVisible } = storeToRefs(useOverlayStore()) const { toggleOverlay } = useOverlayStore() const onClickMenu = () => { toggleOverlay() } return () => ( <div class={cn('start-accounts-view w-full h-full position-relative')}> <NavBar direct-scoped class="navbar bg-gradient-to-b from-[var(--navbar-bg-start)] to-[var(--navbar-bg-end)]"> {{ default: () => '山竹记账', icon: () => <SvgIcon name="menu" class="w-2em h-2em" onClick={onClickMenu} />, }} </NavBar> <PlaceCenter class="p-y-10em"> <SvgIcon class="w-8em h-8em" name="pig" /> </PlaceCenter> <div class={cn('p-x-1em')}> <BarButton>开始记账</BarButton> </div> <FloatButton iconName="add" class="fixed bottom-1em right-1em" /> { overlayVisible.value && <FloatOverlay direct-scoped /> } </div> ) }, })
@/modules/overlay/OverlayMask.tsx
主要处理遮罩样式 和 关闭浮层的逻辑功能
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22import { defineComponent /* type PropType */ } from 'vue' import { cn } from 'cn' import { useOverlayStore } from '@/stores/useOverlayStore.ts' export const OverlayMask = defineComponent({ name: 'OverlayMask', setup(/* _props, _ctx */) { const { closeOverlay } = useOverlayStore() const onClose = () => { closeOverlay() } return () => ( <section onClick={onClose} class={cn( 'fixed z-[var(--z-index-float-overlay)] bg-[var(--overlay-mask-bg)]', 'top-[var(--top-safe-area-height)] left-0', 'w-full h-[calc(100%-var(--top-safe-area-height))]', )}></section> ) }, })
@/modules/overlay/OverlayUserHeader.tsx
目前进展示用户未登录的文字
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20import { defineComponent /* type PropType */ } from 'vue' import { cn } from 'cn' export const OverlayUserHeader = defineComponent({ name: 'OverlayUserHeader', setup(/* _props, _ctx */) { const onClickSignIn = () => { console.log('点击登录') } return () => ( <section onClick={onClickSignIn} class={cn( 'p-y-2em p-x-1em', 'bg-[var(--overlay-user-bg)] text-[var(--overlay-user-text)]', )}> <h2>未登录用户</h2> <p>点击这里登录</p> </section> ) }, })
@/modules/overlay/MenuLinkItem.tsx
拆分为单个的路由跳转链接组件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35import { defineComponent, type PropType } from 'vue' import { RouterLink } from 'vue-router' import { cn } from 'cn' import { SvgIcon } from '@/components/icons/SvgIcon.tsx' import type { SvgIconName } from '@svgoStore' export const MenuLinkItem = defineComponent({ name: 'MenuLinkItem', props: { linkPath: { type: String as PropType<string>, default: '/', }, linkTitle: { type: String as PropType<string>, default: '标题占位', }, iconName: { type: String as PropType<SvgIconName>, default: 'pig', }, }, setup(_props, _ctx) { return () => ( <li> <RouterLink to={_props.linkPath} class={cn('flex justify-start items-center', 'p-y-12px p-x-1em')}> <SvgIcon name={_props.iconName} class="mr-12px" /> <span>{_props.linkTitle}</span> </RouterLink> </li> ) }, })
@/modules/overlay/MenuLinkList.tsx
表驱动管理跳转菜单列表
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38import { defineComponent } from 'vue' import { cn } from 'cn' import type { SvgIconName } from '@svgoStore' import { MenuLinkItem } from '@/modules/overlay/MenuLinkItem.tsx' type MenuLinkItemType = { linkPath: string linkTitle: string iconName: SvgIconName } export const MenuLinkList = defineComponent({ name: 'MenuLinkList', setup(/* _props, _ctx */) { const menuLinkItemList: Readonly<MenuLinkItemType[]> = Object.freeze([ {linkPath: '/statistics', linkTitle: '统计图表', iconName: 'chart'}, {linkPath: '/export', linkTitle: '导出数据', iconName: 'export'}, {linkPath: '/notify', linkTitle: '记账提醒', iconName: 'notify'}, {linkPath: '/tech_stack', linkTitle: '本App技术栈', iconName: 'tech_stack'} ]) return () => ( <nav> <menu class={cn( 'p-t-1em text-20px' )}> {menuLinkItemList.map((item) => ( <MenuLinkItem key={item.linkPath} linkPath={item.linkPath} linkTitle={item.linkTitle} iconName={item.iconName}/> ))} </menu> </nav> ) }, })
参考文章
相关文章
- 无
- 作者: Joel
- 文章链接:
- 版权声明
- 非自由转载-非商用-非衍生-保持署名