Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Material design component "is not a known element" in Angular2

I have a hybrid Angular1 and Angular2 application. In one of the Angular2 components that I route to, I want to use a Material Design Button.

When I insert a button into the template like this <md-button>foo</md-button> the application starts crashing with this console message

Error: Template parse errors: 'md-button' is not a known element: 1. If 'md-button' is an Angular component, then verify that it is part of this module. 2. If 'md-button' is a Web Component then add "CUSTOM_ELEMENTS_SCHEMA" to the '@NgModule.schemas' of this component to suppress this message. ("<h1>Job</h1>[ERROR ->]<md-button>material</md-button>"): JobComponent@0:12     at TemplateParser.parse (http://localhost:3000/node_modules/@angular/compiler/bundles/compiler.umd.js:8321:21) 

So, it might sound like it is case 1 in the message, but I have tried the advice given in this answer to add MdButton to the imports property of my NgModule (which already contained MaterialModule.forRoot() as adviced by the documentation) , but if I do, the whole application goes blank without errors in the console.

Here is some of the relevant code

import { UIRouterModule } from "ui-router-ng2"; import { Ng1ToNg2Module, uiRouterNgUpgrade } from "ui-router-ng1-to-ng2";  import { MaterialModule, MdButton } from '@angular/material';   const upgradeAdapter: UpgradeAdapter = new UpgradeAdapter(     forwardRef(() => XamFlowNg2Module));  uiRouterNgUpgrade.setUpgradeAdapter(upgradeAdapter);  angular.module("xamFlow")     .factory("consoleService",     upgradeAdapter.downgradeNg2Provider(ConsoleService));   /*  * Expose our ng1 content to ng2  */ upgradeAdapter.upgradeNg1Provider("restService");  @NgModule({     declarations: [         JobComponent,     ],     entryComponents: [         // Components that are routed to must be listed here, otherwise you'll get "No Component Factory"         JobComponent,     ],     imports: [         CommonModule,         BrowserModule,         FormsModule,         HttpModule,         Ng1ToNg2Module,         MaterialModule.forRoot()     ],     providers: [         ConsoleService,         ImageService     ] }) class MyModule { }   upgradeAdapter.bootstrap(document.body, ["myApp"]); 
like image 779
Klas Mellbourn Avatar asked Oct 22 '16 18:10

Klas Mellbourn


People also ask

Is not a known element angular test?

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 do you declare a module component?

Set up the Module Structure in Our App If we set upthe module first, a folder will get created with our core module. Then, when the component is generated, it would get placed inside the same folder and will be declared in the module by our CLI.

How do I use a component from another module?

Component can be declared in a single module only. In order to use a component from another module, you need to do two simple tasks: Export the component in the other module. Import the other module, into the current module.

Can't bind to since it isn't a known property?

What does "Can't bind to 'x' since it isn't a known property of 'y'" mean? link. This error often means that you haven't declared the directive "x" or haven't imported the NgModule to which "x" belongs. Perhaps you declared "x" in an application sub-module but forgot to export it.


1 Answers

It should be

<button md-button>foo</button>    

OR

<button md-raised-button>foo</button> 
like image 79
Nikhil Shah Avatar answered Sep 21 '22 01:09

Nikhil Shah