Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

UI-Router Hybrid Sample App: Route to Angular 1 and Angular 2 components

NotificationsYou must be signed in to change notification settings

ui-router/sample-app-angular-hybrid

Repository files navigation

You can run and edit this live in your browser usinghttps://stackblitz.com/github/ui-router/sample-app-angular-hybrid

This sample app is intended to demonstrate a non-trivial ui-router application.

  • Multiple sub-modules
  • Managed state lifecycle
  • Application data lifecycle
  • Authentication (simulated)
  • Authenticated and unauthenticated states
  • REST data retrieval (simulated)
  • Lazy loaded sub module (Contacts)

Running

npm installnpm start

Webpack without Angular-CLI

The sample app uses the Angular CLI.However, there is also abranch which demonstrates a custom webpack config) but no angular-cli.

Visualizer

We're using theState and Transition Visualizer to visually representthe current state tree, as well as the transitions between states. Explore how transitions work by hoveringover them, and clicking to expand details (params and resolves).

Note how states areentered when they were previously not active,exited and re-entered when parameters change,and how parent states whose parameters did not change areretained. Each of these (exited, entered, retained)correspond to a Transition Hook.

Structure

The application is written in Typescript and utilizes ES6 modules.We are loading and bundling modules using webpack.

There are many ways to structure a ui-router app.We aren't super opinionated on application structure.Use what works for you.We organized ours in the following way:

  • Sub-module (feature) organization
    • Each feature gets its own directory.
    • Features contain states and components
    • Specific types of helper code (directives, services, etc)used only within a feature may live in a subdirectorynamed after its type (/mymessages/directives)
  • Leveraging ES6 modules
    • Each state is defined in its own file
    • Each component (controller + template) is defined in its own file
    • Components exported themselves
    • Components are then imported into a states where they are composed into the state definition.
    • States export themselves
    • Thefeature.module.js imports all states for the feature and registers then with the$stateProvider
  • A single angular module is defined for the entire application
    • Created, then exported frombootstrap/ngmodule.js
    • The ng module is imported into some other module whenever services, config blocks, directives, etc needto be registered with angular.

UI-Router Patterns

  • Defining custom, app-specific global behaviors
    • Add metadata to a state, or state tree
    • Check for metadata in transition hooks
    • Example:routerhooks/redirectTo.js
      • If a transition directly to a state with aredirectTo property is started,the transition will be redirected to the state which the property names.
    • Example:routerhooks/authRequired.js
      • If a transition to a state with a truthydata.authRequired: true property is startedand the user is not currently authenticated
  • Defining a default substate for a top-level state
    • Example: declaringredirectTo: 'mymessages.folder' inmymessages/mymessages.states.js (mymessages state)
  • Defining a default parameter for a state
    • Example:folderId parameter defaults to 'inbox' inmymessages/mymessages.states.js (folder state)
  • Application data lifecycle
    • Data loading is managed by the state declaration, via theresolve: block
    • Data is fetched before the state isentered
    • Data is fetched according to state parameters
    • The state isentered when the data is ready
    • The resolved data is injected into the components
    • The resolve data remains loaded until the state is exited
  • Lazy Loading
    • The contacts module is an AngularNgModule. It is defined as a "future state", and lazy loaded just before any contacts state is activated.

About

UI-Router Hybrid Sample App: Route to Angular 1 and Angular 2 components

Resources

Stars

Watchers

Forks

Packages

No packages published

Contributors13


[8]ページ先頭

©2009-2025 Movatter.jp