Can't resolve all parameters for AppComponent
Asked Answered
N

8

14

i'm building big project, this is started part of it. my problem is very common, however I did not find a way to solve it. really need your help! I get an error of type. I will be glad to any help

Uncaught Error: Can't resolve all parameters for AppComponent: (?).
at syntaxError (main-client.js:52112)
at CompileMetadataResolver._getDependenciesMetadata (main-client.js:67193)
at CompileMetadataResolver._getTypeMetadata (main-client.js:67028)
at CompileMetadataResolver.getNonNormalizedDirectiveMetadata (main-client.js:66536)
at CompileMetadataResolver._getEntryComponentMetadata (main-client.js:67341)
at main-client.js:66822
at Array.map (<anonymous>)
at CompileMetadataResolver.getNgModuleMetadata (main-client.js:66822)
at CompileMetadataResolver.getNgModuleSummary (main-client.js:66645)
at main-client.js:66733
at Array.forEach (<anonymous>)
at CompileMetadataResolver.getNgModuleMetadata (main-client.js:66718)
at JitCompiler._loadModules (main-client.js:85147)
at JitCompiler._compileModuleAndComponents (main-client.js:85108)
at JitCompiler.compileModuleAsync (main-client.js:85024)
at CompilerImpl.compileModuleAsync (main-client.js:50948)
at PlatformRef.bootstrapModule (main-client.js:6395)
at Object.defineProperty.value (main-client.js:49385)
at __webpack_require__ (main-client.js:679)
at fn (main-client.js:89)
at Object.options.path (main-client.js:48085)
at __webpack_require__ (main-client.js:679)
at module.exports (main-client.js:725)
at main-client.js:728

Here is my app.module.shared.ts

    import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { TranslateModule, TranslateLoader } from '@ngx-translate/core';
import { TranslateHttpLoader } from '@ngx-translate/http-loader';

import { AppComponent } from './app.component';
import { AppService } from './app.service'
import { HttpClient, HttpClientModule } from '@angular/common/http';
export function HttpLoaderFactory(http: HttpClient) {
    return new TranslateHttpLoader(http,
    ((typeof window !== 'undefined') ? window.location.origin: "http://localhost:5000")
        +'/assets/i18n/', '.json');
}

@NgModule({
declarations: [
    AppComponent,
],
imports: [
    HttpClientModule,
    FormsModule,
    ReactiveFormsModule,
    TranslateModule.forRoot({
    loader: {
        provide: TranslateLoader,
        useFactory: HttpLoaderFactory,
        deps: [HttpClient]
    }
    }),
    BrowserModule    
],
providers: [AppService],
bootstrap: [AppComponent]
})

export class AppModuleShared {
}

Here is my app.component.ts

import { Component } from '@angular/core';
import { AppService } from '@app/app.service';

@Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.css']
})
export class AppComponent {
    constructor(service: AppService) { }
}

Here is my app.component.html

<p>WORK</p>

Here is my app.service.ts

import { Injectable } from '@angular/core';
@Injectable()
export class AppService { }

Here is my package.json

    {
    "name": "new_ang",
    "private": true,
    "version": "0.0.0",
    "scripts": {
        "test": "karma start ClientApp/test/karma.conf.js"
    },
    "dependencies": {
        "@angular/animations": "5.0.2",
        "@angular/common": "5.0.2",
        "@angular/compiler": "5.0.2",
        "@angular/compiler-cli": "5.0.2",
        "@angular/core": "5.0.2",
        "@angular/forms": "5.0.2",
        "@angular/http": "5.0.2",
        "@angular/platform-browser": "5.0.2",
        "@angular/platform-browser-dynamic": "5.0.2",
        "@angular/platform-server": "5.0.2",
        "@angular/router": "5.0.2",
        "@aspnet/signalr-client": "1.0.0-alpha2-final",
        "@ng-bootstrap/ng-bootstrap": "1.0.0-beta.5",
        "@nguniversal/aspnetcore-engine": "^5.0.0-beta.5",
        "@ngx-translate/core": "8.0.0",
        "@ngx-translate/http-loader": "^2.0.0",
        "@types/webpack-env": "^1.13.2",
        "angular": "^1.6.6",
        "angular-oauth2-oidc": "3.0.2",
        "angular2-jwt": "0.2.3",
        "aspnet-prerendering": "3.0.1",
        "aspnet-webpack": "2.0.1",
        "bootstrap": "4.0.0-beta.2",
        "core-js": "2.5.1",
        "font-awesome": "4.7.0",
        "isomorphic-fetch": "2.2.1",
        "msgpack5": "3.6.0",
        "ng2-charts": "^1.6.0",
        "ng2-tree": "^2.0.0-rc.2",
        "ngx-swiper-wrapper": "^5.0.2",
        "preboot": "5.1.7",
        "rxjs": "5.5.2",
        "zone.js": "0.8.18"
    },
    "devDependencies": {
        "@angular/cli": "1.5.2",
        "@angular/compiler-cli": "5.0.2",
        "@compodoc/compodoc": "1.0.4",
        "@ngtools/webpack": "1.8.2",
        "@types/hammerjs": "2.0.35",
        "@types/jasmine": "2.6.3",
        "@types/node": "8.0.51",
        "@types/selenium-webdriver": "3.0.8",
        "@types/source-map": "0.5.2",
        "@types/uglify-js": "2.6.29",
        "@types/webpack": "3.8.1",
        "angular-router-loader": "^0.8.0",
        "angular2-template-loader": "0.6.2",
        "awesome-typescript-loader": "3.3.0",
        "codelyzer": "4.0.1",
        "css-loader": "0.28.7",
        "extract-text-webpack-plugin": "3.0.2",
        "file-loader": "1.1.5",
        "html-loader": "0.5.1",
        "html-webpack-plugin": "2.30.1",
        "istanbul-instrumenter-loader": "3.0.0",
        "karma": "1.7.1",
        "karma-chrome-launcher": "2.2.0",
        "karma-coverage": "1.1.1",
        "karma-coverage-istanbul-reporter": "1.3.0",
        "karma-jasmine": "1.1.0",
        "karma-jasmine-html-reporter": "0.2.2",
        "karma-mocha-reporter": "2.2.5",
        "karma-remap-coverage": "0.1.4",
        "karma-sourcemap-loader": "0.3.7",
        "karma-webpack": "2.0.6",
        "ng-router-loader": "2.1.0",
        "node-sass": "4.7.1",
        "protractor": "5.2.0",
        "raw-loader": "0.5.1",
        "sass-loader": "6.0.6",
        "source-map-loader": "0.2.3",
        "style-loader": "0.19.0",
        "to-string-loader": "1.1.5",
        "ts-node": "3.3.0",
        "tslib": "1.8.0",
        "tslint": "5.8.0",
        "typescript": "2.6.1",
        "uglifyjs-webpack-plugin": "1.0.1",
        "url-loader": "0.6.2",
        "webpack": "3.8.1",
        "webpack-bundle-analyzer": "2.9.1",
        "webpack-hot-middleware": "2.20.0",
        "webpack-merge": "4.1.1"
    }
}

Environment

Angular CLI: 1.5.2
Node: 8.3.0
OS: linux x64
Angular: 5.0.2
Nicolle answered 22/11, 2017 at 16:10 Comment(4)
is that all the code of AppService? Does this service has dependencies injected in its constructor?Graphy
@Graphy AppService is absolutely empty and doesn't containt any code inside, thats all he hasNicolle
Would you mind putting BrowserModule at the top of your imports and check if that makes a difference?Graphy
@Graphy ofc, i did, but result is the sameNicolle
N
18

Problem was solved by adding Inject directive to Service in AppComponent

constructor(@Inject(AppService) service: AppService)
Nicolle answered 23/11, 2017 at 8:57 Comment(2)
This works for me. Without Inject, the app is working fine. However, with ng test, it seems Inject is required. I am using Angular 6Ozuna
Exactly the same situation in Angular 7: https://mcmap.net/q/752979/-angular-unit-testing-a-component-that-creates-a-service-via-an-interface/220545Blackburn
H
4

After 1 day on this I realized I had removed some polyfills I didn't think I needed.

This is what I was missing:

import 'core-js/es7/reflect';

See https://github.com/angular/angular/issues/19417#issuecomment-359722822 for more info.

Harebell answered 15/11, 2018 at 13:3 Comment(0)
B
2

Try to put semicolon after this line in your app.module.shared.ts:

import { AppService } from './app.service' 

we can get this error for various reasons like: Syntax error, duplicate comma, or missing a semicolon

Bushwhack answered 22/11, 2017 at 16:16 Comment(1)
hello @Aakriti , thanx for reply, i put semicolon , but the result is the sameNicolle
C
2

Your service should be refered from a relative path as below,

I think you need

import { AppService } from '.app/app.service';

instead of

import { AppService } from '@app/app.service';
Cot answered 22/11, 2017 at 16:17 Comment(1)
,thanks for reply, i think you're missing / (slash) before .(dot) :) but not, this option did not helpNicolle
S
1

Please make sure that you have 'emitDecoratorMetadata' set to true as below in tsconfig.js

{
  "compileOnSave": false,
  "compilerOptions": {
    "emitDecoratorMetadata": true
  }
}

This will resolve your issue.

Servitude answered 21/2, 2022 at 7:36 Comment(0)
A
0

In my case I was trying to use:

constructor(private location: Location) { }

But you have to be aware there's an interface and there's a library with the same name. The interface was my problem since I needed the library, so I chose carefully again among the options and picked the library wich automatically added:

import { Location } from '@angular/common';

And problem solved....

Anisomerous answered 30/1, 2021 at 21:45 Comment(0)
I
-1

try this one. it works for me, seems polyfill problem.

<script src="node_modules/core-js/client/shim.min.js"></script> https://github.com/angular/angular/issues/19417

Icj answered 14/3, 2018 at 9:54 Comment(2)
It would be helpful if you provide more information on your solutionTybi
use npm install core-js and include shim.min.js file in browserIcj
D
-1

I have resolved this bug, import 'core-js', before all modules. However, I had a Hybrid app;

Disassociate answered 31/1, 2019 at 10:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.