Angular 6 AOT编译报错:自定义组件期望0参数却传入1
Hey there, let's break down these tricky AOT compilation errors you're hitting—since your project runs fine in JIT mode, this all boils down to AOT's much stricter template validation rules that JIT lets slide. Let's go through each scenario and fix them step by step.
Common Root Causes & Fixes for Each Error
1. TableOfContentComponent & InfinityPageScrollComponent: "Expected 0 arguments, got 1" (no @Input properties)
If these components don't have any @Input properties defined but the compiler is yelling about unexpected arguments, here's what to check:
- Accidental method binding: Double-check your component class for any methods with names that match attributes you're using in the template. For example, if
TableOfContentComponenthas a method calledupdate(), and your template has<app-table-of-content [update]="someValue">, AOT will interpret this as trying to call theupdate()method with a parameter—even though you meant it as an input (which doesn't exist). Remove any such invalid bindings. - Typos in template attributes: Make sure you haven't added a random attribute or mistyped a property on the component tag. Even a stray
[foo]="bar"on a component without that input will trigger this error in AOT. - Selector conflicts: Ensure your component's selector isn't clashing with a native HTML element or another component. For example, if your selector is
table-of-content, make sure you're not accidentally using a native element syntax that's being misparsed.
2. SliderComponent & CarouselComponent: "Expected 0 arguments, got 1" (has @Input properties, bindings exist)
For components that do have @Inputs but still throw this error, the issue is usually a mismatch between your template bindings and the component's definition:
- Case sensitivity issues: Angular AOT is strict about property name casing, while JIT is more forgiving. If your component has
@Input() slideIndex: number, but your template uses[slideindex]="currentIndex"(lowercase 'i'), AOT won't recognize the input and will treat it as an invalid method call. Fix the casing to match exactly. - Type mismatches: Check that the value you're passing to the
@Inputmatches its declared type. For example, if your@Input()expects anumberbut you're passing a string (like"1"instead of1), AOT's type checker will flag this as an invalid argument. Use type conversion if needed, e.g.,[slideIndex]="+currentSlideId"to turn a string into a number. - Missing provider for constructor dependencies: If your component's constructor has parameters (like a service) that aren't properly provided in your module, AOT can fail to instantiate the component and throw a misleading "argument mismatch" error. Verify all constructor dependencies are marked with
@Injectable()and added to your module'sprovidersarray.
3. InfinityPageScrollComponent: No attributes, still getting the error
This odd scenario usually stems from:
- Cached compilation artifacts: Old AOT cache can cause false positives. Delete your
distfolder,node_modules/.cache(if using webpack), and recompile from scratch. - Component inheritance issues: If this component extends another class, make sure the parent class's constructor doesn't have unprovided dependencies or invalid input definitions that are leaking into the child.
General Troubleshooting Steps
Beyond the specific fixes above, try these to narrow down the issue:
- Enable strict template checking: In your
tsconfig.json, add"strictTemplates": trueunderangularCompilerOptions. This will give you more precise error messages instead of the vague "argument mismatch" alerts. - Update dependencies: Your versions (
@ngtools/webpack@6.0.3,@angular@6.0.2,webpack@4.0.0) are quite old and have known AOT bugs. Upgrade to the latest patch versions in the Angular 6.x line (e.g.,@angular@6.1.10,@ngtools/webpack@6.2.9)—these patches fix a lot of edge-case AOT compilation issues. - Recheck private/protected members: Even though you said you fixed this, double-check that no template is referencing private or protected properties/methods in any component—AOT will reject this outright.
内容的提问来源于stack exchange,提问作者Yanis-git

