简体   繁体   English

HTML文件中的Bootstrap Angular组件

[英]Bootstrap Angular Components in HTML files

I use Angular on my frontend side with Django. 我在Django的前端使用Angular。 Note that I don't use Angular for SPA, but use Django to serve templates. 请注意,我没有将Angular用于SPA,而是使用Django提供模板。 Problem is that I need to inject some angular components in templates but as I can see I can only bootstrap one root module. 问题是我需要在模板中注入一些角度组件,但是如我所见,我只能引导一个根模块。

This is my app.module.ts : 这是我的app.module.ts

@NgModule({
  declarations: [
    ContactComponent,
    AboutComponent
  ],
  imports: [
    BrowserModule,
    HttpModule
  ],
  providers: [],
  bootstrap: [ContactComponent, AboutComponent]
})

I want to inject ContactComponent <contact></contact> in contact.html , and AboutComponent <about></about> in about.html . 我想在contact.html注入ContactComponent <contact></contact> ,并在about.html注入AboutComponent <about></about>

After ng-build I get following error: ng-build之后,出现以下错误:

The selector "contact" did not match any elements(…) in about.html The selector "about" did not match any elements(…) in contact.html 选择器“联系人”与 about.html 任何元素都不匹配(…) 选择器“ about”与 contact.html 中的任何元素都不匹配(…)

How can I achieve this, to exports angular components and use it in html files. 如何实现这一点,以导出角度分量并将其用于html文件中。 Maybe Angular is build only for SPA or I am missing something. 也许Angular仅针对SPA构建,或者我缺少某些东西。

I use Angular version 4 in my project which is generated through angular-cli (don't use SystemJS anymore). 我在通过angular-cli生成的项目中使用Angular版本4(不再使用SystemJS)。

What you are doing is right in programming terms. 您所做的工作在编程方面是正确的。 You can bootstrap multiple modules using declaration metadata as you are doing. 您可以在执行操作时使用声明元数据来引导多个模块。 So your error is NOT comming because the code you shown. 因此,由于显示了代码,因此不会出现您的错误。 You should check imports, reaches and selectors. 您应该检查导入,到达和选择器。 Anyways. 无论如何。

The sense of bootstraping one only root component is an agreement that give more sense to modularity. 引导唯一的根组件的意义是使模块化更具意义的协议。 And you have some other ways to bootstrap multiple modules 您还有其他方法来引导多个模块

If the sense of your module is more like an individual app you can isolate them with their main.ts doing the bootstrap (each one) main.ts 如果模块的含义更像是一个单独的应用程序,则可以使用它们的main.ts进行引导(每个引导)来隔离它们(每个)

import {bootstrap}    from '@angular2/platform/browser'
import {FirstComponent} from './my-first.component'

bootstrap(FirstComponent);

and then import in your HTML. 然后导入您的HTML。

 System.import('first/main').then(null, console.error.bind(console));
 System.import('second/main').then(null, console.error.bind(console));

If the sense is to have as a "package" you can have a main module that acts as barrel, helper or how you want to name it. 如果感觉是作为“包”,则可以有一个充当桶,助手或您想如何命名的主模块。 It imports your modules 它导入您的模块

@NgModule({
  declarations: [First],
  exports: [First]
})
export class FirstModule

@NgModule({
  declarations: [Second],
  exports: [Second]
})
export class SecondModule

@NgModule({
 imports: [FirstModule, SecondModule],
 exports: [FirstModule, SecondModule]
})
export class BarrelModule

You can use this only module when you require all of them. 当您需要所有模块时,可以使用此模块。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM