{"id":1894,"date":"2017-03-03T13:37:15","date_gmt":"2017-03-03T13:37:15","guid":{"rendered":"http:\/\/salientsoft.co.uk\/?p=1894"},"modified":"2018-10-10T17:15:57","modified_gmt":"2018-10-10T17:15:57","slug":"various-declarationimportprovider-error-categories-in-angular-2","status":"publish","type":"post","link":"https:\/\/salientsoft.co.uk\/?p=1894","title":{"rendered":"Various DI related error categories in Angular 2"},"content":{"rendered":"<p>This post is a common point to document basic errors of the above kind.<\/p>\n<p>1\/ Karma tests fail to run giving the following kind of error:-<\/p>\n<blockquote><p><span style=\"font-family: 'Lucida Console'; font-size: small;\">&#8216;places-list&#8217; is not a known element:<br \/>\n1. If &#8216;places-list&#8217; is an Angular component, then verify that it is part of this module.<br \/>\n2. If &#8216;places-list&#8217; is a Web Component then add &#8220;CUSTOM_ELEMENTS_SCHEMA&#8221; to the &#8216;@NgModule.schemas&#8217; of this component to suppress this message. (&#8221;<br \/>\n&lt;div&gt; Places results:- &lt;\/div&gt;<\/span><\/p>\n<p><span style=\"font-family: 'Lucida Console'; font-size: small;\">[ERROR -&gt;]&lt;places-list&gt;&lt;\/places-list&gt;<br \/>\n<\/span><\/p><\/blockquote>\n<p>In this case the application ran but the tests failed as above, following the addition of a new component, PlacesList, which was added via <em>ng new component, <\/em>and used in the roo<em>t AppComponent.<\/em><\/p>\n<p><a href=\"http:\/\/stackoverflow.com\/questions\/39810320\/angular-2-ng-test-x-is-not-a-known-component\">This StackOverflow post<\/a> details the issue and the solution. In my case the new component needed adding to the declarations list in <em>TestBed.configureTestingModule <\/em>in <em>app.component.spec.ts <\/em>as the component was a required dependency. I had added it (or ng generate may have, I cannot recall) to the <em>@NgModule<\/em> declarations list in <em>app.module.ts<\/em>, which enabled the application to run. However the test bed needed it too.<\/p>\n<p>&nbsp;<\/p>\n<p>2\/ I hit a similar issue with the PlacesService. I was getting this error on the tests:<\/p>\n<blockquote><p><span style=\"font-family: 'Lucida Console'; font-size: small;\">Chrome 56.0.2924 (Windows 7 0.0.0) PlacesListComponent should create FAILED<br \/>\nError: No provider for PlacesService!<br \/>\nError: DI Error<\/span><\/p><\/blockquote>\n<p>I used 2 different ways around this problem.<\/p>\n<p>a\/ I added PlacesService as a provider in\u00a0 places-list.component.spec.ts \u2013 this enabled both the tests and the app to work.<\/p>\n<blockquote>\n<pre><span style=\"font-family: 'Lucida Console'; font-size: small;\">beforeEach(async(() =&gt; {\r\n  TestBed.configureTestingModule({\r\n    declarations: [ PlacesListComponent ],\r\n    providers: [PlacesService]\r\n  })\r\n  .compileComponents();\r\n}));<\/span><\/pre>\n<\/blockquote>\n<p>b\/ Alternatively, I could also add PlacesService as a provider in both <em>app.component.ts<\/em> (i.e. at the top level) and the test definition,\u00a0 <em>places-list-component.spec.ts<\/em><\/p>\n<blockquote>\n<pre><span style=\"font-family: 'Lucida Console'; font-size: small;\">@Component({\r\n  selector: 'app-root',\r\n  templateUrl: '.\/app.component.html',\r\n  styleUrls: ['.\/app.component.css'],\r\n  providers: [PlacesService]\r\n})<\/span><\/pre>\n<\/blockquote>\n<blockquote>\n<pre><span style=\"font-family: 'Lucida Console'; font-size: small;\">beforeEach(async(() =&gt; {\r\n  TestBed.configureTestingModule({\r\n    declarations: [ PlacesListComponent ],\r\n    providers: [PlacesService]\r\n  })\r\n  .compileComponents();\r\n}));<\/span><\/pre>\n<\/blockquote>\n<p>&nbsp;<\/p>\n<p>Whilst b\/ defines the provider in 2 places, from basic knowledge this feels better as in a\/ the provider is defined in the component that uses the service which feels almost as closely coupled as if I was not using DI at all.<\/p>\n<p>Interestingly, when I removed the PlacesService as a provider from app.components.ts and added it to the @NgModule as a provider, in app.module.ts, this failed similarly. <em>This was strange as I expected app.module.ts to declare the provider globally (expect perhaps for the testbeds).<\/em><\/p>\n<p>This needs further looking into and understanding of the DI concepts \u2013 on the face of it, the providers (which are the actual lists of components to be injected) should be as decoupled as possible from the classes where they are used. At the moment, a lot of what I am seeing looks far too incestuous, coming from a background in Java\/Spring where Spring is entirely external and responsible for the \u2018provider list\u2019.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This post is a common point to document basic errors of the above kind. 1\/ Karma tests fail to run giving the following kind of error:- &#8216;places-list&#8217; is not a known element: 1. If &#8216;places-list&#8217; is an Angular component, then verify that it is part of this module. 2. If &#8216;places-list&#8217; is a Web Component [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[198],"tags":[199,40],"_links":{"self":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1894"}],"collection":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1894"}],"version-history":[{"count":4,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1894\/revisions"}],"predecessor-version":[{"id":1906,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/1894\/revisions\/1906"}],"wp:attachment":[{"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1894"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1894"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/salientsoft.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1894"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}