我有一个带有aapp.module
和a的angular4&Universal应用程序app.browser.module/app.server.module
。
App.Module
具有所有应用程序配置和引导程序。app.browser.module
调用浏览器中使用的模块,以避免破坏通用版本。
当我将外部lib(ng2-semantic-ui
)导入时app.browser.module
,应用程序无法识别ng2-semantic-ui
指令,但是当我将其导入到'app.module'时,它可以识别指令。
这是我的模块结构:
import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { SuiModule } from 'ng2-semantic-ui';
@NgModule({
declarations: [
AppComponent,
HomeComponent,
],
imports: [
BrowserModule.withServerTransition({appId: 'app.id'}),
SuiModule,
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppModule } from './app.module';
import { AppComponent } from './app.component';
import { SuiModule } from 'ng2-semantic-ui';
@NgModule({
imports: [
BrowserAnimationsModule,
AppModule,
SuiModule,
],
bootstrap: [AppComponent]
})
export class AppBrowserModule {}
当我SuiModule
在app.module
应用程序内部导入时,可以识别其指令,但是当我将其导出到时app.browser.module
,它不会。为什么?
compiler.es5.js:1690 Uncaught Error: Template parse errors:
'sui-sidebar' is not a known element:
1. If 'sui-sidebar' is an Angular component, then verify that it is part of this module.
2. If 'sui-sidebar' is a Web Component then add 'CUSTOM_ELEMENTS_SCHEMA' to the '@NgModule.schemas' of this component to suppress this message. ("
BrowserAnimationsModule
当我将PS导入其中时,它们在应用程序中被识别app.browser.module
。
您的问题来自Angular的模块层次结构。您要尝试的是建造建筑物,然后在第二层添加一个房间(关于组件),然后在第三层添加一个窗口(语义ui),然后将其添加到第二层的房间中。在您的情况下,您向app.module声明about.component,并尝试在about.components上使用一些语义组件,这些语义组件稍后在同时导入app.module时在app.browser.module上导入。
要修复它,您必须将组件声明移至app.browser.module:
import { NgModule } from '@angular/core';
import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AppModule } from './app.module';
import { AppComponent } from './app.component';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
/*Code that breaks the NG APP when importing SUI on app.browser.module, but works well when imported on app.module*/
import {SuiModule} from 'ng2-semantic-ui';
@NgModule({
imports: [
BrowserAnimationsModule,
AppModule,
SuiModule,
],
declarations: [HomeComponent,
AboutComponent],
bootstrap: [ AppComponent ]
})
export class AppBrowserModule {}
或者您在app.module上执行此操作:
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { AppComponent } from './app.component';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { AppRoutingModule } from './app-routing.module';
import { HomeComponent } from './home/home.component';
import { AboutComponent } from './about/about.component';
/*Code that breaks the NG APP when importing SUI on app.browser.module, but works well when imported on app.module*/
import {SuiModule} from 'ng2-semantic-ui';
@NgModule({
declarations: [
AppComponent,
HomeComponent,
AboutComponent
],
imports: [
BrowserModule.withServerTransition({appId: 'ngcli-universal-seed-app'}),
FormsModule,
HttpModule,
AppRoutingModule,
SuiModule
],
providers: [],
bootstrap: [AppComponent]
})
export class AppModule { }
您可以使用所需的体系结构(尽管坚持使用@angular提出的建议是一件好事),只要正在使用组件的页面在其模块中(直接或通过另一个模块)导入了该组件。
搜索关于这个问题的解决方案,我发现一个有趣的网页约角通用服务器端呈现在这里。
在本文中,我发现到样板的链接比您正在使用的样板的链接结构更好,并且是最新的。你可以在这里找到它。我建议您使用此版本,因为它结构更好并且可以正常工作。
最后,我将您的语义UI代码添加到此样板的“关于”页面,将模块导入其about.module,编译通用应用程序并运行它,一切正常。
我放弃了你的样板与工作添加的代码在这里
总而言之,问题可能来自您正在使用的样板结构,或者在构建时出现了一些依赖冲突。此外,我发现通用应用程序的Google文档不完整且过时。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句