ui-router icon indicating copy to clipboard operation
ui-router copied to clipboard

Trying new @UIRouterModule...

Open kolkov opened this issue 10 years ago • 24 comments

Hi! I cloned ui-router into local folder, and built it with npm run build Then I removed old ui-router-ng2 content in the node_modules folder and copied new content from build_packages\ng2. Then I changed app.module.ts to:

var homeState = { name: 'home', url: '/home', component: HomeComponent };
var aboutState = { name: 'about', url: '/about', component: AboutComponent };

@UIRouterModule({
   imports: [BrowserModule],
   declarations: [AppComponent],
   states: [homeState, aboutState]
 }) 
export class AppModule {};

and got a error: image Whats wrong?

kolkov avatar Aug 31 '16 11:08 kolkov

After I added traceur manually: image

kolkov avatar Aug 31 '16 12:08 kolkov

I suspect you're using systemjs? I saw this in quickstart-ng2 where SystemJS incorrectly detects ui-router-ng2.js as an ES6 module that needs transpiling. I believe it's parsing something from one of the block comments in the unminified file.

Try one of these things:

  • Point systemjs to the ui-router-ng2.min.js minified file
  • Add a meta: block that says the bundle is cjs https://github.com/ui-router/quickstart-ng2/blob/rc5compat/systemjs.config.js#L54

christopherthielen avatar Aug 31 '16 15:08 christopherthielen

Thanks! That worked! But got another error:

image

@UIRouterModule
({
  imports: [
    BrowserModule,
  ],
  declarations: [
   AppComponent
  ],
  states: [],
  providers: [
    {provide: LocationStrategy, useClass: HashLocationStrategy},
    {provide: UIRouterConfig, useClass: MyUIRouterConfig},

  ],
  bootstrap: [ UIView ]
})

kolkov avatar Aug 31 '16 17:08 kolkov

you might need to declare it as an entryComponent

christopherthielen avatar Aug 31 '16 17:08 christopherthielen

Ho can I do this?

kolkov avatar Aug 31 '16 17:08 kolkov

@UIRouterModule
({
  imports: [
    BrowserModule,
  ],
  declarations: [
   AppComponent
  ],
  entryComponents: [
    AppComponent
  ],
  states: [],
  providers: [
    {provide: LocationStrategy, useClass: HashLocationStrategy},
    {provide: UIRouterConfig, useClass: MyUIRouterConfig},

  ],
  bootstrap: [ UIView ]
})

christopherthielen avatar Aug 31 '16 17:08 christopherthielen

Also note: @UIRouterModule is a shortcut for creating a NgModule manually:

https://github.com/angular-ui/ui-router/blob/f6741516a43a605ed7f8f3d1d96517e7e46ef54d/src/ng2/uiRouterNgModule.ts#L50-L66

christopherthielen avatar Aug 31 '16 17:08 christopherthielen

@christopherthielen What is the official latest release of ui-router-ng2 for npm in order to start using @UIRouterModule? I am updating to RC5. Right now I have "ui-router-ng2": "1.0.0-alpha.5" in my package.json

thegrid22593 avatar Aug 31 '16 17:08 thegrid22593

@christopherthielen Yes! Thats helped! And one more error from second module:

image

kolkov avatar Aug 31 '16 18:08 kolkov

@christopherthielen I fixed it by removing undefined states

uiSref="app.public.user.signup"
uiSref="app.public.user.login"

Now all working perfect!

kolkov avatar Aug 31 '16 18:08 kolkov

@kolkov What is the latest release of ui-router to work with angular 2 rc5? Also, where are you importing UIRouterModule from?

thegrid22593 avatar Aug 31 '16 18:08 thegrid22593

@kolkov @christopherthielen nvm I found the quickstart and that is all up to date. Thanks all!

thegrid22593 avatar Aug 31 '16 18:08 thegrid22593

@thegrid22593 No, quickstart is not for UIRouterModule, it's for beta1. UIRouterModule is for future beta2. If you want you can try to build beta2 by yourself as described above.

kolkov avatar Aug 31 '16 18:08 kolkov

@thegrid22593 ui-router 1.0.0-beta.2 will be out shortly and contains the UIRouterModule code being discussed, and full NgModule support.

christopherthielen avatar Aug 31 '16 19:08 christopherthielen

@christopherthielen Great to hear! Thanks!

thegrid22593 avatar Aug 31 '16 19:08 thegrid22593

@christopherthielen What is preferable to use, UIRouterModule or NgModule?

kolkov avatar Aug 31 '16 19:08 kolkov

@kolkov UIRouterModule is easier to use (less boilerplate) but it's your own personal choice.

If you use raw NgModule make sure your module provides the states as UIROUTER_STATES_TOKEN with multi: true and also imports the UIRouterRootModule

christopherthielen avatar Aug 31 '16 19:08 christopherthielen

Thanks! Now we can describe routes for modules separately in the modules themselves? And we no longer need to use this construction in MyUIRouterConfig? INITIAL_STATES.forEach(state => uiRouter.stateRegistry.register(state));

kolkov avatar Aug 31 '16 19:08 kolkov

Now we can describe routes for modules separately in the modules themselves? And we no longer need to use this construction in MyUIRouterConfig? INITIAL_STATES.forEach(state => uiRouter.stateRegistry.register(state));

^ correct.

Check out the quickstart rc5 branch: https://github.com/ui-router/quickstart-ng2/tree/rc5compat/src

christopherthielen avatar Aug 31 '16 19:08 christopherthielen

And what now we do with this function? It is still necessary?

@Injectable()
export class MyUIRouterConfig {
  constructor(private injector: Injector) {}

  configure(uiRouter: UIRouter) {
    // Register each state definition (from app.states.ts) with the StateRegistry   

    // Define a default behavior, for when the URL matched no routes
    uiRouter.urlRouterProvider.otherwise(() => uiRouter.stateService.go("app.public.home", null, null) && null);
  }
}

And do we need here injector still?

kolkov avatar Aug 31 '16 19:08 kolkov