执行npm run dist构建ES2015包时Angular报错,npm start运行正常
Hey there! Let's break down why you're seeing this error only when running npm run dist (production build) and not with npm start (dev server), plus how to fix it quickly.
What's the Root Cause?
The core issue is strictness: Angular's production build enables strict template type checking by default (via settings like strictTemplates or fullTemplateTypeCheck in your angular.json), while the dev server (ng serve, which npm start calls) uses more lenient checks. This means tiny type mismatches that slip through in development get flagged during production builds.
Your specific error Expected 0 arguments, but got 1 points to a method call in your mat-autocomplete.html template where you’re passing an argument to a function that doesn’t accept any. Since removing the <button> tag fixes everything, this is definitely tied to the click event on your custom close button.
Step-by-Step Fix
Check your close button's click binding
Your button code in the autocomplete template is probably something like this:<button (click)="autoComplete.closePanel($event)">×</button>The problem here?
MatAutocomplete.closePanel()doesn’t take any arguments—but you’re passing$eventto it.Remove the unnecessary argument
Update the click binding to call the method without any parameters:<button (click)="autoComplete.closePanel()">×</button>This matches the official
MatAutocompleteAPI, which definesclosePanel()as a zero-argument method.Verify strict mode settings (optional but good practice)
Double-check yourangular.jsonto confirm production build strictness:- Navigate to
projects -> [your-project-name] -> architect -> build -> options - Ensure
strictTemplates: trueis present (this is default for new Angular projects, and it’s what caught this error) - Avoid disabling this setting long-term—it helps catch bugs before they reach production.
- Navigate to
Confirm version compatibility
Look at yourpackage.json(attachment 3) to make sure your Angular core version matches your Angular Material version. Mismatched versions can cause unexpected type-checking hiccups (e.g., Angular 15 paired with Material 14).
Why This Works in Development?
The dev server (ng serve) disables some strict checks by default to speed up rebuilds. It lets minor type slips slide, but the production build prioritizes correctness and performance, so it flags these mismatches.
内容的提问来源于stack exchange,提问作者user266909

