diff --git a/cocos/scene-graph/node.ts b/cocos/scene-graph/node.ts index 322b533fa4..577820e37d 100644 --- a/cocos/scene-graph/node.ts +++ b/cocos/scene-graph/node.ts @@ -862,19 +862,35 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable { public getComponent(classConstructor: Constructor | AbstractedConstructor): T | null; /** - * @en - * Returns the component of supplied type if the node has one attached, null if it doesn't. - * You can also get component in the node by passing in the name of the script. - * @zh - * 获取节点上指定类型的组件,如果节点有附加指定类型的组件,则返回,如果没有则为空。 - * 传入参数也可以是脚本的名称。 - * @param className The class name of the target component - * @example - * ``` - * // get custom test class. - * var test = node.getComponent("Test"); - * ``` - */ + * @en + * Returns the component of supplied type if the node has one attached, null if it doesn't. + * You can also get component in the node by passing in the name of the script. + * @zh + * 获取节点上指定类型的组件,如果节点有附加指定类型的组件,则返回,如果没有则为空。 + * 传入参数也可以是脚本的名称。 + * @param className The class name of the target component + * @example + * ``` + * // get custom test class. + * var test = node.getComponent("Test") as Test | null; + * ``` + */ + public getComponent(className: string): Component | null; + + /** + * @en + * Returns the component of supplied type if the node has one attached, null if it doesn't. + * You can also get component in the node by passing in the name of the script. + * @zh + * 获取节点上指定类型的组件,如果节点有附加指定类型的组件,则返回,如果没有则为空。 + * 传入参数也可以是脚本的名称。 + * @param className The class name of the target component + * @example + * ``` + * // get custom test class. + * var test = node.getComponent("Test"); + * ``` + */ public getComponent(className: string): T | null; public getComponent (typeOrClassName: string | Constructor | AbstractedConstructor): T | null { @@ -896,6 +912,21 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable { * @en Returns all components of given type in the node. * @zh 返回节点上指定类型的所有组件。 * @param className The class name of the target component + * @example + * ``` + * var test = node.getComponents('Test') as Test[]; + * ``` + */ + public getComponents(className: string): Component[]; + + /** + * @en Returns all components of given type in the node. + * @zh 返回节点上指定类型的所有组件。 + * @param className The class name of the target component + * @example + * ``` + * var test = node.getComponents('Test'); + * ``` */ public getComponents(className: string): T[]; @@ -925,7 +956,18 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable { * @param className The class name of the target component * @example * ``` - * var Test = node.getComponentInChildren("Test"); + * var test = node.getComponentInChildren("Test") as Test | null; + * ``` + */ + public getComponentInChildren(className: string): Component | null; + + /** + * @en Returns the component of given type in any of its children using depth first search. + * @zh 递归查找所有子节点中第一个匹配指定类型的组件。 + * @param className The class name of the target component + * @example + * ``` + * var test = node.getComponentInChildren("Test"); * ``` */ public getComponentInChildren(className: string): T | null; @@ -955,7 +997,18 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable { * @param className The class name of the target component * @example * ``` - * var tests = node.getComponentsInChildren("Test"); + * var tests = node.getComponentsInChildren("Test") as Test[]; + * ``` + */ + public getComponentsInChildren(className: string): Component[]; + + /** + * @en Returns all components of given type in self or any of its children. + * @zh 递归查找自身或所有子节点中指定类型的组件 + * @param className The class name of the target component + * @example + * ``` + * var tests = node.getComponentsInChildren("Test"); * ``` */ public getComponentsInChildren(className: string): T[]; @@ -989,7 +1042,19 @@ export class Node extends CCObject implements ISchedulable, CustomSerializable { * @throws `TypeError` if the `className` does not specify a cc-class constructor extending the `Component`. * @example * ``` - * var test = node.addComponent("Test"); + * var test = node.addComponent("Test") as Test; + * ``` + */ + public addComponent(className: string): Component; + + /** + * @en Adds a component class to the node. You can also add component to node by passing in the name of the script. + * @zh 向节点添加一个指定类型的组件类,你还可以通过传入脚本的名称来添加组件。 + * @param className The class name of the component to add + * @throws `TypeError` if the `className` does not specify a cc-class constructor extending the `Component`. + * @example + * ``` + * var test = node.addComponent("Test"); * ``` */ public addComponent(className: string): T;