首页 > 解决方案 > 如何在Angular templateurl中添加变量

问题描述

我想为我的组件加载供应商特定的 html 模板。下面是一个代码片段。问题是,我将解析文本字符串作为输出而不是实际模板。有人可以帮我理解这里出了什么问题吗?

代码片段:

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

const helplineTemplate: string = './vendors/helpline.'+environment.vendor_prefix+'.component.html';

@Component({
  selector: 'app-helpline',
  templateUrl: helplineTemplate,
  styleUrls: ['./helpline.component.css']
})
export class HelplineComponent implements OnInit {
  constructor() { }
  ngOnInit() {
  }
}

我得到的输出:

./vendors/helpline.google.component.html

这里 google 是来自环境文件的变量。environment.vendor_prefix

这可能不是一个重复的问题。我实际上是在尝试理解这种奇怪的行为。虽然它是一个templateUrl,它仍然表现得像template。在编译器命中类装饰器之前,变量值也得到解决。

标签: javascriptangularangular2-template

解决方案


推荐阅读