问题描述
在Angular2中,开发过程中出现了类似templateUrl = 'templates/app.component.html'
的内容.但是在部署中,我需要将该URL设为templateUrl = '/static/angular_templates/app.component.html'
之类的东西.不得不在每个地方更改url的问题.有没有一种简单的方法来设置基本网址并执行
In Angular2, I have something like templateUrl = 'templates/app.component.html'
during development. But in deployment, i need the url to be something like templateUrl = '/static/angular_templates/app.component.html'
. It bites to have to change the url at every single place. Is there an easy way to set a base url and do something like
templateUrl = BASE_TEMPLATE_PATH+ '/app.component.html'
因此,从开发切换到生产时,我只需要更新BASE_TEMPLATE_PATH
?谢谢!
so i only need to update BASE_TEMPLATE_PATH
when switching from development to production? Thanks!
推荐答案
使用TypeScript,您可以使用静态变量,例如:
With TypeScript you can use static variables, like :
export class AppTemplateConstants {
public static get BASE_TEMPLATE_PATH(): string { return 'YOUR_PATH_HERE'; }
}
要导入此内容:
import { AppTemplateConstants } from '../shared/constants/AppTemplateConstants';
要使用此功能:
templateUrl: AppTemplateConstants.BASE_TEMPLATE_PATH + 'app/path/component.html'
这篇关于Angular2如何设置templateUrl基本路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!