Skip to content

引入

ts
import {
  addEmitsForComponent,
  addNameForComponent,
  getFullCompName,
  getPureCompName,
  extractClass,
  extractEvent,
  extractProps
} from 't-comm';

// 不支持 tree-shaking 的项目
import {
  addEmitsForComponent,
  addNameForComponent,
  getFullCompName,
  getPureCompName,
  extractClass,
  extractEvent,
  extractProps
} from 't-comm/lib/component/index';

// 只支持 ESM 的项目
import {
  addEmitsForComponent,
  addNameForComponent,
  getFullCompName,
  getPureCompName,
  extractClass,
  extractEvent,
  extractProps
} from 't-comm/es/component/index';

addEmitsForComponent(filePath, [fileContent])

描述:为 Vue 组件添加 emits 属性

参数

参数名类型描述
filePathstring组件地址
[fileContent]string组件内容

返回: string

新的组件内容

示例

ts
addNameForComponent('xxx.vue');

addNameForComponent(filePath, componentName)

描述:为 Vue 组件添加、修正 name 属性

参数

参数名类型描述
filePathstring组件地址
componentNamestring组件名称

返回: string

新的组件内容

示例

ts
addNameForComponent('xxx.vue', 'PressUploader');

getFullCompName(name, prefix)

描述:获取组件全称

参数

参数名描述
name组件名称
prefix前缀

返回: 全称

示例

ts
getFullCompName('swiper-item', 'press-')
getFullCompName('press-swiper-item', 'press-')

// press-swiper-item

getPureCompName(name, prefix)

描述:获取组件简称

参数

参数名描述
name组件名称
prefix前缀

返回: 简称

示例

ts
getPureCompName('press-swiper-item', 'press-')
getPureCompName('swiper-item', 'press-') // swiper-item

extractClass(params)

描述:提取 Vue 组件的 class

参数

参数名类型描述
paramsobj参数
params.filePathstring源文件地址
[params.targetFilePath]string输出文件地址
[params.extractRegexp]Regexp提取正则

示例

ts
extractClass({
  filePath: 'xxx.vue',
})

extractEvent(params)

描述:提取 Vue 组件的 event

参数

参数名类型描述
paramsobj参数
params.filePathstring源文件地址
[params.targetFilePath]string输出文件地址
[params.extractRegexp]Regexp提取正则

示例

ts
extractEvent({
  filePath: 'xxx.vue',
})

extractProps(params)

描述:提取 Vue 组件的 props

参数

参数名类型描述
paramsobj参数
params.filePathstring源文件地址
[params.targetFilePath]string输出文件地址
[params.extractRegexp]Regexp提取正则

示例

ts
extractProps({
  filePath: 'xxx.vue',
})