• Vue尚品汇后台系统实现三级联动平台属性及解决400报错


    需求:

    1、平台属性有三级分类,我们应该保证点击一级分类后才能获取到二级分类的数据,确认了二级分类后才能获取到三级分类的数据。

    2、并且修改完前一级的分类后,后面的分类应该被重置。

    3、直到选择了三级分类后再向服务器发请求,获取该分类下的所有平台属性。

    需求1:

    首先该组件一挂载完毕,先获得一级分类的数据。然后对一级分类绑定change事件,若发生变化,进行事件的回调

    1. async handler1(){
    2. // 解构出一级分类的id
    3. const {category1Id} = this.cForm;
    4. this.$emit('getCategoryId',{categoryId:category1Id,level:1});
    5. // 通过一级分类的id,获取二级分类的数据
    6. let result = await this.$API.attr.reqCategory2List(category1Id);
    7. if(result.code==200){
    8. this.list2 = result.data;
    9. }
    10. },

    对于二级分类也同样处理。 通过相应的分类ID再获取下一级的分类数据

    需求2:

    1. async handler1(){
    2. // 清除数据
    3. this.list2 = [];
    4. this.list3 = [];
    5. this.cForm.category2Id = '';
    6. this.cForm.category3Id = '';
    7. // 解构出一级分类的id
    8. const {category1Id} = this.cForm;
    9. this.$emit('getCategoryId',{categoryId:category1Id,level:1});
    10. // 通过一级分类的id,获取二级分类的数据
    11. let result = await this.$API.attr.reqCategory2List(category1Id);
    12. if(result.code==200){
    13. this.list2 = result.data;
    14. }
    15. },

     在每一次级层数据发生改变时,清空后面几级的数据。 同时传递数据给父组件告知该级别的数据信息(id和层级)

    需求3:

    1. // 一级分类的select事件回调(当一级分类的option发生变化时,获取相应二级分类的数据)
    2. async handler1(){
    3. // 清除数据
    4. this.list2 = [];
    5. this.list3 = [];
    6. this.cForm.category2Id = '';
    7. this.cForm.category3Id = '';
    8. // 解构出一级分类的id
    9. const {category1Id} = this.cForm;
    10. this.$emit('getCategoryId',{categoryId:category1Id,level:1});
    11. // 通过一级分类的id,获取二级分类的数据
    12. let result = await this.$API.attr.reqCategory2List(category1Id);
    13. if(result.code==200){
    14. this.list2 = result.data;
    15. }
    16. },
    17. // 二级分类的select事件回调
    18. async handler2(){
    19. // 清除数据
    20. this.list3 = [];
    21. this.cForm.category3Id = '';
    22. // 解构出二级分类的id
    23. const {category2Id} = this.cForm;
    24. this.$emit('getCategoryId',{categoryId:category2Id,level:2});
    25. // 通过一级分类的id,获取二级分类的数据
    26. let result = await this.$API.attr.reqCategory3List(category2Id);
    27. if(result.code==200){
    28. this.list3 = result.data;
    29. }
    30. },
    31. // 三级分类的select事件回调
    32. handler3(){
    33. // 解构出三级分类的id
    34. const {category3Id} = this.cForm;
    35. this.$emit('getCategoryId',{categoryId:category3Id,level:3});
    36. },

    同样的,分别对1、2、3级分类做处理,改变该级的分类,向父组件传递相应数据。

    父组件.vue

    1. <template>
    2. <div>
    3. <el-card style="margin:20px 0px">
    4. <CategorySelect @getCategoryId="getCategoryId">CategorySelect>
    5. el-card>
    6. div>
    7. template>
    8. <script>
    9. data(){
    10. return {
    11. category1Id:'',
    12. category2Id:'',
    13. category3Id:'',
    14. }
    15. },
    16. methods:{
    17. // 自定义事件回调
    18. getCategoryId(categoryId,level){
    19. if(level==1){
    20. this.category1Id = categoryId;
    21. this.category2Id = '';
    22. this.category3Id = '';
    23. }else if(level == 2){
    24. this.category2Id = categoryId;
    25. this.category3Id = '';
    26. }else{
    27. this.category3Id = categoryId;
    28. // 发请求,获取平台属性
    29. this.getAttrList();
    30. }
    31. },
    32. // 获取平台属性的数据
    33. async getAttrList(){
    34. // 当用户确定三级分类的数据时候,可以向服务器发请求获取平台属性进行展示
    35. // 获取分类的ID
    36. const {category1Id,category2Id,category3Id} = this;
    37. let result = await this.$API.attr.reqAttrList(category1Id,category2Id,category3Id);
    38. console.log(result);
    39. }
    40. }
    41. script>

    这里就是通过每一次传递过来的数据,更新父组件相应data,当第三级分类改变时,发送请求给服务器以获得相应层级路径下的平台属性。 (上面代码有bug,导致了400,勿直接复制)

     然后就发现,它报错了。400

    看到这种就会很烦,因为要找bug。这里就记录下排错过程吧。

    首先想到接口不能用了? 但是也不是,因为获取三级联动的接口是没问题的,正常返回数据了。

    然后又检查了api是不是写错了,发现也没问题。

    最后再检查处理的数据对不对,父子组件通信时有没有问题。

    这里其实还没开始检查就发现了一点问题,就是上图中我只点了一级分类,但是直接给我返回了400。按照业务的逻辑,应该是三级分类全部完成以后,才发送请求的。所以我初步判定就是父组件里面写的有些问题。

    1. methods:{
    2. // 自定义事件回调
    3. getCategoryId(categoryId,level){
    4. console.log(categoryId,level);
    5. if(level==1){
    6. this.category1Id = categoryId;
    7. this.category2Id = '';
    8. this.category3Id = '';
    9. console.log('一级分类');
    10. }else if(level == 2){
    11. this.category2Id = categoryId;
    12. this.category3Id = '';
    13. }else{
    14. this.category3Id = categoryId;
    15. console.log("发请求!");
    16. // 发请求,获取平台属性
    17. this.getAttrList();
    18. }
    19. },

     首先,进行了三处地方的打印。

    这里就发现不对劲了,首先没进入到第一层的if判断中去,其次level也没获取到。立马想到是父子通信时写错了。

     

    子组件传递数据时,是传的对象形式,而父组件用了两个参数来接收。 所以第一个参数获取到的是对象,第二个参数是undefined。 所以应该直接解构一下 

     

    这样子就没问题了。

  • 相关阅读:
    DAOS整体设计分析 (二)
    黑马JVM总结(二十九)
    多目标优化算法:多目标霸王龙优化算法(MOTROA)MATLAB
    Linux知识结构体系简述
    全面升级 | 两行代码,轻松搞定登录系统
    Java程序是如何执行的
    UE AIPerception感知非Pawn对象
    ubuntu提高 github下载速度
    8年经验之谈 —— 如何使用自动化工具编写测试用例?
    spring框架(SSM)
  • 原文地址:https://blog.csdn.net/m0_56698268/article/details/126965742