Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

after upgrading to angular 12, all material components are "not a known element"

After upgrading to Angular 12, although I have all the required code in app.module.ts, all the material tags are not recognized: for example, for mat-checkbox, I have the following in app.module.ts

import { MatCheckboxModule } from '@angular/material/checkbox';

@NgModule({
    imports: [
    BrowserModule,  
    MatCheckboxModule,

and at execution:

'mat-checkbox' is not a known element:

  1. If 'mat-checkbox' is an Angular component,then verify that it is part of this module.
  2. If 'mat-checkbox' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component

And same problem for all the material tags (mat-icon, mat-table, mat-label . . .).

In previous version (Angular 9), all was OK. Thanks for help.

like image 541
bruno Avatar asked May 21 '21 08:05

bruno


People also ask

Why is angular material update not working?

Angular material update: It gives errors in some versions of angular material when running update scripts because of the update scripts do not run smoothly in some versions. What I did is updating material after getting everything done. So when I finish with all other updates, I just ran ng update @angular/material.

How do I get Started with angular material?

Getting started Add Angular Material to your project! Schematics Use schematics to quickly generate views with Material Design components. Theming Angular Material Customize your application with Angular Material's theming system. Theming your own components Use Angular Material's theming system in your own custom components.

How to fix “my-element is not a known element” error in angular?

In Angular 9 and 10 we can notice that the “ my-element is not a known element ” error is missing when our tests don’t have all required stubs. Make sure to check debug messages when running tests and add all absent stubs. Otherwise, you will have to update your test suite when the Angular team fixes this bug.

How does angular 9 handle unknown elements in unit tests?

It seems that since version 9 Angular handles unknown elements in unit tests differently than the previous releases. If we forget to stub an element that we used inside a component under test, the test will pass. Furthermore, the message about an unknown element will be displayed only in the debug window.


Video Answer


2 Answers

Note 1 (fatal problem)

You are using lazy loading. somewhere in your project, you have the old syntax of lazy loading

{
  ...
  loadChildren: 'path/to/your.module#YourModule'
}

Replace all of them in your project to:

{
  ...
  loadChildren: () => import('path/to/your.module').then(m => m.YourModule)
}

Even if you think it is not related to your current feature. After doing this the problem will be solved.

Note2:

Also during the upgrade to Angular12 if some npm packages are broken you can use

npm i --save package-name --force

to install it for now.

Note3:

Also if you see some bugs in the ts files you can suppress(ignore) them by using // @ts-ignore on top of the error line for now.

Note4

Also if you see an error like "Class is using Angular features but is not decorated. Please add an explicit Angular decorator".

add @Injectable() on top of the class declaration.

Note5

In my idea, If You are upgrading from an older version of angular to angular 12. For doing this first create a project with ng new APP_NAME --strict=false which you don't have a strict mode for now. When all problems are solved and you have a working project now you can create a new project which has strict mode.

:)-|-< If the answer was useful Vote +1 Please >-|-(:

like image 162
Jafar Amini Avatar answered Nov 15 '22 05:11

Jafar Amini


i am assuming you are having lazy loaded modules.

if yes,

create a shared module,

import and export all material module in shared module,

import the shared module in root (app) module and feature (lazy loaded) modules.

if no, then what you have done was right, i guess.

like image 20
Shankarlal D Avatar answered Nov 15 '22 04:11

Shankarlal D