Skip to content

Router resolves undefined values when building with "ng build --prod" #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Humberd opened this issue Mar 17, 2017 · 4 comments
Closed

Comments

@Humberd
Copy link

Humberd commented Mar 17, 2017

What is wrong?

I'm using @angular/cli and when I build a project using ng build --prod all the values that I resolve in all the states are returning undefined values.

When I build using ng serve or ng build everything works fine.

Steps to recreate

I created this repo that shows the problem.

I created a home component:

@Component({
  moduleId: module.id,
  template: `Hello: {{foo}}`
})
export class HomeComponent implements OnInit {
  @Input() foo: string;

  constructor() {
  }

  ngOnInit() {
  }
}

In the app.module I created a default home state for that component:

export function resolveFn() {
  return () => {
    return "bar";
  }
}

let state: Ng2StateDeclaration = {
  name: "home",
  url: "/home",
  component: HomeComponent,
  resolve: [{
    token: "foo",
    resolveFn: resolveFn
  }],

};

@NgModule({
  declarations: [
    AppComponent,
    HomeComponent
  ],
  imports: [
    BrowserModule,
    FormsModule,
    HttpModule,
    UIRouterModule.forRoot({
      states: [state],
      otherwise: "home"
    })
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {
}

In the app.component.html I added ui-view directive:

<h1>
  APP COMPONENT
</h1>
<div ui-view></div>

How does it look using ng serve

image

How does it look using ng build --prod

image

@marshall007
Copy link

@Humberd see #36. It's fixed in 1.0.0-beta.5 which hasn't been released yet. You'll need to use @ResolveData() instead of @Input() to decorate your input.

@aitboudad
Copy link
Contributor

or just wait for angular 4 no need to use @ResolveData(), the only workaround right now is to downgrade the @ngtools/webpack version

@Humberd
Copy link
Author

Humberd commented Mar 20, 2017

I updated the @angular/cli to the newest version 1.0.0-rc.2 and I can inject the input to the constructor like this:

export class HomeComponent implements OnInit {
  constructor(@Inject("foo") private foo: string) {
  }

  ngOnInit() {
  }
}

@christopherthielen
Copy link
Member

This should be working in latest ui-router using @Input() foo: string;, although injecting is also fine.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants