angular-cli:根据环境使用不同的模板 [英] angular-cli: Use different templates depending on the environment

查看:20
本文介绍了angular-cli:根据环境使用不同的模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 @angular/cli: 1.0.0 并且我想根据环境使用组件模板.我已经实现了这个:

I'm using @angular/cli: 1.0.0 and I want to use component templates depending on the environment. I've already implemented this:

import {Component} from '@angular/core';
import {environment} from '../environments/environment';

@Component({
  selector: 'tbe',
  templateUrl: environment.BASE_TEMPLATE_PATH+'app.component.html'
})
export class AppComponent {
  ...
}

我的环境设置如下:

export const environment = {
  production: false,
  BASE_TEMPLATE_PATH: '../templates/default/'
};

构建过程确实有效,但浏览器控制台抛出错误:

The building process actually works but the browser console throws an error:

Error: Cannot find module "."

如果我将 environment.BASE_TEMPLATE_PATH+'app.component.html' 替换为实际路径,它会起作用.

If I replace environment.BASE_TEMPLATE_PATH+'app.component.html' with the actual path, it works.

这里可以使用环境变量吗?如果是,我的错误在哪里?

Is it possible to use environment variables here? If yes, where is my mistake?

更新:

我为我的问题找到了更好的解决方案:http://www.smartjava.org/content/dynamic-component-loading-angular2-replace-compile

I found a better solution for my problem: http://www.smartjava.org/content/dynamic-component-loading-angular2-replace-compile

我已更改 plunker 示例以设置 templateUrl 而不是纯 html 模板,这有效:https://plnkr.co/edit/tfxl3ba869oFtrqzyLNk

I've changed the plunker example to set a templateUrl instead of the plain html template and this works: https://plnkr.co/edit/tfxl3ba869oFtrqzyLNk

问题:

ComponentResolver 已折旧,我无法在当前的 Angular4 版本中使用它.我用 ComponentFactoryResolver 尝试过,但它不起作用.

ComponentResolver is depreciated and I can't use it with the current Angular4 version. I tried it with ComponentFactoryResolver but it doesn't work.

推荐答案

同样可行的(至少在最新版本的 Angular 中)是

What also would work (at least in the latest Version of Angular) would be

import {Component} from '@angular/core';
import {environment} from '../environments/environment';

const env = environment;

@Component({
  selector: 'tbe',
  templateUrl: env.BASE_TEMPLATE_PATH+'app.component.html'
})

export class AppComponent {
  ...
}

我面临着同样的任务,所以我四处游荡.我也可以确认,其他文件不包含在您的 AOT-build 中.

I am facing the same task so I fiddles around. I also can confirm, that the other files are not included in your AOT-build.

这篇关于angular-cli:根据环境使用不同的模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆