创建base公共组件

公共模块

1

基础模块参照了vant的思路,使用bem命名规范。先创建一个命名空间,这个命名空间返回创建组件函数与生成命名方法。在创建组件函数中创建nameinstall属性用于注册vue组件

创建组件函数

创建base组件

npm run plop
# 输入组件名称得到packages/base模块

在src文件夹中创建create文件夹并创建component.ts文件用于创建组件方法。创建组件与要name属性和install方法来注册组件

/**
 * Create a basic component with common options
 */
import { App, defineComponent, ComponentOptionsWithObjectProps } from 'vue'

/**
 *
 * @description 创建组件
 * @export createComponent
 * @param {string} name
 * @return {*} defineComponent
 */
export function createComponent (name: string) {
  return function (sfc: ComponentOptionsWithObjectProps) {
    sfc.name = name
    sfc.install = (app: App) => {
      app.component(name as string, sfc)
      app.component(name), sfc)
    }

    return defineComponent(sfc)
  } as typeof defineComponent
}

因为我们组件名字可能包含多个单词所以我们把他转换为驼峰命名法所以创建src/format/string.ts文件来导出驼峰命名函数

// base/src/format/string.ts

const camelizeRE = /-(\w)/g

/**
 *
 * @description 把-换成驼峰命名
 * @export camelize
 * @param {string} str
 * @return {*}  {string}
 */
export function camelize (str: string): string {
  return str.replace(camelizeRE, (_, c) => c.toUpperCase())
}

// base/src/create/component.ts
import { camelize } from '../format/string'
// 修改这句代码来转换为驼峰命名法
app.component(camelize(`-${name}`), sfc)

创建create/bem.ts文件生成bem的函数

Bem 函数传入参数与生成的名字

  • b() // 'button'
  • b('text') // 'button__text'
  • b({ disabled }) // 'button button--disabled'
  • b('text', { disabled }) // 'button__text button__text--disabled'
  • b(['disabled', 'primary']) // 'button button--disabled button--primary'
export type Mod = string | { [key: string]: any };
export type Mods = Mod | Mod[];

function gen (name: string, mods?: Mods): string {
  if (!mods) {
    return ''
  }

  if (typeof mods === 'string') {
    return ` ${name}--${mods}`
  }

  if (Array.isArray(mods)) {
    return mods.reduce<string>((ret, item) => ret + gen(name, item), '')
  }

  return Object.keys(mods).reduce(
    (ret, key) => ret + (mods[key] ? gen(name, key) : ''),
    ''
  )
}

/**
 *
 * @description 创建BEM命名空
 * @export
 * @param {string} name
 * @return {*} string
 */
export function createBEM (name: string) {
  return function (el?: Mods, mods?: Mods): Mods {
    if (el && typeof el !== 'string') {
      mods = el
      el = ''
    }

    el = el ? `${name}__${el}` : name

    return `${el}${gen(el, mods)}`
  }
}

export type BEM = ReturnType<typeof createBEM>;

创建create/index.ts文件,这个文件导出一个函数这个函数有一个参数,这个参数就是创建组件的名字,返回一个数组,这个数组的第一项是创建组件的方法,第二项就是根据组件名字创建bem命名规则的函数

import { createBEM } from './bem'
import { createComponent } from './component'

/**
 *
 *  @description 创建命名空间
 * @export
 * @param {string} name
 * @return {*} [createComponent(name), createBEM(name)]
 */
export function createNamespace (name: string) {
  name = 'two-' + name
  return [createComponent(name), createBEM(name)] as const
}

后续的公共的东西也会提取到公共base模块中

原文地址:https://kspf.xyz/archives/142/

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 前端组件人人都在用,人人都在封装。目前已知的几个大型开源组件库也并没有给出具体的封装规范。但是根据其开放的组件样式...
    yolkpie阅读 2,196评论 0 1
  • 开始之前 构建一个组件库需要考虑哪些问题 代码结构 样式解决方案 组件需求分析和编码 自建测试用例分析和编码 代码...
    梁某人的剑阅读 1,280评论 0 0
  • 一 . Ext的组件大致可以分成三大类,即基本组件、工具栏组件、表单及元素组件。 基本组件有: xtype Cl...
    独自堆雪人阅读 2,068评论 0 0
  • 简化 Symbol 的诀窍是平衡动态元素和约束的使用。这样可以最小化设计系统中所需的Symbol数量,以便易于维护...
    iris0327阅读 6,266评论 0 7
  • 我是黑夜里大雨纷飞的人啊 1 “又到一年六月,有人笑有人哭,有人欢乐有人忧愁,有人惊喜有人失落,有的觉得收获满满有...
    陌忘宇阅读 8,594评论 28 53