Angular2如何设置templateUrl基本路径 [英] Angular2 how to set templateUrl base path

查看:69
本文介绍了Angular2如何设置templateUrl基本路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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基本路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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