3.1.12 • Published 4 months ago

v3-usehook v3.1.12

Weekly downloads
-
License
ISC
Repository
github
Last release
4 months ago

vue3-hook

createUseTable

接收一个IUseTableOption类型参数

参数名是否必填默认值描述
comoponentnull组件库table组件
reqnull全局请求相关配置
req.paramsnull请求相关额外参数
req.reNamenull重写分页请求当前页和条数名称
req.reName.index'index'分页请求当前页名称
req.reName.size'size'分页请求当前条数限制名称
res全局响应相关配置
res.reNamenull分页接口响应数据
res.reName.total'total'根据res.reName.total获取请求返回的总数数据
res.reName.list'data'根据res.reName.list获取请求返回的列表数据

createUseTable返回IUserTableReturn类型接口

参数名描述
UseTableComponent返回的TableComponent,在组件内使用
search根据关键字搜索
reload重制表格数据
dataSource列表数据源

TableTemplate.vue 当前使用的组件库的表格组件

<template>
  <div>
    <el-table
      :data="tableData"
      bodyClass="leading-none"
      v-loading="loading"
     v-bind="attrs"
    >
      <el-table-column
        v-for="col in columns"
        :prop="col.prop"
        :label="col.label"
        :key="col.prop"
        :type="col.type"
      >
        <template #default="scope">
          <!-- 普通列 -->
          <slot
            v-if="!col.type"
            :name="col.slot"
            :data="{ text: scope.row[col.prop], raw: scope.row }"
            ><span class="leading-none">{{
              col.renderText
                ? col.renderText(scope.row[col.prop], scope.row)
                : scope.row[col.prop] || '-'
            }}</span>
          </slot>
          <!-- 状态列 -->
          <template v-if="col.type === 'enum'">
            <Tag
              v-if="col.enum[scope.row[col.prop]]?.text"
              :value="col.enum[scope.row[col.prop]]?.text"
              :icon="`pi ${col.enum[scope.row[col.prop]]?.icon}`"
              :severity="col.enum[scope.row[col.prop]]?.status"
            ></Tag>

            <span v-else>-</span>
          </template>

          <!-- 操作列 -->
          <div v-if="col.type === 'action'">
            <template v-for="(item, index) in actions!.actions(scope.row)" :key="index">
              <el-button
                v-bind="item"

              >
                {{ item.label }}
              </el-button>
            </template>
          </div>
        </template>
      </el-table-column>
      <template #empty> 暂无数据~ </template>
    </el-table>
    <el-pagination
      v-model:current-page="pageInfo.index"
      v-model:page-size="pageInfo.size"
      :page-sizes="[2, 10, 20, 30, 40]"
      layout="total, sizes, prev, pager, next, jumper"
      :total="pageInfo.total"
      @size-change="onSizeChange"
      @current-change="onCurrentChange"
    />
  </div>
</template>

<script setup lang="ts">
import { useInject } from 'v3-usehook';
import { computed } from 'vue'


// ## 注入
let {
  columns, // 表格展示列数据
  loading, //  异步数据加载状态
  pageInfo, // 当前分页数据
  tableData,// 表格数据源
  handleSizeChange, // 分页大小change事件
  handlePageChange, // 分页当前页change事件
  handleActionButtonClick, // 操作列按钮事件
  attrs // 表格参数属性
} = useInject()


// 获取操作列按钮列表
const actions = computed(() => columns.find((v: any) => v.type === 'action'))

const onSizeChange = (val: number) => {
  handleSizeChange(val)
}
const onCurrentChange = (val: number) => {
  handlePageChange(val)
}

</script>

<style lang="less" scoped>
span {
  vertical-align: auto;
}
.p-tag-value {
  padding: 3px 7px;
}
.p-button.p-button-sm {
  padding: 7px 15px;
}
</style>

声明table.js文件

// 创建一个useTable方法,传入TableTemplate目标
export const useTable = createUseTable(TableTemplate)

业务组件内使用

<template>
  <UseTableComponent />
</template>

<script setup>
const columns: IColumns[] = [
  { type: 'selection', prop: 'selection' },
  { type: 'index', prop: 'index', label: '序号' },
  { prop: 'name', label: '姓名' },
  { prop: 'mobile', label: '联系方式' },
  {
    label: '操作',
    prop: 'action',
    type: 'action',
    actions: (record, reload) => [
      {
        label: '操作',
        onClick() {
          // record 当前行数据
          // reload 刷新表格
        }
      }
    ]
  }
]
let {UseTableComponent} = useTable({
  requestApi: API // 列表接口
  columns
})
</script>

useTable(params: IUseTableParams, attrs?: any, options?: IUseTableOption)

1、 IUseTableParams<T = any> | 参数名 | 类型 | 描述 | |---------|-----|--------| | requestApi | Promise |一步请求API | |dataSource | any[]| 静态表格数据源 | | columns | IColumns[] | 表格展示列 |

2、 attrs:表格参数

3、 options: 同上 IUseTableOption

IColumns

参数名是否必填类型描述
label表格列名
prop表格对应列绑定的列表数据字段参数,根据prop获取当前行的对应数据
hideInTableboolean是否隐藏该列
type"index" ,"checkbox" ,"action" ,"enum";表格列展示类型
emun状态列请看示例
actions操作列,请看示例
slotstring插槽名称,可以生成对应具名插槽
renderText(text,record) => any格式化当前参数展示数据,text:为当前数据,record:为当前行数据
render(text,record) => Component同上

createUseModal (v3.0已废弃)

createUseModal: (component:Component) => (content:Copmonent,args:any) =>Promise 该方法传入一个组件,返回一个useModal函数,使用该函数创建弹窗,并在改函数内,传入弹窗内的组件

用法

1、ModalTemplate

<template>
  <el-dialog
    v-model="visible"
    title="Tips"
    width="30%"
    :before-close="handleClose"

<template #footer> <el-button @click="visible = false">Cancel <el-button type="primary" @click="visible = false"> Confirm

#### 某js文件导出函数

```javascript
export const useModal = createUseModal(ModalTemplate)

业务组件内引入

useModal(AddUserForm,{name:'张三',mobile:'18888888888'}).then(res => {

})

AddUserForm组件内

<template>
 <p>name:{{args.name}} mobile: {{args.mobile}}</p>
<template>

<script>
const {args, close} = useInject<IModalInject>()
//args useModal方法中第二个传入的数据
// close 用于关闭弹窗可以传入数据,用于触发useModal的then,并且会携带数据返回
</script>

IModalInject

参数名类型描述
visibleboolean弹窗的显示隐藏
close(data:any) => any关闭弹窗并且传递数据
contentComponent内容组件

createUseModalComponent创建弹窗(v3.0已废弃)

由于createUseModal 中的所有组件无法读取全局组件需要手动引入,因此新增了createUseModalComponent支持使用全局组件

createUseModalComponent: (component:Component) => (content:Component,args:any) => {open, UseDialogComponent}

createUseModalComponent用法同上 (v3.0已废弃)

createUseModalComponent 与 createUseModal 区别

createUseModalComponent 创建成功后 返回 useModalComponent 业务组件内使用区别如下

业务组件内

<template>
  <UseModalComponent />
</template>

<script>
  const {open,UseModalComponent} = useModalComponent(AddUseForm,{name:'zs'})
  open().then(res => {
    // res close中传递的数据
  })
</script>

模态框、抽屉函数创建(v3.0)

准备ModalTemplate模板

<template>
  <n-modal v-model:show="visible" transform-origin="mouse">
    <n-card
      :title="args && args.title"
      :bordered="false"
      size="huge"
      role="dialog"
      aria-modal="true"
    >
      <component :is="content"></component>
      <template #footer v-if="args && !args.hideFooter">
        <NSpace class="text-right" justify="end">
          <n-button @click="onConfirm" :loading="loading" type="primary">确定</n-button
          ><n-button @click="handleClose">取消</n-button>
        </NSpace>
      </template>
    </n-card>
  </n-modal>
</template>

<script setup lang="ts">
import { NModal, NCard, NButton, NSpace } from 'naive-ui'
import { useInject, type IModalInject } from 'v3-usehook'
/**
 * visible:控制显示隐藏
 * args:自定义参数
 * content:引入的内容组件
 * close:关闭事件
 * loading:确认按钮loading动画
 * onOk:接管点击确认事件
 */
const { visible, args, content, close, loading, onOk } = useInject<IModalInject>()
function handleClose() {
  close(false)
}
function onConfirm(){
  onOk()
}
</script>

步骤一:注入组件

<script setup>
  import { CustomPopupProvide } from 'v3-usehook'
  // 1、在App.vue文件中引入CustomPopupProvide组件
  // 2、传入模态框模板以及抽屉模板
</script>

<template>
 <CustomPopupProvide :modal-template="ModalTemplate" :drawer-template="DrawerTemplate">
  <App />
 </CustomPopupProvide>
</tempalte

步骤二:使用组件

<script>
> comp:传入的组件、args:自定义参数
> uesModal(comp:component | VNode, args:{[propName:string]:any}): Promise<any>
  // 用法一:
  const { useModal, useDrawer } = usePopup()
   function open(){
  // 用法二:
  const modal = useModal()
  // 用法一:
    useModal(/**组件 */,{title:'标题',...args},).then(res => {})
  // 用法二:
    modal(/**组件 */,{title:'标题',...args},).then(res => {})
   }
</script>

<template>
 <button @click="open">open</button>
</tempalte

步骤三:传入的组件内获取依赖注入

<script>
  const {args, close,onOk} = useInject<IModalInject>()
</script>

<template>
 
</tempalte

useInject

3.1.12

4 months ago

3.1.11

8 months ago

3.1.10

8 months ago

2.0.2

11 months ago

3.1.9

8 months ago

3.1.8

8 months ago

2.0.0

11 months ago

3.0.3

9 months ago

3.0.2

9 months ago

3.0.1

9 months ago

3.0.0

9 months ago

1.3.26

12 months ago

3.1.3

8 months ago

3.1.2

8 months ago

3.1.1

8 months ago

3.1.0

8 months ago

3.1.7

8 months ago

3.1.6

8 months ago

3.1.5

8 months ago

3.1.4

8 months ago

3.0.0-0

9 months ago

1.3.25

12 months ago

1.3.24

12 months ago

1.3.23

12 months ago

1.3.22

12 months ago

1.3.21

12 months ago

1.3.20

1 year ago

1.3.17

1 year ago

1.3.16

1 year ago

1.3.15

1 year ago

1.3.14

1 year ago

1.3.13

1 year ago

1.3.12

1 year ago

1.3.11

1 year ago

1.3.10

1 year ago

1.3.9

1 year ago

1.3.8

1 year ago

1.3.7

1 year ago

1.3.6

1 year ago

1.3.5

1 year ago

1.3.3

1 year ago

1.3.2

1 year ago

1.3.1

1 year ago

1.3.0

1 year ago

1.2.11

1 year ago

1.2.10

1 year ago

1.2.9

1 year ago

1.2.8

1 year ago

1.2.7

1 year ago

1.2.6

1 year ago

1.2.5

1 year ago

1.2.4

1 year ago

1.2.3

1 year ago

1.2.2

1 year ago

1.2.1

1 year ago

1.2.0

1 year ago

1.1.6

1 year ago

1.1.4

1 year ago

1.1.3

1 year ago

1.1.2

1 year ago

1.1.1

1 year ago

1.1.0

1 year ago

1.0.0

1 year ago