0

I've got a site that I'm in the middle of converting from Angular 2 to Angular 5. What I've got a question on is how is the preload in Angular 5 now handled in comparison. For example the code on the old angular 2 code was :

       .state('data', {
        url: "/data",
        templateUrl: "data/data.tpl.html",*emphasized text*
        resolve: {
            preLoad:['dataService', function(service) {
                return service.primeData();
            }]
        },
        caption: "Search",
        navOrder: 1

My question is how does the preload: above get handled now on Angular 5. Thank you for any comments or help with this.

1

2 Answers 2

1

To preload your module/service in Angular 5, you can do it through route config:

app-routing.module.ts

import { NgModule }     from '@angular/core';
import {
  RouterModule, Routes,
} from '@angular/router';

import { DataComponent } from './data.component';
import { PageNotFoundComponent }   from './not-found.component';


const appRoutes: Routes = [
  {
    path: 'data',
    loadChildren: 'app/data/data.module#DataModule'
  },
  { path: '',   redirectTo: '/heroes', pathMatch: 'full' },
  { path: '**', component: PageNotFoundComponent }
];

@NgModule({
  imports: [
    RouterModule.forRoot(
      appRoutes,
    )
  ],
  exports: [
    RouterModule
  ]
})
export class AppRoutingModule {}

You can enable preload all modules (or add your own custom preloading):

RouterModule.forRoot(
  appRoutes,
  {
    enableTracing: true, // <-- debugging purposes only
    preloadingStrategy: PreloadAllModules
  }
)
Sign up to request clarification or add additional context in comments.

4 Comments

quick question. In the case of custom preloading how would I achieve that. In my example I return service.primeData();?
I would call "service.primeData()" to get the data in the Data module
Also not all of the components will have preload The route I show does have it but others don't
@yams have a look at custom preloading
0
+50

app.router.ts

import { NgModule ,ModuleWithProviders } from '@angular/core';

import { RouterModule, Routes } from '@angular/router';

import { dataService } from './service/dataService';


const router: Routes = [
   { path: 'DataService, redirectTo: '/dashboard', pathMatch: 'full' },

   { 
      path: 'detail/:id', 
      component: HeroDetailComponent,
      resolve: {
        data: dataService 
      },
  }

];
export const routes: ModuleWithProviders = RouterModule.forRoot(router);

app.module.ts

import { BrowserModule } from '@angular/platform-browser';

import { NgModule , APP_INITIALIZER    } from '@angular/core';

import {routes} from './app.router';

import { dataService } from './service/dataService';

@NgModule({

   declarations: [

      your component here 
   ],

   imports: [

      routes,

   ],

   providers: [

      dataService 

   ]

})
export class AppModule { }

dataService.ts

import { Injectable } from '@angular/core';

import { Router, Resolve, ActivatedRouteSnapshot} from '@angular/router';

@Injectable()

export class dataService implements Resolve {

   constructor(private heroService: HeroService, private router: Router) { }

   resolve(route: ActivatedRouteSnapshot): Promise | boolean {

      let id = route.params['id'];

      let result = this.check_id(id);

      if ( result ) {

          return result ;

      } else { // id not found

          this.router.navigate(['/dashboard']);

          return false;
      }

   }

 check_id(id){

     // check somthing return somthing or false

    return true;
 }
}

1 Comment

Promise requires the use of a parameter in the definition :interface Promise<T> { readonly [Symbol.toStringTag]: "Promise"; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.