I cannot instantiate class with parameters compiling with Aot

Asked

Viewed 480 times

1

Versions.

TUDO ATUALIZADO NA DATA DE HOJE (2017-08-23)
@angular/cli: 1.3.1
node: 8.3.0
os: win32 x64
@angular/animations: 4.3.5
@angular/common: 4.3.5
@angular/compiler: 4.3.5
@angular/core: 4.3.5
@angular/forms: 4.3.5
@angular/http: 4.3.5
@angular/platform-browser: 4.3.5
@angular/platform-browser-dynamic: 4.3.5
@angular/router: 4.3.5
@angular/cli: 1.3.1
@angular/compiler-cli: 4.3.5
@angular/language-service: 4.3.5

Method 1 WITH parameters - Compile WITH ERROR using

ng build --prod ( with aot )

and WITHOUT ERROR when compiling using

ng build --prod --aot=false (sem aot)

in service:

@Injectable()
export class MvsTest {
    public Test: string = "";
    constructor(param: string) { 
        this.Test = param; 
    }
}

in the other service

@Injectable()
export class ApplicationService {
    public test1 = new MvsTest ('New1');
    public test2 = new MvsTest ('New2');
    constructor() { }
}

Method 2 NO parameter- Compile WITHOUT ERROR using ng build --prod ( with Aot ) and WITHOUT ERROR when compiling using ng build --prod --aot=false (Aot-free)

in service:

@Injectable()
export class MvsTest {
    public Test: string = "";
    constructor() { }
}

in the other service

@Injectable()
export class ApplicationService {
    public test1 = new MvsTest ();
    public test2 = new MvsTest ();
    constructor() {
        this.test1.Test= 'New1';
        this.test2.Test= 'New2';
    }
}

error log.

ERROR in Can't resolve all parameters for MvsTest in C:/DEVELOPER/_SHARED/mvs-test/mvs-test.service.ts: (?).
    ERROR in ./src/main.ts..... etc...etc...etc... 
    ERROR in Can't resolve all parameters for MvsTest in C:/DEVELOPER/src/app/_SHARED/mvs-test/mvs-test.service.ts: (?).
    ERROR in ./src/main.ts
    Module not found: Error: Can't resolve './$$_gendir/app/app.module.ngfactory' in 'C:\DEVELOPER\src'
    resolve './$$_gendir/app/app.module.ngfactory' in 'C:\DEVELOPER\src'
      using description file: C:\DEVELOPER\package.json (relative path: ./src)
        Field 'browser' doesn't contain a valid alias configuration
      after using description file: C:\DEVELOPER\package.json (relative path: ./src)
        using description file: C:\DEVELOPER\package.json (relative path: ./src/$$_gendir/app/app.module.ngfactory)
          no extension
            Field 'browser' doesn't contain a valid alias configuration
            C:\DEVELOPER\src\$$_gendir\app\app.module.ngfactory doesn't exist
          .ts
            Field 'browser' doesn't contain a valid alias configuration
            C:\DEVELOPER\src\$$_gendir\app\app.module.ngfactory.ts doesn't exist
          .js
            Field 'browser' doesn't contain a valid alias configuration
            C:\DEVELOPER\src\$$_gendir\app\app.module.ngfactory.js doesn't exist
          as directory
            C:\DEVELOPER\src\$$_gendir\app\app.module.ngfactory doesn't exist
    [C:\DEVELOPER\src\$$_gendir\app\app.module.ngfactory]
    [C:\DEVELOPER\src\$$_gendir\app\app.module.ngfactory.ts]
    [C:\DEVELOPER\src\$$_gendir\app\app.module.ngfactory.js]
    [C:\DEVELOPER\src\$$_gendir\app\app.module.ngfactory]
     @ ./src/main.ts 3:0-74
     @ multi ./src/main.ts

Desired functionality.

I need to instantiate the class with parameters because it is more difficult to have to instantiate without the parameter and then set the parameter. When we create many inherited classes the code doubles in size if you have to create and then set. There will be many instances and I want the code very clean. I want to compile with Aot.

I know I can create and set the property later but I would really like a solution where I can instantiate the class already with the parameter.

But... it doesn’t solve what I really need but it provisionally exists as I create something like this hypothetical example?:

public test1 = new MvsTest().Test = 'Test1';  // **(hypothetical example)**

Thank you very much

  • I suggest to open an Issue in the official Change of the angular. I had problem with AOT too and I went to open it.

  • Okay, thank you Giovane

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.