Angular 2 Can't resolve all parameters for abcService - angularjs

Getting the following error message when trying to inject a config object into a service:
Error: Can't resolve all parameters for abcService: (?, [object Object]).
I followed the documentation on dependency injection, but can't get it working.
In my service abc.service.ts I have the following:
import { IabcConfig } from 'abc.types';
export let ABC_CONFIG = new OpaqueToken('abc.config');
#Injectable()
export class abcService {
private config: IabcConfig;
constructor(#Inject(ABC_CONFIG) config: IabcConfig, private http: Http) {
this.config = config;
}
}
and in my module abc.module.ts:
import { NgModule, ModuleWithProviders } from '#angular/core';
import { CommonModule } from '#angular/common';
import { IabcConfig } from '#abc/abc.types';
import { abcService, ABC_CONFIG } from '#abc/abc.service';
export const abcConfig: IabcConfig = {
save: false,
title: 'ABC',
}
#NgModule({
imports: [
CommonModule
]
})
export class ABCModule {
static forRoot(): ModuleWithProviders {
return {
ngModule: SharedModule,
providers: [
o7AuthenticationService,
{ provide: ABC_CONFIG, useValue: abcConfig },
]
};
}
}
Important libs I am using:
angular#2.4.0
angular2-universal#2.1.0-rc.1
zone.js#0.7.4

Add import for Angular's HttpModule in ABCModule.
....
#NgModule({
imports: [
CommonModule,
HttpModule
]
})
...
Update: probably I misunderstood original code, but I still assume that angular cannot find/resolve HTTP that you use in AbcService's constructor. Unless you didn't provide piece of code for that...

So ends up that when using a custom npm package, with services that take injectable objects, at least in webpack.config.ts we had to add our custom package #abc under externals: includeClientPackages(/#angularclass|#angular|#abc)
This fixed all the 'could not resolve parameter' errors

Related

Angular 9 - How Do I Downgrade Multiple Modules with Components?

I'm trying to group multiple Angular 9 Modules into a downgrade Module for AngularJS. I can get it to work fine for just one, but I'm having trouble getting it to work for multiple. I keep getting these errors:
With downgradedModule: specified
Error while instantiating component 'ExampleComponent': Unable to find the specified downgraded module.
Without downgradedModule: specified:
Error while instantiating component 'ExampleComponent': 'downgradedModule' not specified.
I've tried changed the value in downgradedModule to 'SharedModule', 'sharedModule', 'shared-module', and SharedModule with no luck. I'm not sure what else to do here.
Note: All of the manual bootstrapping is done. Not including it in my code samples because that part is working fine. Like I said, this works fine when I only downgrade one module
ngx.module.ts:
import { platformBrowserDynamic } from '#angular/platform-browser-dynamic';
import { downgradeModule, downgradeComponent } from '#angular/upgrade/static';
import { SharedModule } from './shared/shared.module';
import { ViewerModule } from './viewer/viewer.module';
import { ExampleComponent } from './shared/example/example.component';
const sharedModule = (extraProviders) => platformBrowserDynamic(extraProviders).bootstrapModule(SharedModule);
const viewerModule = (extraProviders) => platformBrowserDynamic(extraProviders).bootstrapModule(ViewerModule);
export const NgxModule = angular
.module('ngx', [
downgradeModule(viewerModule),
downgradeModule(sharedModule)
])
.directive('exampleComponent',
downgradeComponent({ component: ExampleComponent, downgradedModule: 'sharedModule' })) // error
.name;
shared.module.ts:
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { CommonModule } from '#angular/common';
import { ExampleComponent } from './example/example.component';
#NgModule({
imports: [
BrowserModule,
CommonModule
],
declarations: [
ExampleComponent
],
entryComponents: [
ExampleComponent
]
})
export class SharedModule {
constructor() { }
ngDoBootstrap() {
}
}
viewer.module.ts:
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { CommonModule } from '#angular/common';
#NgModule({
imports: [
BrowserModule,
CommonModule
],
declarations: [
PdfComponent
],
entryComponents: [
PdfComponent
]
})
export class ViewerModule {
constructor() { }
ngDoBootstrap() {
}
}
I was able to work on this over the weekend and found my solution from reading the documentation a little more closely:
downgradedModule?: string: The name of the downgraded module (if any) that the component "belongs
to", as returned by a call to downgradeModule(). It is the module,
whose corresponding Angular module will be bootstrapped, when the
component needs to be instantiated.
Source: https://angular.io/api/upgrade/static/downgradeComponent
One pitfall others might run into is that you can't call downgradeModule() directly when assigning it to the downgradedModule property. You have to downgrade it first and store it in a variable first, then pass it in.
const ng2BootstrapFn = (extraProviders) => platformBrowserDynamic(extraProviders)
.bootstrapModule(SharedModule);
const downgradedSharedModule = downgradeModule(ng2BootstrapFn);
export const NgxModule= angular
.module('ngx', [downgradedSharedModule ])
.directive('exampleComponent', downgradeComponent({
component: ExampleComponent,
downgradedModule: downgradedSharedModule }))
.name;

How do I inject an AngularJS service in an Angular 5 component?

I'm migrating an AngularJS project to Angular 5 using ngUpgrade but I'm running into a problem when trying to inject an AngularJS service in one of my new components.
I followed Angular's upgrade guide and created a serviceProvider that uses the $injector (see code below) but I keep getting this error:
core.js:1449 ERROR Error: Uncaught (in promise): Error: Trying to get the AngularJS injector before it being set.
I suspect I need to use forwardRef somewhere to fix this, but I'm unable to find out how and where (and why).
Following the example of the upgrade guide I created a serviceProvider as follows:
ajs-upgraded-providers.ts:
// The AngularJS service I want to use
import { AuthenticationService } from '../ajs/services/authentication.service';
export function authenticationServiceFactory($injector) {
return $injector.get('authentication');
}
export const authenticationServiceProvider = {
provide: AuthenticationService,
useFactory: authenticationServiceFactory,
deps: ['$injector']
};
Then I provide that to the app's NgModule:
app.module.ts:
#NgModule({
declarations: [
AppComponent
],
imports: [
BrowserModule,
UpgradeModule,
],
providers: [
authenticationServiceProvider,
],
bootstrap: [
AppComponent,
],
})
export class AppModule {
}
I bootstrap that module using ngUpgrade:
main.ts
import { platformBrowserDynamic } from '#angular/platform-browser-dynamic';
import { UpgradeModule } from '#angular/upgrade/static';
import { AppModule } from './app/app.module';
import { environment } from './environments/environment';
// Import our AngularJS module
import '../ajs/app';
platformBrowserDynamic()
.bootstrapModule(AppModule)
.then(platformRef => {
// Use the upgrade module to bootstrap the hybrid
const upgrade = platformRef.injector.get(UpgradeModule) as UpgradeModule;
upgrade.bootstrap(document.documentElement, ['myAngularJSApp']);
});
If I understand correctly, this should allow me to directly inject the AngularJS service into my component like so:
login.component.ts
import { Component } from '#angular/core';
import { AuthenticationService } from '../ajs/services/authentication.service';
#Component({
selector: 'my-login-page',
templateUrl: './login-page.component.html'
})
export class LoginPageComponent {
constructor(private authenticationService: AuthenticationService) {
console.log('authentication', authenticationService);
}
}
Can I do anything to simplify this even more? I tried to follow the upgrade guide as closely as possible, so why doesn't this work?
You need to add
providers: [AuthenticationService]
in Component declaration, like this:
import { Component } from '#angular/core';
import { AuthenticationService } from '../ajs/services/authentication.service';
#Component({
selector: 'my-login-page',
templateUrl: './login-page.component.html',
providers: [AuthenticationService]
})
export class LoginPageComponent {
constructor(private authenticationService: AuthenticationService) {
console.log('authentication', authenticationService);
}
}

Error in dependency injection when Angular component is downgraded to angularjs

I am in the process of upgrading angularjs app to angular 5 as outlined in this guide
I have the hybrid app bootstrapped from angular code, that went well. As a next step I created an angular5 component which depends on an angular5 service. I have downgraded the component and declared as directive in angularjs. The problem I see is the service doesn't get injected into the component. If I remove the service dependency from the component it works fine.
Here is my code and the error
Component
#Component({
selector: 'test-detail',
template: `
<h2>Windstorm details! {{test}}</h2>
<div><label>id: </label>1</div>
`
})
export class TestComponent {
private test:string;
constructor( private testService:TestService){
}
ngOnInit(){
this.test = this.testService.test();
}
}
Service:
import { Injectable } from '#angular/core';
#Injectable()
export class TestService{
test(){
return "hello";
}
}
NG5 Module
import { BrowserModule } from '#angular/platform-browser';
import { NgModule } from '#angular/core';
import { UpgradeModule } from '#angular/upgrade/static';
import {TestComponent} from '../ng5/directives/test.directive';
import {TestService} from '../ng5/services/test.service';
import { HttpClientModule } from '#angular/common/http';
#NgModule({
imports: [
BrowserModule,
UpgradeModule,
HttpClientModule
],
declarations:[
TestComponent
] ,
entryComponents: [
TestComponent
],
providers:[
TestService
]
})
export class AppModule {
constructor(private upgrade: UpgradeModule) {
}
ngDoBootstrap() {
this.upgrade.bootstrap(document.documentElement, ['MyApp']);
}
}
AngularJS Module:
angular.module("MyApp").directive('testDetail', downgradeComponent({ component: TestComponent }) as angular.IDirectiveFactory);
The error I get when launching the page is
uncaught Error: Can't resolve all parameters for TestComponent: (?).
at syntaxError (compiler.js:485)
at CompileMetadataResolver.webpackJsonp.529.CompileMetadataResolver._getDependenciesMetadata (compiler.js:15699)
at CompileMetadataResolver.webpackJsonp.529.CompileMetadataResolver._getTypeMetadata (compiler.js:15534)
at CompileMetadataResolver.webpackJsonp.529.CompileMetadataResolver.getNonNormalizedDirectiveMetadata (compiler.js:15019)
at CompileMetadataResolver.webpackJsonp.529.CompileMetadataResolver._getEntryComponentMetadata (compiler.js:15847)
at compiler.js:15317
at Array.map (<anonymous>)
at CompileMetadataResolver.webpackJsonp.529.CompileMetadataResolver.getNgModuleMetadata (compiler.js:15317)
at JitCompiler.webpackJsonp.529.JitCompiler._loadModules (compiler.js:34404)
at JitCompiler.webpackJsonp.529.JitCompiler._compileModuleAndComponents (compiler.js:34365)
I am answering my own question. Declaring service the following way resolved the issue
providers:[
{ provide: 'TestService', useClass: TestService }
]

Error encountered resolving symbol values statically. Calling function 'ɵmakeDecorator', function calls are not supported

I have made a custom "angular component library". I call this library ACL
I am importing the library into another project. When I run
ionic-app-scripts build, it builds successfully
BUT, when I run
ionic-app-scripts build --prod
it gives me following error message. any one with any clue.
Error encountered resolving symbol values statically. Calling function 'ɵmakeDecorator', function calls are not supported.
Consider replacing the function or lambda with a reference to an exported function, resolving symbol NgModule in C:/Monsenso-Source/administration/node_modules/#monsenso/components/node_modules/#angular/core/core.d.ts, resolving symbol WebModule in C:/Monsenso-Source/administration/node_modules/#monsenso/components/dist/web.module.d.ts, resolving symbol WebModule in C:/Monsenso-Source/administration/node_modules/#monsenso/components/dist/web.module.d.ts
at Error (native)
at syntaxError (C:\Monsenso-Source\administration\node_modules\#angular\compiler\bundles\compiler.umd.js:1550:34)
at simplifyInContext (C:\Monsenso-Source\administration\node_modules\#angular\compiler\bundles\compiler.umd.js:23870:23)
at StaticReflector.simplify (C:\Monsenso-Source\administration\node_modules\#angular\compiler\bundles\compiler.umd.js:23882:13)
at StaticReflector.annotations (C:\Monsenso-Source\administration\node_modules\#angular\compiler\bundles\compiler.umd.js:23331:41)
at NgModuleResolver.resolve (C:\Monsenso-Source\administration\node_modules\#angular\compiler\bundles\compiler.umd.js:13883:70)
at CompileMetadataResolver.getNgModuleMetadata (C:\Monsenso-Source\administration\node_modules\#angular\compiler\bundles\compiler.umd.js:14473:60)
at addNgModule (C:\Monsenso-Source\administration\node_modules\#angular\compiler\bundles\compiler.umd.js:23050:58)
at C:\Monsenso-Source\administration\node_modules\#angular\compiler\bundles\compiler.umd.js:23061:14
at Array.forEach (native)
error Command failed with exit code 1.
The same error is also coming, if I execute the following command:
"node_modules/.bin/ngc" -p tsconfig-aot.json
Code for the ACL
page-template-middle.component.html
<ion-content padding>
<ion-grid>
<ion-row>
<ion-col col-4>
<!-- empty -->
</ion-col>
<ion-col col-4>
<ng-content></ng-content>
</ion-col>
<ion-col col-4>
<!-- empty -->
</ion-col>
</ion-row>
</ion-grid>
</ion-content>
page-template-middle.component.ts
import { Component, OnInit } from '#angular/core';
#Component({
selector: 'mon-page-template-middle',
templateUrl: './page-template-middle.component.html'
})
export class PageTemplateMiddleComponent {
}
web.module.ts
import { NgModule } from '#angular/core';
import { BrowserModule } from '#angular/platform-browser';
import { FormsModule } from '#angular/forms';
import { HttpModule } from '#angular/http';
import { IonicApp, IonicModule } from 'ionic-angular';
import {
PageTemplateMiddleComponent
} from './components/page-template-middle/page-template-middle.component';
#NgModule({
declarations: [
PageTemplateMiddleComponent
],
exports: [
PageTemplateMiddleComponent
],
imports: [
BrowserModule,
FormsModule,
HttpModule,
IonicModule
],
providers: [
]
})
export class WebModule {
}
Code for the consumer project
app.module.ts
import { BrowserModule } from '#angular/platform-browser';
import { ErrorHandler, NgModule } from '#angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { WebModule } from '#monsenso/components';
import { MyAppComponent } from './app.component';
import { HomePageComponent } from '../pages/home/home';
#NgModule({
bootstrap: [IonicApp],
declarations: [
MyAppComponent,
HomePageComponent
],
entryComponents: [
MyAppComponent,
HomePageComponent
],
imports: [
BrowserModule,
WebModule,
IonicModule.forRoot(MyAppComponent)
],
providers: [
{provide: ErrorHandler, useClass: IonicErrorHandler}
]
})
export class AppModule {
}
I can't make any statements about the code you're working on because you haven't pasted any in your question. But in general, this error relates to Angular's AOT compiler being unable to resolve argument "types" in anonymous functions that aren't exported from your module.
For example, let's say I have a factory function that I want to use to provide a service for use in some of my components.
import { NgModule } from '#angular/core';
import { MyService } from './my-service';
#NgModule({
providers: [{
provide: MyService,
useFactory: () => {
return new MyService();
},
}],
})
export class MyModule {}
In that example you can see an anonymous (unnamed) function being used as my factory function. This will work fine if you're not compiling AOT, but will otherwise break.
To fix this, the function needs to be exported from your module ("module" in the Typescript sense).
import { NgModule } from '#angular/core';
import { MyService } from './my-service';
export function myServiceFactory() {
return new MyService();
}
#NgModule({
providers: [{
provide: MyService,
useFactory: myServiceFactory,
}],
})
export class MyModule {}
I can't say that your error is exactly related to your ACL Angular Module using a factory function. But the error you've pasted in your question is absolutely related to Angular not being able to statically analyze a function because it hasn't been exported.

Can't resolve all parameters for Auth: (?) error, ionic2 AngularJS

i wanted to provide authonification to my ionic2 app, so i followed the official documentation and applied it to a brand new ionic2 app ionic start my_app blank
once i serve my app, this error fires :
Error: Can't resolve all parameters for Auth: (?).
at syntaxError (http://localhost:8100/build/main.js:107898:34)
at CompileMetadataResolver._getDependenciesMetadata (http://localhost:8100/build/main.js:121235:35)
at CompileMetadataResolver._getTypeMetadata (http://localhost:8100/build/main.js:121103:26)
at CompileMetadataResolver._getInjectableMetadata (http://localhost:8100/build/main.js:121089:21)
at CompileMetadataResolver.getProviderMetadata (http://localhost:8100/build/main.js:121379:40)
at http://localhost:8100/build/main.js:121308:49
at Array.forEach (<anonymous>)
at CompileMetadataResolver._getProvidersMetadata (http://localhost:8100/build/main.js:121269:19)
at CompileMetadataResolver.getNgModuleMetadata (http://localhost:8100/build/main.js:120924:50)
at JitCompiler._loadModules (http://localhost:8100/build/main.js:131988:66)
i've injected Auth with providers in app.moodule.ts as you can
import { BrowserModule } from '#angular/platform-browser';
import { ErrorHandler, NgModule } from '#angular/core';
import { IonicApp, IonicErrorHandler, IonicModule } from 'ionic-angular';
import { SplashScreen } from '#ionic-native/splash-screen';
import { StatusBar } from '#ionic-native/status-bar';
import { MyApp } from './app.component';
import { Auth } from '#ionic/cloud-angular';
#NgModule({
declarations: [
MyApp,
],
imports: [
BrowserModule,
IonicModule.forRoot(MyApp)
],
bootstrap: [IonicApp],
entryComponents: [
MyApp,
],
providers: [
StatusBar,
SplashScreen,
Auth,
{provide: ErrorHandler, useClass: IonicErrorHandler }
]
})
export class AppModule {}
according to my researches, this error comes from:
Having forgot to decorate the Auth class with #Injectable() or #Inject(Auth)
i have tried doing so by injecting :
#IonicPage()
#Component({
selector: 'page-home',
templateUrl: 'home.html',
})
#Injectable()
export class HomePage {........}
OR
export class HomePage {
constructor(#Inject(Auth) public auth: Auth) {
}
.......
}
But this shows the same error though.
Having a dependency in the Auth constructor that i haven't provided in app.module.ts
as you can see, i've handled this case in my code.
A circular dependency.
which i really think is the issue here, honestly i got a clue how to check this out since i still a beginner.
Hope someone can help

Resources