Error in parsing template: The element 'mat-icon' is unrecognized and cannot be processed

I'm currently working on a project using Angular CLI and angular material v.5.2.5, and I'm encountering an issue when trying to implement

mat-icon-button

The console is showing the following error:

Uncaught Error: Template parse errors: 'mat-icon' is not a known element...

However, if I switch to using

mat-raised-button

everything works perfectly. I'm struggling to find a solution for this problem.

Here are snippets from my code:

index.html

 <!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Todo</title>
  <base href="/">

  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700,400italic"
    rel="stylesheet">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>
  <app-root></app-root>
</body>
</html>

main.ts

 import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app/app.module';
import { environment } from './environments/environment';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule)
  .catch(err => console.log(err));

app.module.ts

 import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { MatButtonModule } from '@angular/material/button';


import { AppComponent } from './app.component';


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    MatButtonModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Answer №1

Make sure to include the MatIconModule in your imports section.

imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    MatButtonModule,
    MatIconModule, // This line was missing
],

Answer №2

When working with Angular 9 or later, make sure to

import { MatIconModule } from '@angular/material/icon'

Next, add the following line to your imports array:

imports: [
    // other imports...
    MatIconModule, // <-- this line
],

Answer №3

Utilizing Angular 6, I have created a separate file named material.module.ts to house all of my mat Module dependencies. To incorporate this file into app.module.ts, simply reference material.module.ts and everything will function properly. Adding 'MatIconModule' to my code proved successful.

Below is the code snippet:

material.module.ts

import { NgModule } from '@angular/core';
import { CommonModule } from '@angular/common';

import {MatButtonModule,MatCheckboxModule,MatToolbarModule,MatInputModule,MatProgressSpinnerModule,MatCardModule,MatMenuModule, MatIconModule} from '@angular/material';


@NgModule({
  imports: [MatButtonModule, MatCheckboxModule,MatToolbarModule,MatInputModule,MatProgressSpinnerModule,MatCardModule,MatMenuModule,MatIconModule],
  exports: [MatButtonModule, MatCheckboxModule,MatToolbarModule,MatInputModule,MatProgressSpinnerModule,MatCardModule,MatMenuModule, MatIconModule]
})


export class MaterialModule{}

and app.module.ts

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';

import { MaterialModule } from './material.module';


@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    BrowserAnimationsModule,
    MaterialModule,
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

Answer №4

Personally, I experienced a situation where the mat icon module was successfully imported, but the error was actually arising from components that were not included in the app module declarations for unknown reasons.

@NgModule({
  declarations: [
   ...
   TheFileWithMatIconTagComponent,
   ...
 ]

Answer №5

There is a possibility that your app.module.ts or its associated .module.ts file may not compile correctly. Instead of indicating errors in the module itself, it might only display an error message without specifying the exact problem. It is recommended to inspect the related files for any import issues or problematic code.

Answer №6

import {MatIconModule} from '@angular/material';// ensure this is added to the app.module.ts file

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    BrowserModule,
    AppRoutingModule,
    BrowserAnimationsModule,
    MatSliderModule,
    MatButtonModule,
    MatCheckboxModule,
    MatIconModule // make sure to include this in the app.module.ts file
  ],

Answer №7

In the event that you have ensured that the modules are set and imported correctly but the issue still persists, one troubleshooting step to consider is turning off the server using Ctrl+C and then restarting it again.

Answer №8

execute:

ng add @angular/material 

and all will be well.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Testing Material-UI's autocomplete with React Testing Library: a step-by-step guide

I am currently using the material-ui autocomplete component and attempting to test it with react-testing-library. Component: /* eslint-disable no-use-before-define */ import TextField from '@material-ui/core/TextField'; import Autocomplete from ...

Troubleshooting the tabindex issue with buttons in MatMenu in Angular

I am currently using Angular 17 in my project and I am working on adding ADA compliance to it by placing tabindex where needed. However, I am encountering an issue with a button inside a mat menu. The tabindex="0" attribute is not focusing on th ...

Upon the initial render, the fetch request in the NextJS Client component is not triggered

When I load my home page, I want to display a collection of cards from a client component. However, the API fetch request in the client component does not trigger on the initial render, preventing the cards from appearing. To fix this issue, I have to manu ...

Exploring the power of developing AngularJS applications using Typescript and the app

I am currently working on a basic application that utilizes Typescript and AngularJS. In my app, I have used app.js to register my controller and some route parameters: /// <reference path="../scripts/typings/angularjs/angular.d.ts" /> /// <refer ...

The current version of Aurelia in Karma does not support the use of Reflect.getOwnMetadata as a function

Since I updated to the most recent version of Aurelia (March update beta.1.1.4), I consistently encounter this error whenever I run karma tests: Error: Reflect.getOwnMetadata is not a function Error loading C:/Software/myproject/test/unit/myclass.spec.ts ...

What is the process for injecting dependencies in a custom Angular class?

I am facing an issue with my Angular service setup: @Injectable({ providedIn: 'root', }) export class Services { public service1 = new Service1(); } The Service1 class looks like this: export class Service1 { public http: HttpRequ ...

Is SASS stylesheet supported by the Angular 2 Ahead-of-Time compiler?

Considering giving Angular 2 Ahead-of-Time compilation another shot. I'll have to do some significant refactoring since my current custom build process will need to be reworked. Prior to diving in, I'm curious: if I reference external .scss fil ...

Unable to connect with the 'formControl' as it is not a recognized attribute of the 'select' element

I am trying to create a simple select element with values from an enumerated list. You can check out the demo for this on Stackblitz here. I copied the code directly from another project which had a working stackblitz implementation and encountered the fo ...

No types are assigned to any props

I recently began working on a SvelteKit skeleton project for my personal website. However, I encountered an error when using Svelte with TypeScript - specifically, I kept getting the message Type '<some prop type>' is not assignable to type ...

How can I pass an array of string inputs into Vue 3?

Working with Vue 3, I'm setting up a form that will display text input fields corresponding to a fixed-length array of strings. Each field's v-model should connect to the respective string in the array. Here is my current code snippet for the vie ...

Converting JSON to TypeScript: How to properly serialize property names instead of the base class field names

Take a look at the code snippet below. I anticipate the serialized result to be: { "origin": { "x": 1, "y": 2 }, "size": { "width": 3, "height": 4 } } However, the actual result is: { "origin": { ...

What is the best method to create a TypeScript dictionary from an object using a keyboard?

One approach I frequently use involves treating objects as dictionaries. For example: type Foo = { a: string } type MyDictionary = { [key: string]: Foo } function foo(dict: MyDictionary) { // Requirement 1: The values should be of type Foo[] const va ...

Angular now displays an unsupported Internet Explorer version message instead of showing a white screen

I am responsible for developing new features and maintaining an Angular application (version 8.3.4). Initially, we wanted the application to be compatible with all versions of Internet Explorer, but that turned out to be impractical. While the application ...

Having difficulty in dynamically loading an image from an API's URL in Angular

In the title, I mentioned that I am utilizing a free API to display cryptocurrency news for my practice project. Everything seems to be working fine except for displaying the images in card view. I will share my code here, so if you have any suggestions on ...

Error in Typescript: Function not being triggered on button click

As someone who is just starting out with typescript, I've been tackling a simple code that should display an alert message in the browser when a button is clicked. I've experimented with the button and input tags, as well as using both onclick ev ...

Error: An unexpected token < was caught in the TypeScript Express code

Using TypeScript to compile and run an Express server that simply serves an HTML file. However, encountering an error in the response under the network tab in Chrome for app.js: Uncaught SyntaxError: Unexpected token '<' Below is the server c ...

What is the process for displaying an error message in a component.ts file?

Is there a way to receive error messages in my component.ts file? How can I display the service class error message on an HTML page? Component.ts Method: addNew(): any { this.apiService.addIndexReference(Data.AccessToken,this.indexReference,this.inde ...

Resolving parent routes in Angular 2

I am encountering an issue with my code. The 'new' route is a child route for the 'users' route. The 'users' route has a resolver, and everything works fine up to this point. However, after successfully creating a new user, ...

The joinAndSelect function in NestJS is having trouble fetching all the data from the PostgreSQL database

In my database, I have three tables named product, branch, and product_branches. The product table contains columns: id, name The branch table contains columns: id, name, lat, lng And the product_branches table contains columns: productId, branchId I w ...

Can a TypeScript generator function be accurately typed with additional functionality?

Generator functions come with prototype properties that allow for the addition of behavior. The generated generators inherit this behavior. Unfortunately, TypeScript does not seem to recognize this feature, leaving me unsure of how to make it aware. For i ...