Skip to content

子工作流转换#

¥Sub-workflow conversion

Feature availability

适用于 n8n 版本 1.97.0 及更高版本的所有套餐。

¥Available on all plans from n8n version 1.97.0.

使用子工作流转换将你的工作流重构为可重用的部分。引用其他节点的表达式会自动更新并作为参数添加到 执行工作流触发器 节点中。

¥Use sub-workflow conversion to refactor your workflows into reusable parts. Expressions referencing other nodes are automatically updated and added as parameters in the Execute Workflow Trigger node.

请参阅 sub-workflows 了解该概念的概述。

¥See sub-workflows for a general introduction to the concept.

选择子工作流的节点#

¥Selecting nodes for a sub-workflow

要将工作流的一部分转换为子工作流,你必须选择原始工作流中要转换的节点。

¥To convert part of a workflow to a sub-workflow, you must select the nodes in the original workflow that you want to convert.

选择一组有效的节点。选择必须是连续的,并且必须最多从一个起始节点和一个结束节点连接到工作流的其余部分。选择必须满足以下条件:

¥Do this by selecting a group of valid nodes. The selection must be continuous and must connect to the rest of the workflow from at most one start node and one end node. The selection must fulfill these conditions:

  • 不得包含触发节点。

¥Must not include trigger nodes.

  • 选择集中的单个节点只能与选择集之外的节点建立入站连接。

¥Only a single node in the selection can have incoming connections from nodes outside of the selection.

  • 该节点可以有多个传入连接,但只能有一个输入分支(这意味着它不能是 合并节点 等)。

    ¥That node can have multiple incoming connections, but only a single input branch (which means it can't be a Merge node for example).

  • 该节点不能有来自选择集中其他节点的入站连接。

    ¥That node can't have incoming connections from other nodes in the selection.

  • 选择集中的单个节点只能与选择集之外的节点建立出站连接。

¥Only a single node in the selection can have outgoing connections to nodes outside of the selection.

  • 该节点可以有多个出站连接,但只能有一个输出分支(例如,它不能是 如果节点)。

    ¥That node can have multiple outgoing connections, but only a single output branch (it can't be an If node for example).

  • 该节点不能有指向选择集中其他节点的出站连接。

    ¥That node can't have outgoing connections to other nodes in the selection.

  • 选择必须包含输入节点和输出节点之间的所有节点。

¥The selection must include all nodes between the input and output nodes.

如何将工作流的一部分转换为子工作流#

¥How to convert part of a workflow to a sub-workflow

选择画布上所需的节点。右键单击画布背景,然后选择“转换为子工作流”。

¥Select the desired nodes on the canvas. Right-click the canvas background and select Convert to sub-workflow.

注意事项#

¥Things to keep in mind

大多数子工作流转换都能正常工作,但有一些注意事项和限制需要牢记:

¥Most sub-workflow conversions work without issues, but there are some caveats and limitations to keep in mind:

  • 你必须手动设置输入和输出的类型约束。默认情况下,子工作流的输入和输出允许所有类型。你可以在子工作流的 执行子工作流触发节点编辑字段(集)节点 中设置预期类型(标记为“返回”,并且仅当子工作流有输出时才包含)。

¥You must set type constraints for input and output manually: By default, sub-workflow input and output allow all types. You can set expected types in sub-workflow's Execute Sub-workflow Trigger node and Edit Fields (set) node (labeled as Return and only included if the sub-workflow has outputs).

  • 对 AI 节点的支持有限:处理 AI 工具等子节点时,你必须选择所有子节点,并且可能需要在转换之前复制与其他 AI 代理共享的任何节点。

¥Limited support for AI nodes: When dealing with sub-nodes like AI tools, you must select them all and may need to duplicate any nodes shared with other AI Agents before conversion.

  • 使用 v1 执行顺序:无论工作流如何设置,新工作流都将使用 v1 执行顺序。父工作流设置 - 你可以在设置中将其改回原来的方式。

¥Uses v1 execution ordering: New workflows use v1 execution ordering regardless of the parent workflow's settings - you can change this back in the settings.

  • 访问器函数(例如 first()last()all())需要格外注意:使用这些函数的表达式并非总能完美地转换为子工作流上下文。n8n 可能会转换凭据以尝试保留其功能,但你应该检查它们在新环境中是否按预期工作。

¥Accessor functions like first(), last(), and all() require extra care: Expressions using these functions don't always translate cleanly to a sub-workflow context. n8n may transform them to try to preserve their functionality, but you should check that they work as intended in their new context.

Sub-node parameter suffixes

1
n8n adds suffixes like `_firstItem`, `_lastItem`, and `_allItems` to variable names accessed by these functions. This helps preserve information about the original expression, since item ordering may be different in the sub-workflow context.
  • itemMatching 函数需要一个固定的索引:使用 itemMatching 功能 时,索引值不能使用表达式。你必须传递一个固定的数字。

¥The itemMatching function requires a fixed index: You can't use expressions for the index value when using the itemMatching function. You must pass it a fixed number.