建立vue-admin前端页面

1、编写api->position.js

import request from '@/utils/request'

export function getPositionAll() {
  return request({
    url: '/aivisit/position/',
    method: 'get'
  })
}

export function createPosition(data) {
  return request({
    url: '/aivisit/position/',
    method: 'post',
    data
  })
}

export function updatePosition(id, data) {
  return request({
    url: `/aivisit/position/${id}/`,
    method: 'put',
    data
  })
}

export function deletePosition(id) {
  return request({
    url: `/aivisit/position/${id}/`,
    method: 'delete'
  })
}

2、编写views->demo->position.vue

<template>
  <div class="app-container">
    <div>
      <el-input
        v-model="search"
        placeholder="输入岗位名称进行搜索"
        style="width: 200px"
        class="filter-item"
        @keyup.native="handleFilter"
      />
      <el-button
        type="primary"
        icon="el-icon-plus"
        @click="handleAdd"
        v-if="checkPermission(['position_create'])"
        size="small"
        >新增</el-button
      >
    </div>
    <el-table
      v-loading="listLoading"
      :data="
        tableData.filter(
          (data) =>
            !search || data.name.toLowerCase().includes(search.toLowerCase())
        )
      "
      style="width: 100%; margin-top: 10px"
      highlight-current-row
      row-key="id"
      height="100"
      stripe
      border
      v-adaptive="{ bottomOffset: 50 }"
    >
      <el-table-column type="index" width="50" />
      <el-table-column label="岗位名称">
        <template slot-scope="scope">{{ scope.row.name }}</template>
      </el-table-column>
      <el-table-column label="创建日期">
        <template slot-scope="scope">
          <span>{{ scope.row.create_time }}</span>
        </template>
      </el-table-column>
      <el-table-column align="center" label="操作">
        <template slot-scope="scope">
          <el-button
            type="primary"
            size="small"
            icon="el-icon-edit"
            :disabled="!checkPermission(['position_update'])"
            @click="handleEdit(scope)"
          />
          <el-button
            type="danger"
            size="small"
            icon="el-icon-delete"
            :disabled="!checkPermission(['position_delete'])"
            @click="handleDelete(scope)"
          />
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :visible.sync="dialogVisible"
      :title="dialogType === 'edit' ? '编辑岗位' : '新增岗位'"
    >
      <el-form
        ref="Form"
        :model="position"
        label-width="80px"
        label-position="right"
        :rules="rule1"
      >
        <el-form-item label="名称" prop="name">
          <el-input v-model="position.name" placeholder="名称" />
        </el-form-item>
      </el-form>
      <span slot="footer">
        <el-button type="danger" @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="confirm('Form')">确认</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  getPositionAll,
  createPosition,
  deletePosition,
  updatePosition,
} from "@/api/position";
import { genTree, deepClone } from "@/utils";
import adaptive from '@/directive/el-table'
import checkPermission from "@/utils/permission";

const defaultM = {
  id: "",
  name: "",
};
export default {
  directives: { adaptive },
  data() {
    return {
      position: {
        id: "",
        name: "",
      },
      search: "",
      tableData: [],
      positionList: [],
      listLoading: true,
      dialogVisible: false,
      dialogType: "new",
      rule1: {
        name: [{ required: true, message: "请输入名称", trigger: "blur" }],
      },
    };
  },
  computed: {},
  created() {
    this.getList();
  },
  methods: {
    checkPermission,
    getList() {
      this.listLoading = true;
      getPositionAll().then((response) => {
        console.log(response)
        this.positionList = response.data;
        this.tableData = response.data;
        this.listLoading = false;
      }).catch(err => {
        // 报错
        console.log(err)
      })
      ;
    },
    resetFilter() {
      this.getList();
    },
    handleFilter() {
      const newData = this.positionList.filter(
        (data) =>
          !this.search ||
          data.name.toLowerCase().includes(this.search.toLowerCase())
      );
      this.tableData = genTree(newData);
    },
    handleAdd() {
      this.position = Object.assign({}, defaultM);
      this.dialogType = "new";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },
    handleEdit(scope) {
      this.position = Object.assign({}, scope.row); // copy obj
      this.dialogType = "edit";
      this.dialogVisible = true;
      this.$nextTick(() => {
        this.$refs["Form"].clearValidate();
      });
    },
    handleDelete(scope) {
      this.$confirm("确认删除?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "error",
      })
        .then(async () => {
          await deletePosition(scope.row.id);
          this.getList();
          this.$message({
            type: "success",
            message: "成功删除!",
          });
        })
        .catch((err) => {
          console.error(err);
        });
    },
    async confirm(form) {
      this.$refs[form].validate((valid) => {
        if (valid) {
          const isEdit = this.dialogType === "edit";
          if (isEdit) {
            updatePosition(this.position.id, this.position).then(() => {
              this.getList();
              this.dialogVisible = false;
              this.$message({
                message: "编辑成功",
                type: "success",
              });
            });
          } else {
            createPosition(this.position).then((res) => {
              // this.position = res.data
              // this.tableData.unshift(this.position)
              this.getList();
              this.dialogVisible = false;
              this.$message({
                message: "新增成功",
                type: "success",
              });
            });
          }
        } else {
          return false;
        }
      });
    },
  },
};
</script>

3、加菜单router->index.js

  {
    path: '/position',
    component: Layout,
    redirect: '/demo/position',
    children: [
      {
        path: 'index',
        component: () => import('@/views/demo/position'),
        name: 'Position',
        meta: { title: '岗位管理', icon: 'guide', noCache: true }
      }
    ]
  },

4、配置服务端.env.development

# just a flag
ENV = 'development'

# base api
VUE_APP_BASE_API = 'http://127.0.0.1:8000/api'

4、 遇到的坑:页面获取不到json

utils->request.js

    // if the custom code is not 20000, it is judged as an error.
    if (res.code !== 20000 && res.code !== 0 && res.code !== 200) {  //原来只有 res.code !== 20000
      Message({
        message: res.message || 'Error',
        type: 'error',
        duration: 5 * 1000
      })

5、遇到的坑:没有实现表格页面缩放

position.vue


import adaptive from '@/directive/el-table'
export default {
  directives: { adaptive },
  data() {
	....
  }
}

    <el-table
      v-loading="listLoading"
      :data="
        tableData.filter(
          (data) =>
            !search || data.name.toLowerCase().includes(search.toLowerCase())
        )
      "
      style="width: 100%; margin-top: 10px"
      highlight-current-row
      row-key="id"
      height="100"
      stripe
      border
      v-adaptive="{ bottomOffset: 50 }"  //原代码有错,应该是v-adaptive
    >
### 关于 `vue-admin-template` 和黑马程序员教程 对于希望基于 `vue-admin-template` 进行项目开发并寻求相关资源的情况,可以从以下几个方面入手: #### 使用 vue-admin-template 创建项目基础结构 为了创建一个新的管理后台模板项目而不直接修改现有的 `vue-admin-template` 项目,可以选择按照如下方式操作[^2]: ```bash vue create my-admin-template ``` 这会初始化一个全新的 Vue.js 工程,在此基础上逐步引入所需的功能模块。 #### 自定义路由设置 当新项目建立完毕之后,可以根据实际需求调整路由配置。例如,在 (`***src/***/router/index.js`) 文件中移除不必要的默认页面如表单(form)、表格(table),只保留必要的核心路径[^3]: ```javascript // src/router/index.js import Vue from 'vue'; import Router from 'vue-router'; Vue.use(Router); export default new Router({ routes: [ { path: '/', name: 'home', component: () => import('@/views/Home') } // 只留下必需的核心路由... ] }); ``` #### 构建与部署准备 完成上述工作后,为了让应用程序能够在生产环境中正常运行,还需要适当调整项目的构建脚本以及服务端配置。具体来说就是编辑位于根目录下的 `vue.config.js` 文件来指定输出路径和其他选项[^4]: ```javascript module.exports = { publicPath: './', // 设置相对路径作为公共前缀 outputDir: 'dist', // 输出文件夹名称设为 dist assetsDir: 'static', // 将静态资源放入 static 子文件夹 lintOnSave: false, // 不启用保存时的 ESLint 检查 }; ``` 至于打包发布,则可以通过执行以下命令生成适合上线使用的版本,并将其放置到 Web 服务器(比如 Apache 或 Nginx)对应的文档根目录下: ```bash npm run build:prod ``` #### 获取更多学习资料 关于如何更深入地理解和运用这些技术栈,推荐参考一些高质量的教学视频或书籍,特别是那些专注于前端框架及其生态系统的内容。虽然这里提到的是“黑马程序员”,但实际上并没有特定针对此培训机构提供的官方教材链接;不过网络上有许多开源社区贡献者分享的经验总结和技术博客文章可供借鉴。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值