vue3+ts

该代码片段展示了一个基于Vue.js的用户管理界面,包括用户列表、新建、编辑和删除功能。使用了Pinia进行状态管理,El-Table组件展示数据,并根据权限控制显示不同操作按钮。同时,实现了分页和网络请求来获取及更新用户数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

<template>

  <div class="content">

    <div class="header">

      <h3 class="title">用户列表</h3>

      <el-button v-if="isCreate" type="primary" @click="handleNewUserClick"

        >新建用户</el-button

      >

    </div>

    <div class="table">

      <el-table :data="usersList" border style="width: 100%">

        <el-table-column align="center" type="selection" width="60px" />

        <el-table-column

          align="center"

          type="index"

          label="序号"

          width="60px"

        />

        <el-table-column

          align="center"

          label="用户名"

          prop="name"

          width="150px"

        />

        <el-table-column

          align="center"

          label="真实姓名"

          prop="realname"

          width="150px"

        />

        <el-table-column

          align="center"

          label="手机号码"

          prop="cellphone"

          width="150px"

        />

        <el-table-column

          align="center"

          label="状态"

          prop="enable"

          width="100px"

        >

          <!-- 作用域插槽 -->

          <template #default="scope">

            <el-button

              size="small"

              :type="scope.row.enable ? 'primary' : 'danger'"

              plain

            >

              {{ scope.row.enable ? '启用' : '禁用' }}

            </el-button>

          </template>

        </el-table-column>

        <el-table-column align="center" label="创建时间" prop="createAt">

          <template #default="scope">

            {{ formatUTC(scope.row.createAt) }}

          </template>

        </el-table-column>

        <el-table-column align="center" label="更新时间" prop="updateAt">

          <template #default="scope">

            {{ formatUTC(scope.row.updateAt) }}

          </template>

        </el-table-column>

        <el-table-column align="center" label="操作" width="150px">

          <template #default="scope">

            <el-button

              v-if="isUpdate"

              size="small"

              icon="Edit"

              type="primary"

              text

              @click="handleEditBtnClick(scope.row)"

            >

              编辑

            </el-button>

            <el-button

              v-if="isDelete"

              size="small"

              icon="Delete"

              type="danger"

              text

              @click="handleDeleteBtnClick(scope.row.id)"

            >

              删除

            </el-button>

          </template>

        </el-table-column>

      </el-table>

    </div>

    <div class="pagination">

      <el-pagination

        v-model:current-page="currentPage"

        v-model:page-size="pageSize"

        :page-sizes="[10, 20, 30]"

        layout="total, sizes, prev, pager, next, jumper"

        :total="usersTotalCount"

        @size-change="handleSizeChange"

        @current-change="handleCurrentChange"

      />

    </div>

  </div>

</template>

<script setup lang="ts">

import { storeToRefs } from 'pinia'

import useSystemStore from '@/store/main/system/system'

import { formatUTC } from '@/utils/format'

import { ref } from 'vue'

import usePermissions from '@/hooks/usePermissions'

// 定义事件

const emit = defineEmits(['newClick', 'editClick'])

// 用户的权限判断

const isCreate = usePermissions('users:create')

const isDelete = usePermissions('users:delete')

const isUpdate = usePermissions('users:delete')

const isQuery = usePermissions('users:query')

// 1.发起action,请求usersList的数据

const systemStore = useSystemStore()

const currentPage = ref(1)

const pageSize = ref(10)

fetchUserListData()

// 2.获取usersList数据,进行展示

const { usersList, usersTotalCount } = storeToRefs(systemStore)

// 3.页码相关的逻辑

function handleSizeChange() {

  fetchUserListData()

}

function handleCurrentChange() {

  fetchUserListData()

}

// 4.定义函数, 用于发送网络请求

function fetchUserListData(formData: any = {}) {

  if (!isQuery) return

  // 1.获取offset/size

  const size = pageSize.value

  const offset = (currentPage.value - 1) * size

  const pageInfo = { size, offset }

  // 2.发起网络请求

  const queryInfo = { ...pageInfo, ...formData }

  systemStore.postUsersListAction(queryInfo)

}

// 5.删除/新建/编辑的操作

function handleDeleteBtnClick(id: number) {

  systemStore.deleteUserByIdAction(id)

}

function handleNewUserClick() {

  emit('newClick')

}

function handleEditBtnClick(itemData: any) {

  emit('editClick', itemData)

}

defineExpose({ fetchUserListData })

</script>

<style lang="less" scoped>

.content {

  margin-top: 20px;

  padding: 20px;

  background-color: #fff;

}

.header {

  display: flex;

  justify-content: space-between;

  align-items: flex-end;

  margin-bottom: 10px;

  .title {

    font-size: 22px;

  }

}

.table {

  :deep(.el-table__cell) {

    padding: 12px 0;

  }

  .el-button {

    margin-left: 0;

    padding: 5px 8px;

  }

}

.pagination {

  display: flex;

  justify-content: flex-end;

  margin-top: 10px;

}

</style>

### 项目初始化与配置 在 Vue 3 中使用 TypeScript 可以充分利用其静态类型检查和现代 JavaScript 特性,从而提高代码的可维护性和开发体验。Vue 3 的设计对 TypeScript 提供了更好的支持,使得开发者可以更方便地在项目中集成 TypeScript [^1]。 使用 Vue CLI 创建项目时,可以通过选择 TypeScript 选项来初始化一个支持 TypeScript 的 Vue 3 项目。具体命令如下: ```bash vue create my-ts-project ``` 在项目创建过程中,选择 `Manually select features`,然后勾选 `TypeScript` 选项。这样创建的项目已经集成了 TypeScript 的基本配置,可以直接使用 TypeScript 编写组件 [^2]。 如果使用 Vite 创建 Vue 3 项目,则可以通过以下命令快速创建一个支持 TypeScript 的项目: ```bash npm create vite@latest my-ts-project --template vue-ts ``` Vite 会自动配置 TypeScript 的编译环境,并生成一个基础的 Vue 3 + TypeScript 项目结构 [^3]。 ### 类型定义与组件编写 在 Vue 3 中使用 TypeScript 时,可以利用接口(`interface`)或类型别名(`type`)来定义组件的 props,从而确保传入的数据具有正确的类型。例如: ```typescript interface User { id: number; name: string; email?: string; } const props = defineProps<{ user: User; }>(); ``` 上述代码中,`defineProps` 是 Vue 3 Composition API 提供的方法,用于定义组件的 props,并通过 TypeScript 类型系统进行类型检查 [^4]。 对于 Vue 3 的组件定义,可以使用 `defineComponent` 函数来包裹组件对象,这样可以获得更好的类型推导和 IDE 支持: ```typescript import { defineComponent } from &#39;vue&#39;; export default defineComponent({ props: { title: { type: String, required: true } }, setup(props) { return () => <h1>{props.title}</h1>; } }); ``` 这种方式不仅提高了代码的可读性,还增强了组件的类型安全性 [^3]。 ### 第三方库与装饰器支持 在某些场景下,可能需要使用 `vue-property-decorator` 来支持类组件的编写方式。该库提供了 `@Component`、`@Prop` 等装饰器,可以更直观地定义组件及其属性: ```bash npm install vue-property-decorator -s ``` 安装完成后,可以在组件中使用装饰器语法: ```typescript import { Component, Prop, Vue } from &#39;vue-property-decorator&#39;; @Component export default class HelloWorld extends Vue { @Prop() private msg!: string; } ``` 这种方式更适合习惯使用类组件风格的开发者,并且能够提供良好的类型推导支持 [^5]。 ### 开发工具与 IDE 支持 TypeScript 提供了非常友好的 IDE 支持,开发者可以在编辑器中即时看到编译错误,提升编程体验。主流编辑器如 VS Code 对 TypeScript 有良好的内置支持,能够提供智能提示、代码重构等功能,极大提高了开发效率 [^4]。 此外,TypeScript 的类型定义文件(`.d.ts`)可以帮助开发者更好地理解第三方库的 API,确保类型安全。在 Vue 项目中使用 TypeScript 时,建议启用 `strict` 模式以获得更严格的类型检查,从而减少潜在的运行时错误 [^1]。 ---
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值