简体   繁体   English

在Angular(4)中使用拆分的应用程序模块处理导入(客户端,服务器,共享)

[英]Working with imports with split app modules in Angular(4) (client, server, shared)

I created a .net spa application with Angular for the front end using the yeoman generator. 我使用yeoman生成器使用Angular为前端创建了一个.net spa应用程序。 It creates an app with three app.module.ts files (app.module.client.ts, app.module.shared.ts, app.module.server.ts). 它使用三个app.module.ts文件(app.module.client.ts,app.module.shared.ts,app.module.server.ts)创建一个应用程序。 Unfortunately the NgModule imports which are located in client app module file do not seem to be properly injected into the components. 不幸的是,位于客户端应用程序模块文件中的NgModule导入似乎未正确注入到组件中。 So when I try to use ngFor on an input I am given the error "Can't bind to 'ngModel' since it isn't a known property of 'input'." 因此,当我尝试在输入上使用ngFor时,出现错误“无法绑定到ngModel”,因为它不是'input'的已知属性。 I think this is because the forms module is not being imported even though my app.module.client.ts file looks like this: import { NgModule } from '@angular/core'; 我认为这是因为即使我的app.module.client.ts文件看起来像这样,表单模块也没有被导入:从'@ angular / core'导入{NgModule};

import { BrowserModule } from '@angular/platform-browser';
import { FormsModule } from '@angular/forms';
import { HttpModule } from '@angular/http';
import { sharedConfig } from './app.module.shared';
import {SelectModule} from 'ng2-select';

    @NgModule({
        bootstrap: sharedConfig.bootstrap,
        declarations: sharedConfig.declarations,
        imports: [
            BrowserModule,
            FormsModule,
            HttpModule,
            SelectModule,
            ...sharedConfig.imports
        ],
        providers: [
            { provide: 'ORIGIN_URL', useValue: location.origin }
        ]
    })
    export class AppModule {
    }

Perhaps I'm missing something on how these divided modules work. 也许我缺少有关这些分开的模块如何工作的信息。 How do I properly import modules given this different module architecture? 给定这种不同的模块架构,如何正确导入模块?

Update: I was able to solve the problem by including the module imports in the app.module.server.ts as well. 更新:我能够通过在app.module.server.ts中包括模块导入来解决问题。 I want to leave this question open and hopefully someone can explain how this works. 我想公开这个问题,希望有人可以解释它是如何工作的。 It's especially strange since the prebuilt template from Yeoman has all the module imports in the app.module.client.ts file and they seem impotent when running the website with dotnet run they had to be duplicated in the server file as well. 尤为奇怪的是,来自Yeoman的预构建模板在app.module.client.ts文件中导入了所有模块,并且在使用dotnet run网站时它们似乎无能为力,它们也必须在服务器文件中复制。

The ASP.NET Core SPA with Angular 4 template splits the AppModule into three files so that Webpack will be able to efficiently compile the client-side bundle (suitable for running in browsers) and the server-side prerendering bundle (meant for running in Node). 带有Angular 4模板的ASP.NET Core SPAAppModule分为三个文件,以便Webpack能够高效地编译客户端捆绑(适用于在浏览器中运行)和服务器端预渲染捆绑(适用于在Node中运行) )。

More specifically, such approach expects that: 更具体地说,这种方法期望:

  • browser-specific modules and providers will be put in the app.module.browser.ts file. 浏览器特定的模块和提供程序将放在app.module.browser.ts文件中。

  • server-specific modules and providers will be put in the app.module.server.ts file. 服务器特定的模块和提供程序将放在app.module.server.ts文件中。

  • anything needed regardless of the executing context will be put in the app.module.shared.ts file. 无论执行上下文如何,所需的所有内容都将放入app.module.shared.ts文件中。

This will allow two different bootstrap cycles: one for the browsers ( shared + browser ) and one for the server ( shared + server ). 这将允许两个不同的引导周期:一个用于浏览器( 共享 + 浏览器 ),一个用于服务器( 共享 + 服务器 )。 The main difference between the two is that the first one imports the BrowserModule from the @angular/platform-browser package, while the latter imports the ServerModule from the @angular/platform-server package - which is required to render Angular app pages from the server. 两者之间的主要区别是,第一个从@angular/platform-browser包中导入BrowserModule ,而ServerModule@angular/platform-server包中导入ServerModule这是从@angular/platform-server渲染Angular应用程序页面所必需的服务器。

That said, you should put all your app references into the app.module.shared.ts file and write your Angular code using an isomorphic approach - meaning that it will work regardless of the executing context: all the rest should be handled by the default Webpack configuration shipped with the template, which will generate the appropriate client & server builds for JIT, AoT & server-side rendering. 就是说,您应该将所有应用程序引用放入app.module.shared.ts文件中,并使用isomorphic方法编写Angular代码-这意味着它可以在不考虑执行上下文的情况下正常工作:所有其余部分均应由默认处理模板附带的Webpack配置将为JIT,AoT和服务器端渲染生成适当的客户端和服务器版本。

For some (very) basic info about how to start writing isomorphic code in Angular and/or identify the executing environment and react accordingly, check out this blog post that I wrote on the topic. 有关如何开始在Angular中编写同构代码和/或确定执行环境并做出相应反应的一些(非常)基本信息,请查看我在该主题上撰写的这篇博客文章

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

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