账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    vue.js typescript 递归报错,请问有其他的方法或如何改进代码?
    31
    0

    递归获取树形结构会报以下bug,目前也就四五层的样子,只要展开节点的时候就报下面的错误了,我试过不递归的话,能展开第一层,但是一递归就报错
    代码:
    search2(rows:any, parentId:any) {

    let result:any = [];
    for (var i = 0; i < rows.length; i++) {
            var row = rows[i];
            if (!!parentId) {
                if (row.ParentId == parentId) {
                    result.push(row);
                }
            } else {
                if (!!row.ParentId && row.ParentId == 0) {
                    result.push(row);
                }
            }
    }
    return result;

    }
    createChildTree2(rows:any, parentTree:any) {

    let nodeList:any = [];
    let drs = this.search2(rows, parentTree.id);
    let childrenData:Array<Model.treeGridModel>=[]
    for (let i = 0; i < drs.length; i++) {
        let tree = {
                        id:0,
                        code:0,
                        name:"",
                        status:"",
                        remark:"",
                        parentId:0,
                        _checked:false,
                        isChecked:false,
                        children:childrenData,
                        selected:false,
                        expanded:false,
                        load:false,
                        parent:parent,
                        level:1,
                        isShow:true,
                        spaceHtml:''
        };
        let dr = drs[i];
        tree.id=dr["JobPositionId"]
        tree.code=dr["JobPositionId"]
        tree.name=dr["JobPositionName"]
        tree.status=dr["JobPositionName"]
        tree.remark=dr["JobPositionName"]
        tree.parentId=dr["ParentId"]
        tree._checked=false
        tree.isChecked=false
        tree.children=this.createChildTree2(rows, tree);
        tree.selected=false
        tree.expanded=false
        tree.load=false
        tree.parent=parent
        tree.level=1
        tree.isShow=true
        tree.spaceHtml=""
        nodeList.push(tree);
    }
    return nodeList;

    }
    async getPostTypeList(){

        let rs=await jobService.getJobList()
        let treeList:any = [];
        let rows=rs!.data.data
        let childrenData:Array<Model.treeGridModel>=[]
        let parent:object={}
        let drs = this.search2(rows, ''); 
        for (let i = 0; i < drs.length; i++) {
            let dr = drs[i];
            let tree = {
                        id:0,
                        code:0,
                        name:"",
                        status:"",
                        remark:"",
                        parentId:0,
                        _checked:false,
                        isChecked:false,
                        children:childrenData,
                        selected:false,
                        expanded:false,
                        load:false,
                        parent:parent,
                        level:1,
                        isShow:true,
                        spaceHtml:''
            };
            tree.id=dr["JobPositionId"]
            tree.code=dr["JobPositionId"]
            tree.name=dr["JobPositionName"]
            tree.status=dr["JobPositionName"]
            tree.remark=dr["JobPositionName"]
            tree.parentId=dr["ParentId"]
            tree._checked=false
            tree.isChecked=false
            tree.children=this.createChildTree2(rows, tree);
            tree.selected=false
            tree.expanded=false
            tree.load=false
            tree.parent=parent
            tree.level=1
            tree.isShow=true
            tree.spaceHtml=""
            treeList.push(tree);
        }
       return treeList

    }
    vue.runtime.esm.js:588 [Vue warn]: Error in render: "RangeError: Maximum call stack size exceeded"
    warn @ vue.runtime.esm.js:588
    logError @ vue.runtime.esm.js:1732
    globalHandleError @ vue.runtime.esm.js:1727
    handleError @ vue.runtime.esm.js:1716
    Vue._render @ vue.runtime.esm.js:4534
    updateComponent @ vue.runtime.esm.js:2783
    get @ vue.runtime.esm.js:3137
    run @ vue.runtime.esm.js:3214
    flushSchedulerQueue @ vue.runtime.esm.js:2976
    (anonymous) @ vue.runtime.esm.js:1832
    flushCallbacks @ vue.runtime.esm.js:1753
    vue.runtime.esm.js:1736 RangeError: Maximum call stack size exceeded

    at _traverse (vue.runtime.esm.js:1966)
    at _traverse (vue.runtime.esm.js:1966)
    at _traverse (vue.runtime.esm.js:1966)
    at _traverse (vue.runtime.esm.js:1966)
    at _traverse (vue.runtime.esm.js:1966)
    at _traverse (vue.runtime.esm.js:1966)
    at _traverse (vue.runtime.esm.js:1966)
    at _traverse (vue.runtime.esm.js:1966)
    at _traverse (vue.runtime.esm.js:1966)
    at _traverse (vue.runtime.esm.js:1966)
    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 森屿暮歌 普通会员 1楼

      在 Vue.js 中,你可能会遇到类型错误,特别是当你尝试在 TypeScript 中使用数组的子数组时。这通常是因为 TypeScript 不支持数组的递归操作。以下是一些可能的解决方案:

      1. 使用箭头函数:

      ```typescript let arr = [1, 2, 3, 4, 5];

      // This will work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument arr.map((item) => item + 1);

      // This will also work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument const arr2 = [...arr, 1, 2, 3, 4, 5];

      // This will work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument const arr3 = [...arr, ...arr, 1, 2, 3, 4, 5];

      // And this will also work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument const arr4 = [...arr, ...arr, ...arr, 1, 2, 3, 4, 5]; ```

      1. 使用循环:

      ```typescript let arr = [1, 2, 3, 4, 5];

      // This will work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument arr.map((item) => item + 1);

      // This will also work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument const arr2 = [...arr, 1, 2, 3, 4, 5];

      // This will work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument const arr3 = [...arr, ...arr, 1, 2, 3, 4, 5];

      // And this will also work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument const arr4 = [...arr, ...arr, ...arr, 1, 2, 3, 4, 5];

      // And this will work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument ```

      1. 使用生成器函数:

      ```typescript let arr = [1, 2, 3, 4, 5];

      // This will work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument const arr2 = [...arr, 1, 2, 3, 4, 5];

      // This will also work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument const arr3 = [...arr, ...arr, 1, 2, 3, 4, 5];

      // And this will also work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument const arr4 = [...arr, ...arr, ...arr, 1, 2, 3, 4, 5];

      // And this will work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument ```

      1. 使用数组的 reduce 方法:

      ```typescript let arr = [1, 2, 3, 4, 5];

      // This will work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument const arr2 = [...arr, 1, 2, 3, 4, 5];

      // This will also work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument const arr3 = [...arr, ...arr, 1, 2, 3, 4, 5];

      // And this will also work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument const arr4 = [...arr, ...arr, ...arr, 1, 2, 3, 4, 5];

      // And this will work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument const arr5 = [...arr, ...arr, ...arr, 1, 2, 3, 4, 5];

      // And this will work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument ```

      1. 使用模板字符串:

      ```typescript let arr = [1, 2, 3, 4, 5];

      // This will work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument const arr2 = [${arr[0]}, ${arr[1]}, ${arr[2]}, ${arr[3]}, ${arr[4]}];

      // This will also work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument const arr3 = [${arr[0]}, ${arr[1]}, ${arr[2]}, ${arr[3]}, ${arr[4]}];

      // And this will also work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument const arr4 = [${arr[0]}, ${arr[1]}, ${arr[2]}, ${arr[3]}, ${arr[4]}];

      // And this will work, but TypeScript will report an error // because the map function expects an array as the second argument // Instead, you can use the spread operator to pass the array as the first argument const arr5 = [${arr[0]}, ${arr[1]}, ${arr[2]}, ${arr[3]}, ${arr[4]}]; ```

      以上就是解决在 Vue.js 中出现类型错误的一些方法。你可以根据自己的需求选择合适的方法。

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部