創(chuàng)建base公共組件

公共模塊

1

基礎(chǔ)模塊參照了vant的思路,使用bem命名規(guī)范。先創(chuàng)建一個(gè)命名空間,這個(gè)命名空間返回創(chuàng)建組件函數(shù)與生成命名方法。在創(chuàng)建組件函數(shù)中創(chuàng)建nameinstall屬性用于注冊(cè)vue組件

創(chuàng)建組件函數(shù)

創(chuàng)建base組件

npm run plop
# 輸入組件名稱得到packages/base模塊

在src文件夾中創(chuàng)建create文件夾并創(chuàng)建component.ts文件用于創(chuàng)建組件方法。創(chuàng)建組件與要name屬性和install方法來注冊(cè)組件

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

/**
 *
 * @description 創(chuàng)建組件
 * @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
}

因?yàn)槲覀兘M件名字可能包含多個(gè)單詞所以我們把他轉(zhuǎn)換為駝峰命名法所以創(chuàng)建src/format/string.ts文件來導(dǎo)出駝峰命名函數(shù)

// 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'
// 修改這句代碼來轉(zhuǎn)換為駝峰命名法
app.component(camelize(`-${name}`), sfc)

創(chuàng)建create/bem.ts文件生成bem的函數(shù)

Bem 函數(shù)傳入?yún)?shù)與生成的名字

  • 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 創(chuàng)建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>;

創(chuàng)建create/index.ts文件,這個(gè)文件導(dǎo)出一個(gè)函數(shù)這個(gè)函數(shù)有一個(gè)參數(shù),這個(gè)參數(shù)就是創(chuàng)建組件的名字,返回一個(gè)數(shù)組,這個(gè)數(shù)組的第一項(xiàng)是創(chuàng)建組件的方法,第二項(xiàng)就是根據(jù)組件名字創(chuàng)建bem命名規(guī)則的函數(shù)

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

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

后續(xù)的公共的東西也會(huì)提取到公共base模塊中

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

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 前端組件人人都在用,人人都在封裝。目前已知的幾個(gè)大型開源組件庫也并沒有給出具體的封裝規(guī)范。但是根據(jù)其開放的組件樣式...
    yolkpie閱讀 2,359評(píng)論 0 1
  • 開始之前 構(gòu)建一個(gè)組件庫需要考慮哪些問題 代碼結(jié)構(gòu) 樣式解決方案 組件需求分析和編碼 自建測試用例分析和編碼 代碼...
    梁某人的劍閱讀 1,360評(píng)論 0 0
  • 一 . Ext的組件大致可以分成三大類,即基本組件、工具欄組件、表單及元素組件。 基本組件有: xtype Cl...
    獨(dú)自堆雪人閱讀 2,294評(píng)論 0 0
  • 簡化 Symbol 的訣竅是平衡動(dòng)態(tài)元素和約束的使用。這樣可以最小化設(shè)計(jì)系統(tǒng)中所需的Symbol數(shù)量,以便易于維護(hù)...
    iris0327閱讀 6,405評(píng)論 0 7
  • 我是黑夜里大雨紛飛的人啊 1 “又到一年六月,有人笑有人哭,有人歡樂有人憂愁,有人驚喜有人失落,有的覺得收獲滿滿有...
    陌忘宇閱讀 8,857評(píng)論 28 54

友情鏈接更多精彩內(nèi)容