【问题标题】:angular 2 lazy loading - routes from server角度 2 延迟加载 - 来自服务器的路由
【发布时间】:2016-10-11 17:15:56
【问题描述】:

我一直在使用延迟加载进行 Angular 2 项目。它运行良好,但我需要从服务器获取模块名称,然后创建路由,但它不起作用。

这是我所拥有的:

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

function getRoutes(): Routes{
  let x: any = [
         {path: '', redirectTo: 'welcome', pathMatch: 'full'}, 
         {path: 'backend', loadChildren: 'app/backend/backend.module'}
      ]  
  return x;
}

export const routes: Routes = routess();

export const routing = RouterModule.forRoot(routes);

这是,我需要什么:

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

function getRoutes(): Routes{
 let x: any;

 $.get( "api/getRoutes", function( data ) {
   x = data; //object array from server
 }); 
  return x;
}

export const routes: Routes = routess();

export const routing = RouterModule.forRoot(routes);

问题是,函数getRoutes 没有等待服务器结果并返回空数据。

有什么方法可以等待服务器数据,然后再往路由中添加数据?

【问题讨论】:

    标签: ajax angular angular2-routing


    【解决方案1】:

    仅将NgModule 用于基本路由设置(/welcome 等),然后在应用程序的其他位置加载路由并更新路由器配置。然后你可以使用resetConfig():

    this.http.get('/api/getRoutes')
      .subscribe(config => this.router.resetConfig(config))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-04-03
      • 2018-04-23
      • 1970-01-01
      • 2020-07-12
      • 1970-01-01
      • 1970-01-01
      • 2015-03-28
      相关资源
      最近更新 更多