react,打字稿,忽略生成器方法的类,webpack 问题?

问题描述 投票:0回答:1

我有两个不同的项目,第一个是用 create react app + typescript 创建的,在这个项目中我有一个类来处理使用生成器的树结构:

class Nodo {
    key: string | number
    title: string
    parent: string | number | null
    [prop: string]: any;
    children?: Nodo[];

    constructor(key: number | string, title: string, parent: string | number | null, children: Nodo[] = [], data?: { [key: string]: any }) {
        this.key = key
        this.title = title
        this.parent = parent
        this.children = children
        this.data = data
    }
    get isLeaf() {
        console.log(this.children?.length)
        return this.children?.length === 0
    }

    get hasChildren() {
        return !this.isLeaf
    }
}


export class myTree {
    root: Nodo
    constructor(rootNode: Nodo) {
        this.root = rootNode
    }

    *preOrderTraversal(node = this.root): any {
        console.log("pre")
        yield node;
        if (node.children?.length) {
            for (let child of node.children) {
                yield* this.preOrderTraversal(child);
            }
        }
    }

    *postOrderTraversal(node = this.root): any {
        if (node.children?.length) {
            for (let child of node.children) {
                yield* this.postOrderTraversal(child);
            }
        }
        yield node;
    }

    insert(parentNodeKey: number | string, newNode: Nodo) {
        newNode.parent = parentNodeKey
        for (let node of this.preOrderTraversal()) {
            console.log("bucle insert")
            if (node.key === parentNodeKey) {
                node.children.push(newNode);
                return true;
            }
        }
        return false;
    }

    remove(key: number | string) {
        let node: Nodo
        for (node of this.preOrderTraversal()) {
            const filtered = node.children?.filter(c => c.key !== key);
            if (filtered?.length !== node.children?.length) {
                node.children = filtered;
                return true;
            }
        }
        return false;
    }

    find(key: number | string) {
        for (let node of this.preOrderTraversal()) {
            if (node.key === key) return node;
        }
        return undefined;
    }

}

这在这个项目中工作正常,我可以实例化树并添加节点...

我有第二个项目,它使用了 react、typescrip 和 webpack,我已经将代码从一个项目复制到另一个项目,但它在使用 webpack 的项目中不起作用,我可以实例化类,创建根节点,但是当我尝试使用 insert 方法插入一个新节点,没有任何反应,就像忽略 *preOrderTraversal 一样,没有错误或警告,并且方法中的 console.log 没有显示......我绝对迷路了... 任何帮助都会很受欢迎......提前致谢

reactjs typescript class webpack generator
1个回答
0
投票

我已经更改了我的 tsconfig.json,目标为 ES6,现在它正在工作......

最新问题
© www.soinside.com 2019 - 2024. All rights reserved.