✅P51_三级分类-删除-删除效果优化

gong_yz大约 4 分钟谷粒商城

编写分类remove方法

模拟role.vue发起post请求,category.vueremove方法如下:

remove(node, data) {
  var ids = [data.catId]
  this.$http({
    url: this.$http.adornUrl("/product/category/delete"),
    method: "post",
    data:this.$http.adornData(ids,false)
  }).then(({ data }) => {
    console.log("remove", node, data);
  });
},

经过删除测试发现,页面数据没有主动刷新,新增获取分类方法:this.getMenuList();

remove(node, data) {
  var ids = [data.catId]
  this.$http({
    url: this.$http.adornUrl("/product/category/delete"),
    method: "post",
    data:this.$http.adornData(ids,false)
  }).then(({ data }) => {
    console.log("remove", node, data);
    this.getMenuList();
  });
},

增加删除提示

参考:

删除提示:

    remove(node, data) {
      var ids = [data.catId];
      // 反引号是新标准es6的功能,可以用$配合大括号来实现变量拼接
      this.$confirm(`是否删除【${data.name}】菜单?`, "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/product/category/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getMenuList();
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
      console.log("remove", node, data);
    }

此时删除分类后,所有分类数据会收缩为一级分类。要实现删除子级分类后,分类依然展开,进行如下操作:

参考:

category.vue

this.expandKey = [node.parent.data.catId];数据来源:


效果展示

确认删除

删除成功提示


category.vue 代码

<!-- 三级分类页面 -->
<template>
  <div>
    <el-tree :data="menu" :props="defaultProps" show-checkbox :expand-on-click-node="false" node-key="catId"
      :default-expanded-keys="expandKey">
      <span class="custom-tree-node" slot-scope="{ node, data }">
        <span>{{ node.label }}</span>
        <span>
          <!-- 一级和二级目录才可以添加 -->
          <el-button v-if="node.level <= 2" type="text" size="mini" @click="() => append(data)">
            Append
          </el-button>
          <!-- 如果没有子节点才可以进行删除 -->
          <el-button v-if="node.childNodes.length == 0" type="text" size="mini" @click="() => remove(node, data)">
            Delete
          </el-button>
        </span>
      </span>
    </el-tree>
  </div>
</template>

<script>
//这里可以导入其他文件(比如:组件,工具js,第三方插件js,json文件,图片文件等等)
//例如:import 《组件名称》 from '《组件路径》';

export default {

  data() {
    //这里存放数据
    return {
      menu: [],
      expandKey: [],
      defaultProps: {
        children: "children",
        label: "name"
      }
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {

    getMenuList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl('/product/category/list/tree'),
        method: 'get',
      }).then(({ data }) => {  // 1.使用解构即{},将data对象解构出来
        console.log("成功获取到三级分类数据", data);
        // 2.将data中的data赋值给menu
        this.menu = data.data;
      });
    },

    append(data) {
      console.log("append", data);
    },

    remove(node, data) {
      var ids = [data.catId];
      // 反引号是新标准es6的功能,可以用$配合大括号来实现变量拼接
      this.$confirm(`是否删除【${data.name}】菜单?`, "提示", {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl("/product/category/delete"),
          method: "post",
          data: this.$http.adornData(ids, false)
        }).then(({ data }) => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          this.getMenuList();
          //默认菜单展示
          this.expandKey = [node.parent.data.catId];
        });
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
      console.log("remove", node, data);
    }
  },
  //生命周期 - 创建完成(可以访问当前this实例)
  created() {
    this.getMenuList();
  },
}
</script>
<style  scoped></style>

附:Vue模板增加post请求和get请求

post请求和get请求可以抽取出来进行封装,将以下片段放入模板中("文件"->"首选项"->"用户片段"),

"http-get 请求": {
		"prefix": "httpget",
		"body": [
			"this.\\$http({",
			"url: this.\\$http.adornUrl(''),",
			"method: 'get',",
			"params: this.\\$http.adornParams({})",
			"}).then(({data}) => {",
			"})"
		],
		"description": "httpGET 请求"
	},
	"http-post 请求": {
		"prefix": "httppost",
		"body": [
			"this.\\$http({",
			"url: this.\\$http.adornUrl(''),",
			"method: 'post',",
			"data: this.\\$http.adornData(data, false)",
			"}).then(({ data }) => { });"
		],
		"description": "httpPOST 请求"
	}

httpget/httppost 回车生成代码片段。

	remove(node, data) {
      var ids = [data.catId];
      this.$http({
        url: this.$http.adornUrl("/product/category/delete"),
        method: "post",
        data: this.$http.adornData(ids, false),
      }).then(({ data }) => {
        console.log("删除成功!");
      });
      console.log("remove", node, data);
    },
  },