根据main.ts中的URL引导多个或单个模块

时间:2018-10-19 12:23:08

标签: angular typescript

我正在使用一个无法完全转移到完整角度项目的(.NET Framework MVC)Web项目,因此我无法使用angular的路由进行延迟加载,但是我也不想加载所有在使用组件。这是一种企业解决方案,很难说(“便宜”):“嘿,让我们充分利用有角度的东西,而不必理会旧项目!”。

我认为,我已经以一种干净的方式解决了这个问题,可以根据URL加载不同的模块,从而避免将所有内容一起加载:

main.ts

console.log("bootstrapping start");
if (window.location.pathname.toLowerCase().includes("contactplans/details")) {
    console.log("bootstrapping contactplan details");
    platformBrowserDynamic().bootstrapModule(ContactplanDetailsModule)
        .catch(err => console.log(err));
} else if (window.location.pathname.toLowerCase().includes("contactplans/index") || window.location.pathname.toLowerCase().endsWith("contactplans") || window.location.pathname.toLowerCase().includes("settings/contactplans")) {
    console.log("bootstrapping contactplan index");
    platformBrowserDynamic().bootstrapModule(ContactplanListModule) //contact plan index and settings page
        .catch(err => console.log(err));
} 
console.log("bootstrap decision done, bootstrapping menu");
platformBrowserDynamic().bootstrapModule(MenuModule)
    .catch(err => console.log(err));

因此,它基于URL加载模块,并在每个页面上加载菜单模块。

就目前而言,我有点必须这样做,这只是一个小例子,在越来越多的单个页面上,Angular的使用将显着增长,直到我们可以更轻松地“切换”到完整页面角度项目(在.NET Core中可以协同工作)。

因此,这在开发上效果很好。使用ng build --watch完成所需的操作。 现在开始生产,运行ng build --prod会产生问题。对我来说,它更像是一个 bug ,然后是其他东西。

在下面的屏幕截图中,我演示了在ng build --prod之后对上面的代码进行修改时所产生的结果。

Click here to show bigger enter image description here

如您所见,当仅使用一行引导程序时,它工作正常,没有错误。

但是当我有多个想要的版本时,它将实际的Module更改为function() {},这会导致控制台错误:

Uncaught Error: No NgModule metadata found for 'function(){}'.

这真的是一个错误还是我做错了什么?

1 个答案:

答案 0 :(得分:1)

基于url的自举组件

我遇到了同样的问题,我的解决方案是基于url引导不同的组件,而不是引导不同的模块

解决方案基于本文:How to manually bootstrap an Angular application

您无需编辑main.ts,但在AppModule中,您可以根据url手动引导不同的组件并将其添加到DOM

app.module.ts

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

import { ContactplanDetailsComponent } from './ContactplanDetails.component';
import { ContactplanListComponent } from './ContactplanList.component';
import { MenuComponent } from './Menu.component';



@NgModule({
  imports: [BrowserModule],
  declarations: [
     ContactplanDetailsComponent,
     ContactplanListComponent,
     MenuComponent
  ],
  //bootstrap: [AppComponent] Bootstrapping is done manually
  entryComponents: [
     ContactplanDetailsComponent,
     ContactplanListComponent,
     MenuComponent
  ]

})
export class AppModule { 
  ngDoBootStrap(app) {
    bootstrapComponent(app);
  }
}

function bootstrapComponent(app) {

  var name = [];

  const options = {
    'contact-plan-details': ContactplanDetailsComponent,
    'contact-plan-list': ContactplanListComponent,
    'menu': MenuComponent
  };

  if (window.location.pathname.toLowerCase().includes("contactplans/details")) {
    name.push("contact-plan-details"); 
  } else if (window.location.pathname.toLowerCase().includes("contactplans/index") || window.location.pathname.toLowerCase().endsWith("contactplans") || window.location.pathname.toLowerCase().includes("settings/contactplans")) {
    name.push("contact-plan-list");
  } 
  name.push("menu")

  name.forEach(function (element) {

    const componentElement = document.createElement(element);
    document.body.appendChild(componentElement);

    const component = options[element];
    app.bootstrap(component);

  });
}