在Angular 4

时间:2017-05-28 12:21:53

标签: angular angular-cli

问题:

如果我通过:

npm run build -- brl

如何(或是否可以)在Angular主TS文件中获取brl值?

例如我们假设我想在main.ts中替换[npm_variable],代码如下:



import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './[npm_variable]/app/app.module';
import { environment } from '../../environments/environment';

if (environment.production) {
  enableProdMode();
}

platformBrowserDynamic().bootstrapModule(AppModule);




概述:

我知道Angular CLI现在支持以下几个应用程序入口点:

ng build --prod --aot --app myAppName

我正在创建一个多站点应用程序,我希望代码库与多个入口点相同。

我使用以下文件夹结构来实现多站点架构。如您所见,我是所有常见组件,配置,模块和服务所在的公共文件夹。除此之外,我还有不同的网站,例如brl,chn等......(大约13个不同的网站共享相同的代码库)

Angular Folder Structure

以下是我的.angular-cli.json



{
  "$schema": "./node_modules/@angular/cli/lib/config/schema.json",
  "project": {
    "name": "angular"
  },
  "apps": [
    {
      "name": "brl",
      "root": "resources/angular/sites/brl",
      "outDir": "public/dist/brl",
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "../../polyfills.ts",
      "test": "../../test.ts",
      "tsconfig": "../../tsconfig.app.json",
      "testTsconfig": "../../tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "../../styles.scss"
      ],
      "scripts": [],
      "environmentSource": "../../environments/environment.ts",
      "environments": {
        "dev": "../../environments/environment.ts",
        "prod": "../../environments/environment.prod.ts"
      }
    },
    {
      "name": "chn",
      "root": "resources/angular/sites/chn",
      "outDir": "public/dist/chn",
      "index": "index.html",
      "main": "main.ts",
      "polyfills": "../../polyfills.ts",
      "test": "../../test.ts",
      "tsconfig": "../../tsconfig.app.json",
      "testTsconfig": "../../tsconfig.spec.json",
      "prefix": "app",
      "styles": [
        "../../styles.scss"
      ],
      "scripts": [],
      "environmentSource": "../../environments/environment.ts",
      "environments": {
        "dev": "../../environments/environment.ts",
        "prod": "../../environments/environment.prod.ts"
      }
    },
    "lint": [
        {
        "project": "resources/angular/tsconfig.app.json"
        },
        {
        "project": "resources/angular/tsconfig.spec.json"
        }
    ],
    "test": {
        "karma": {
        "config": "./karma.conf.js"
        }
    },
    "defaults": {
        "styleExt": "scss",
        "component": {}
  }
}




然后我按下以下命令:

ng build --prod --aot --app brl

OR

ng build --prod --aot --app chn

这很有效,直到我意识到,我需要更复杂和面向对象的方法。对于。例如在这里,只有我的app.component.ts会根据网站提供不同的代码。

但是,我的main.ts(我的应用程序的入口点)正在为所有网站重复。

0 个答案:

没有答案