You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行npm run dist构建ES2015包时Angular报错,npm start运行正常

Fixing "Expected 0 arguments, but got 1" Error in Angular Material Autocomplete During Production Build

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

  1. 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 $event to it.

  2. Remove the unnecessary argument
    Update the click binding to call the method without any parameters:

    <button (click)="autoComplete.closePanel()">×</button>
    

    This matches the official MatAutocomplete API, which defines closePanel() as a zero-argument method.

  3. Verify strict mode settings (optional but good practice)
    Double-check your angular.json to confirm production build strictness:

    • Navigate to projects -> [your-project-name] -> architect -> build -> options
    • Ensure strictTemplates: true is 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.
  4. Confirm version compatibility
    Look at your package.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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 06:27:56