docs: add guides for the top 10 errors (#40060)
add top 10 error guides provide description and debugging for top errors PR Close #40060
This commit is contained in:
parent
fbfd4889a9
commit
e4df94214b
|
@ -0,0 +1,19 @@
|
|||
@name Expression Changed After Checked
|
||||
@category runtime
|
||||
@shortDescription Expression has changed after it was checked
|
||||
|
||||
@description
|
||||
Angular throws an `ExpressionChangedAfterItHasBeenCheckedError` when an expression value has been changed after change detection has completed. Angular only throws this error in development mode.
|
||||
|
||||
In dev mode, Angular performs an additional check after each change detection run, to ensure the bindings haven’t changed. This catches errors where the view is left in an inconsistent state. This can occur, for example, if a method or getter returns a different value each time it is called, or if a child component changes values on its parent. If either of these occur, this is a sign that change detection is not stabilized. Angular throws the error to ensure data is always reflected correctly in the view, which prevents erratic UI behavior or a possible infinite loop.
|
||||
|
||||
This error commonly occurs when you’ve added template expressions or begun to implement lifecycle hooks like `ngAfterViewInit` or `ngOnChanges`. It is also common when dealing with loading status and asynchronous operations, or a child component changes its parent bindings.
|
||||
|
||||
@debugging
|
||||
The [source maps](https://developer.mozilla.org/en-US/docs/Tools/Debugger/How_to/Use_a_source_map) generated by the CLI are very useful when debugging. Navigate up the call stack until you find a template expression where the value displayed in the error has changed.
|
||||
|
||||
Ensure that there are no changes to the bindings in the template after change detection is run. This often means refactoring to use the correct [component lifecycle hook](https://angular.io/guide/lifecycle-hooks) for your use case. If the issue exists within `ngAfterViewInit`, the recommended solution is to use a constructor or `ngOnInit` to set initial values, or use `ngAfterContentInit` for other value bindings.
|
||||
|
||||
If you are binding to methods in the view, ensure that the invocation does not update any of the other bindings in the template.
|
||||
|
||||
Read more about which solution is right for you in ['Everything you need to know about the "ExpressionChangedAfterItHasBeenCheckedError" error'](https://indepth.dev/everything-you-need-to-know-about-the-expressionchangedafterithasbeencheckederror-error/) and why this is useful at ['Angular Debugging "Expression has changed after it was checked": Simple Explanation (and Fix)'](https://blog.angular-university.io/angular-debugging/).
|
|
@ -0,0 +1,11 @@
|
|||
@name Circular Dependency in DI
|
||||
@category runtime
|
||||
@shortDescription Circular dependency in DI detected while instantiating a provider
|
||||
|
||||
@description
|
||||
A cyclic dependency exists when a [dependency of a service](https://angular.io/guide/hierarchical-dependency-injection) directly or indirectly depends on the service itself. For example, if `UserService` depends on `EmployeeService`, which also depends on `UserService`. Angular will have to instantiate `EmployeeService` to create `UserService`, which depends on `UserService`, itself.
|
||||
|
||||
@debugging
|
||||
Use the call stack to determine where the cyclical dependency exists. You will be able to see if any child dependencies rely on the original file by [mapping out](https://angular.io/guide/dependency-injection-in-action) the component, module, or service’s dependencies and identify the loop causing the problem.
|
||||
|
||||
Break this loop (or circle) of dependency to resolve this error. This most commonly means removing or refactoring the dependencies to not be reliant on one another.
|
|
@ -0,0 +1,19 @@
|
|||
@name No Provider Found
|
||||
@category runtime
|
||||
@shortDescription No provider for found injectorDetails
|
||||
|
||||
@description
|
||||
You see this error when you try to inject a service but have not declared a corresponding provider. A provider is a mapping that supplies a value that you can inject into the constructor of a class in your application.
|
||||
|
||||
Read more on providers in our [Dependency Injection guide](https://angular.io/guide/dependency-injection).
|
||||
|
||||
@debugging
|
||||
Work backwards from the object where the error states that a [provider](https://angular.io/guide/architecture-services) is missing: `No provider for ${this}!`. This is commonly thrown in [services](https://angular.io/tutorial/toh-pt4), which require non-existing providers.
|
||||
|
||||
To fix the error ensure that your service is registered in the list of providers of an `NgModule` or has the `@Injectable` decorator with a `providedIn` property at top.
|
||||
|
||||
The most common solution is to add a provider in `@Injectable` using `providedIn`:
|
||||
|
||||
```typescript
|
||||
@Injectable({ providedIn: 'app' })
|
||||
```
|
|
@ -0,0 +1,20 @@
|
|||
@name Selector Collision
|
||||
@category runtime
|
||||
@shortDescription Multiple components match with the same tagname
|
||||
|
||||
@description
|
||||
Two or more components use the same [element selector](https://angular.io/guide/component-overview#specifying-a-components-css-selector). Because there can only be a single component associated with an element, selectors must be unique strings to prevent ambiguity for Angular.
|
||||
|
||||
@debugging
|
||||
Use the element name from the error message to search for places where you’re using the same [selector declaration](https://angular.io/guide/architecture-components) in your codebase:
|
||||
|
||||
```typescript
|
||||
@Component({
|
||||
selector: 'YOUR_STRING',
|
||||
...
|
||||
})
|
||||
```
|
||||
|
||||
Ensure that each component has a unique CSS selector. This will guarantee that Angular renders the component you expect.
|
||||
|
||||
If you’re having trouble finding multiple components with this selector tag name, check for components from imported component libraries, such as Angular Material. Make sure you're following the [best practices](https://angular.io/guide/styleguide#component-selectors) for your selectors to prevent collisions.
|
|
@ -0,0 +1,25 @@
|
|||
@name Export Not Found
|
||||
@category runtime
|
||||
@shortDescription Export not found!
|
||||
|
||||
@description
|
||||
Angular can’t find a directive with `{{ PLACEHOLDER }}` export name. The export name is specified in the `exportAs` property of the directive decorator. This is common when using FormsModule or Material modules in templates, and you’ve forgotten to [import the corresponding modules](https://angular.io/guide/sharing-ngmodules).
|
||||
|
||||
@debugging
|
||||
Use the export name to trace the templates or modules using this export.
|
||||
|
||||
Ensure that all dependencies are [properly imported and declared in your NgModules](https://angular.io/guide/sharing-ngmodules). For example, if the export not found is `ngForm`, we need to import `FormsModule` and declare it in the list of imports in `*.module.ts` to resolve the error.
|
||||
|
||||
```typescript
|
||||
import { FormsModule } from '@angular/forms';
|
||||
|
||||
@NgModule({
|
||||
...
|
||||
imports: [
|
||||
FormsModule,
|
||||
…
|
||||
```
|
||||
|
||||
If you recently added an import, you may need to restart your server to see these changes.
|
||||
|
||||
|
|
@ -0,0 +1,29 @@
|
|||
@name Argument Not Literal
|
||||
@category compiler
|
||||
@shortDescription Decorator argument is not an object literal
|
||||
|
||||
@description
|
||||
To make the metadata extraction in the Angular compiler faster, the decorators `@NgModule`, `@Pipe`, `@Component`, `@Directive`, and `@Injectable` accept only object literals as arguments.
|
||||
|
||||
This is an [intentional change in Ivy](https://github.com/angular/angular/issues/30840#issuecomment-498869540), which enforces stricter argument requirements for decorators than View Engine. Ivy requires this approach because it compiles decorators by moving the expressions into other locations in the class output.
|
||||
|
||||
@debugging
|
||||
Move all declarations:
|
||||
|
||||
```typescript
|
||||
const moduleDefinition = {...}
|
||||
|
||||
@NgModule(moduleDefinition)
|
||||
export class AppModule {
|
||||
constructor() {}
|
||||
}
|
||||
```
|
||||
|
||||
into the decorator:
|
||||
|
||||
```typescript
|
||||
@NgModule({...})
|
||||
export class AppModule {
|
||||
constructor() {}
|
||||
}
|
||||
```
|
|
@ -0,0 +1,11 @@
|
|||
@name Missing Token
|
||||
@category compiler
|
||||
@shortDescription No suitable injection token for parameter
|
||||
|
||||
@description
|
||||
There is no injection token for a constructor parameter at compile time. [InjectionTokens](https://angular.io/api/core/InjectionToken) are tokens that can be used in a Dependency Injection Provider.
|
||||
|
||||
@debugging
|
||||
Look at the parameter that throws the error and all uses of the class. This error is commonly thrown when a constructor defines parameters with primitive types like `string`, `number`, `boolean`, and `Object`.
|
||||
|
||||
Use the [@Injectable](https://angular.io/api/core/Injectable) method or [@Inject](https://angular.io/api/core/Inject) decorator from `@angular/core` to ensure that the type you are injecting is reified (has a runtime representation). Make sure to add a provider to this decorator so that you do not throw [NG0201: No Provider Found](https://angular.io/errors/NG0201).
|
|
@ -0,0 +1,17 @@
|
|||
@name Invalid Element
|
||||
@category compiler
|
||||
@shortDescription Unknown HTML element or component
|
||||
|
||||
@description
|
||||
One or more elements cannot be resolved during compilation because the element is not defined by the HTML spec, or there is no component or directive with such element selector.
|
||||
|
||||
The runtime error for this is `NG0304: '${tagName}' is not a known element: …’`.
|
||||
|
||||
@debugging
|
||||
Use the element name in the error to find the file(s) where the element is being used.
|
||||
|
||||
Check that the name and selector are correct. If the component is from a different module or import, check that the component is exported from its origin module and imported into the correct `*.modules.ts` file, and declared in the imports list.
|
||||
|
||||
When using custom elements or web components, ensure that you add [`CUSTOM_ELEMENTS_SCHEMA`](https://angular.io/api/core/CUSTOM_ELEMENTS_SCHEMA) to the app module.
|
||||
|
||||
If this does not resolve the error, check the imported libraries for any recent changes to the exports and properties you are using, and restart your server.
|
|
@ -0,0 +1,15 @@
|
|||
@name Invalid Attribute
|
||||
@category compiler
|
||||
@shortDescription Unknown attribute or input
|
||||
|
||||
@description
|
||||
An attribute or property cannot be resolved during compilation.
|
||||
|
||||
This error arises when attempting to bind to a property that does not exist. Any property binding must correspond to either:
|
||||
* A native property on the HTML element, or
|
||||
* An `@Input()` property of a component or directive applied to the element.
|
||||
|
||||
The runtime error for this is `NG0304: '${tagName}' is not a known element: …’`.
|
||||
|
||||
@debugging
|
||||
Look at documentation for the specific [binding syntax](https://angular.io/guide/binding-syntax) used. This is usually a typo or incorrect import. There may also be a missing direction with property selector ‘name’ or missing input.
|
|
@ -0,0 +1,26 @@
|
|||
@name Missing Reference Target
|
||||
@category compiler
|
||||
@shortDescription No directive found with export
|
||||
|
||||
@description
|
||||
Angular can’t find a directive with `{{ PLACEHOLDER }}` export name. This is common with a missing import or a missing [`exportAs`](https://angular.io/api/core/Directive#exportAs) on a directive.
|
||||
|
||||
@debugging
|
||||
Use the string name of the export not found to trace the templates or modules using this export.
|
||||
|
||||
Ensure that all dependencies are properly imported and declared in our Modules. For example, if the export not found is `ngForm`, we will need to import `FormsModule` and declare it in our list of imports in `*.module.ts` to resolve the missing export error.
|
||||
|
||||
```typescript
|
||||
import { FormsModule } from '@angular/forms';
|
||||
|
||||
@NgModule({
|
||||
...
|
||||
imports: [
|
||||
FormsModule,
|
||||
…
|
||||
```
|
||||
|
||||
If you recently added an import, you will need to restart your server to see these changes.
|
||||
|
||||
This is a duplicate issue of a [NG0301: Export Not Found](https://angular.io/errors/NG0301).
|
||||
|
Loading…
Reference in New Issue