执行自定义Schematics 'my-comp'时无法解析工厂问题求助
I ran into this exact issue when building custom schematics before, and looking at your code and configuration, the problem comes down to a mismatch between your function export style and the factory path in collection.json.
Here are two straightforward fixes:
Option 1: Update your collection.json to match the default export
Since you're using export default for your schematic function, the factory reference should point to #default instead of #myComp:
{ "$schema": "../node_modules/@angular-devkit/schematics/collection-schema.json", "schematics": { "my-comp": { "description": "This schematics generate an Angular Component on the current module or in an specified one", "factory": "./my-comp/index#default" } } }
Option 2: Switch to a named export in your factory code
If you prefer to keep the #myComp reference in your config, change your default export to a named export:
export function myComp(options: IFlexComponentOptions): Rule { return (host: Tree, context: SchematicContext) => { console.log("options before sanatize", options); // ... your existing logic return rule(host, context); }; }
Why this happens
The Angular DevKit schematics engine looks for the exact export name you specify in collection.json. When you use export default, the internal identifier for that function is default — not the name you gave the function. Your config was trying to find a named export called myComp, which didn't exist, hence the resolution error.
Quick validation steps after fixing
- If you're using TypeScript, make sure you've compiled your code with
tsc(schematics runs against the compiled JavaScript files) - Re-run the command:
schematics .:my-comp
内容的提问来源于stack exchange,提问作者Jorge C

