GVKun编程网logo

angular – 在没有延迟加载的情况下路由到子路由模块(angular路由懒加载)

14

在这篇文章中,我们将为您详细介绍angular–在没有延迟加载的情况下路由到子路由模块的内容,并且讨论关于angular路由懒加载的相关问题。此外,我们还会涉及一些关于Angular10:为延迟加载的

在这篇文章中,我们将为您详细介绍angular – 在没有延迟加载的情况下路由到子路由模块的内容,并且讨论关于angular路由懒加载的相关问题。此外,我们还会涉及一些关于Angular 10:为延迟加载的模块创建捆绑包、Angular 2如何为延迟加载的模块提供单例服务、Angular 4延迟加载,命名路由器插座无法正常工作、Angular 4延迟加载和路由不起作用的知识,以帮助您更全面地了解这个主题。

本文目录一览:

angular – 在没有延迟加载的情况下路由到子路由模块(angular路由懒加载)

angular – 在没有延迟加载的情况下路由到子路由模块(angular路由懒加载)

我想拥有多个路由模块,以保持我的应用程序清洁和易于阅读.我目前使用延迟加载SubComponent,但我不想这样做.所以我正在寻找一种方法来改变它.无论如何,这是目前正在运行的代码.

我有以下两个路由文件.

APP-routing.module.ts:

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

const routes: Routes = [
  { path: '',component: HomeComponent },{ path: 'sub',loadChildren: './sub/sub.module#SubModule' }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],exports: [RouterModule]
})

子routing.module.ts:

import { NgModule } from '@angular/core';
import { Routes,component: SubComponent,children: [
    { path: 'new',component: SubeditComponent }
  ] },];

@NgModule({
  imports: [RouterModule.forChild(routes)],exports: [RouterModule]
})

它的工作方式很好,但我不想对这个SubComponent应用延迟加载.所以,理想情况下我想将app-routing.module.ts更改为:

import { NgModule } from '@angular/core';
import { Routes,component: SubComponent }
];

@NgModule({
  imports: [RouterModule.forRoot(routes)],exports: [RouterModule]
})

这将不起作用,并导致以下错误:

ERROR Error: Uncaught (in promise): Error: Cannot match any routes. URL Segment: 'sub/new'
Error: Cannot match any routes. URL Segment: 'sub/new'

SubComponent的大小会大幅增加,我不想因为自己的原因而应用延迟加载.所以无论如何,有没有办法在避免延迟加载的同时使用多个路由文件?

解决方法

您是否尝试过这样加载:

{path:’sub’,loadChildren :()=>子模块}

你可以找到更多细节here.

Angular 10:为延迟加载的模块创建捆绑包

Angular 10:为延迟加载的模块创建捆绑包

如何解决Angular 10:为延迟加载的模块创建捆绑包?

我有一个使用xslx库创建Excel报告的服务。由于xlsx很大,并且只能在一个地方使用,因此我想将服务以及依赖项放入延迟加载的模块中。

到目前为止,一切正常:我将模块懒惰地加载到需要的地方,然后从模块注入器获取服务。现在的问题是获取服务实例,我需要将其导入使用组件的组件中。一旦执行此操作,xlsx就会被捆绑到使用该服务的模块中,而不是捆绑到延迟加载的模块中。

有什么想法吗?

Thx 马库斯

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

Angular 2如何为延迟加载的模块提供单例服务

Angular 2如何为延迟加载的模块提供单例服务

根据我对Angular 2 rc5的理解,要从另一个模块(不是AppModule)提供服务作为单个模块提供给每个组件,即使是那些延迟加载的模块,我们也不会将该服务包含在该其他模块的providers数组中.我们改为使用RouterModule.forRoot()导出它并将结果导入AppModule

According to the docs:

The SharedModule should only provide the UserService when imported by
the root AppModule. The SharedModule.forRoot method helps us meet this
challenge…the SharedModule does not have providers…When we add
the SharedModule to the imports of the AppModule,we call forRoot. In
doing so,the AppModule gains the exported classes and the
SharedModule delivers the singleton UserService provider at the same
time

我真的很挣扎如何为延迟加载的路由提供第三方服务(我的AppModule的导入数组中的模块使用的服务).我无法控制这个第三方模块,因此我不能从该模块的NgModule.providers数组中删除该服务,并将其放在RouterModule.forRoot()中,就像我使用其中一个服务一样.

具体服务是MdIconRegistry,它是Angular Material 2 alpha 7-3的MdIconModule的提供者.此服务用于注册svg图标,然后可以使用< md-icon svgIcon ='iconName'>显示在页面上.标签.所以:

>我在我的根AppModule中导入了MdIconModule
>我使用有问题的服务在我的AppComponent中注册svg图标

该图标可见并且运行良好,但仅适用于启动时加载的模块.延迟加载的模块无法看到这些图标,因此我怀疑Angular注入器没有注入MdIconRegistry服务的同一个实例.

tl; dr:我如何从第三方模块使服务可用于我的延迟加载组件?

Here is a plunker that demonstrates the problem(用打字稿编码).

PS:这引起了MdIconModule开发人员on github.的注意

I do not think it has anything to do with the component being lazy-loaded.

LazyLoadedComponent不是AppModule的一部分 – 它是LazyModule的一部分.根据文档,组件只能是一个模块的一部分.如果您也尝试将LazyLoadedComponent添加到AppModule,则会收到相应的错误.所以LazyLoadedComponent甚至根本没有看到MdIconModule.您可以通过查看调试器中的模板输出来确认这一点 – 它保持不变.

<md-icon svgIcon="play"></md-icon>

解决方案似乎是将MdIconModule添加到LazyModule,虽然仅此一项不能解决问题,但它确实会在输出中添加错误.

Error retrieving icon: Error: Unable to find icon with the name “:play”

现在模板输出看起来像这样,所以我们知道它正在加载.

<md-icon role="img" svgicon="play" ng-reflect-svg-icon="play" aria-label="play"></md-icon>

我从LazyLoadedComponent添加了对addSvgIconSet的调用,并且它使它工作……所以这证明每个组件有一个MdIconRegistry服务的实例 – 不是你想要的,但可能指向正确的方向.

这是新款 – http://plnkr.co/edit/YDyJYu?p=preview

经过进一步审查,我在docs发现了这个:

Why is a service provided in a lazy loaded module visible only to that module?

Unlike providers of the modules loaded at launch,providers of lazy loaded modules are module-scoped.

最后更新!这是答案.没有为延迟加载的组件正确设置MdIconModule …但是我们可以轻松创建我们自己的模块,该模块已正确设置并使用它.

import { NgModule } from '@angular/core';
import { HttpModule } from '@angular/http';

import { MdIcon } from '@angular2-material/icon';
import { MdIconRegistry } from '@angular2-material/icon';

@NgModule({
  imports: [HttpModule],exports: [MdIcon],declarations: [MdIcon]
})
export class MdIconModuleWithProviders {
  static forRoot(): ModuleWithProviders {
    return {
      ngModule: MdIconModuleWithProviders,providers: [ MdIconRegistry ]
    };
  }
}

Plunk更新并完全正常工作. (对不起,更新了同一个) – > http://plnkr.co/edit/YDyJYu?p=preview

有人可能会提交拉取请求,以便Angular Material导出两种样式的模块.

Angular 4延迟加载,命名路由器插座无法正常工作

Angular 4延迟加载,命名路由器插座无法正常工作

我有一个延迟加载的问题,而不是路由到指定的路由器插座.有人可以看看我错在哪里吗?我有一个主页,其中有一个指向Product的产品 – >默认路由器插座和产品详细信息 – >命名路由器插座.
<div>
   <div><a [routerLink]="['product']"> Product </a> </div>
   <div><a [routerLink]="['productdetail',{outlets:{productdetail: 'detail'}}]"> Product Detail </a> </div>
  <div> <router-outlet></router-outlet></div>
  <div> <router-outlet name="detail"></router-outlet>
</div>

以下是plunker代码.

Plunker Code

这是已知的bug,你可以跟踪问题 here

The workaround or we can say solution to this issue is,use non-empty paths for your top
level routes if auxilary(i.e. named) routes exist in a lazy loaded module.

我能看到的唯一缺陷是,在路线中增加了一个额外的网址段

MainRoutingModule:顶级非空路径(即“路径:’加载’”)

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

import { MainpageComponent } from './mainpage.component';
import { ProductComponent } from './product.component';
import { ProductDetailComponent } from './productdetail.component';

const childroutes: Routes = [

 { path: 'load',component: MainpageComponent,children: [ 
      {path: 'product',component: ProductComponent
      {path: 'productdetail',component: ProductDetailComponent,outlet: 'detail' 
      },]
 },];

export const routing: ModuleWithProviders = RouterModule.forChild(childroutes);

const newLocal: NgModule = {
    imports: [RouterModule.forChild(childroutes) ],exports: [RouterModule,],declarations: []
};

@NgModule(newLocal)


export class MainRoutingModule { }

MainpageComponent:使用辅助路由的正确语法.

[routerLink]=”[{outlets:{detail:[‘productdetail’]}}]”

import { Component,OnInit,ViewEncapsulation } from '@angular/core';
import { Router } from '@angular/router';

@Component({
  selector: 'app-main',template: `
  <div>

  <div><a [routerLink]="['product']"> Product </a> </div>
  <div><a [routerLink]="[{outlets:{detail:['productdetail']}}]"> Product Detail </a> </div>
  <div> <router-outlet></router-outlet></div>
  <div> <router-outlet name="detail"></router-outlet>

</div>
  `,encapsulation: ViewEncapsulation.None,})

export class MainpageComponent {}

LoginComponent:

Use [routerLink]=”[‘mainpage/load’]” to load the main module.

import { Component,OnDestroy } from '@angular/core';

@Component({
  selector: 'app-login',template: `This is login page place holder <br> <a [routerLink]="['mainpage/load']">Go to Main Page</a>`,})

export class LoginComponent implements Oninit,OnDestroy {
constructor() {}

ngOnInit(): void {}

}

演示:https://plnkr.co/edit/0ZpNL3lvbRbexLzQqRZh?p=preview

Angular 4延迟加载和路由不起作用

Angular 4延迟加载和路由不起作用

我有一个模块与我的应用程序的路线.其中一条路线是延迟加载模块.

问题是这个延迟加载模块内部有子组件的路由.但是在我的路由器配置上这条路线没有出现……所以当我打电话给懒人模块时,我的屏幕上没有显示任何内容.

这是我的路由器配置(主模块):

export const MODULE_ROUTES: Route[] =[
    { path: '',redirectTo: 'dashboard',pathMatch: 'full' },{ path: 'dashboard',component: HomeComponent,canActivate: [AuthGuard] },{ path: 'calendar',loadChildren: 'app/dashboard/calendar/calendar-module.module#CalendarModuleModule',canActivate: [AuthGuard]},{ path: '**',component: nopageFoundComponent,pathMatch: 'full' }
]
.
.
.


@NgModule({
    imports: [
        RouterModule.forChild(MODULE_ROUTES)
.
.
.

在我的懒惰模块上:

export const MODULE_CALENDAR_ROUTES: Route[] = [
    {
        path: 'calendar',component: CalendarComponent,canActivateChild: [AuthGuard,CalendarGuard],children: [
            {
                path: '',component: MainCalendarComponent,CalendarGuard]
            },{

                path: 'user',component: EditEventComponent,CalendarGuard]
            }
        ]
    }
]

.
.
.

@NgModule({
    imports: [
        SharedModule,.
.
.
        RouterModule.forChild(MODULE_CALENDAR_ROUTES)

如果我打印我的路由器配置,我的懒惰模块上的路由声明不显示:

Routes:  [
  {
    "path": "dashboard","canActivate": [
      null
    ]
  },{
    "path": "calendar","loadChildren": "app/dashboard/calendar/calendar-module.module#CalendarModuleModule",{
    "path": "**","pathMatch": "full"
  },{
    "path": "dashboard"
  }
]

你能帮助我吗?

解决方法

问题在于我在懒惰模块上声明我的路线的方式:

export const MODULE_CALENDAR_ROUTES: Route[] = [
    {
        path: 'calendar',CalendarGuard]
            }
        ]
    }
]

CalendarComponent的路径必须从以下更改:

path: 'calendar',// wrong
component: CalendarComponent,...

到下面:

path: '',// right
component: CalendarComponent,...

感谢@jotatoledo on gitter帮助我解决这个问题.

关于angular – 在没有延迟加载的情况下路由到子路由模块angular路由懒加载的介绍现已完结,谢谢您的耐心阅读,如果想了解更多关于Angular 10:为延迟加载的模块创建捆绑包、Angular 2如何为延迟加载的模块提供单例服务、Angular 4延迟加载,命名路由器插座无法正常工作、Angular 4延迟加载和路由不起作用的相关知识,请在本站寻找。

本文标签: