我在这里寻找一些最好的做法.我的angular2应用程序将存在于现有的内容管理系统中.因此,我需要捕获由此CMS生成的一些"变量"(如auth令牌等),并将其与我的angular2应用程序中的http请求一起使用.
当CMS显示index.html页面时,CMS会对其进行预解析,并在将页面发送到浏览器之前替换一些令牌(即[ModuleContext:ModuleId]).
这是我的index.html页面的示例(abreviated):
<!-- 2. Capture CMS values to pass to app -->
<script type="text/javascript">
var moduleId = parseInt("[ModuleContext:ModuleId]");
var portalId = parseInt("[ModuleContext:PortalId]");
var sf = $.ServicesFramework(moduleId);
</script>
<!-- 3. Configure SystemJS and Bootstrap App-->
<script type="text/javascript">
System.config({
packages: {
//sets the root path of the Angular2 App
'DesktopModules/KrisisShifts/app': {
format: 'register',
defaultExtension: 'js'
}
},
map: { 'app': './app' }
});
System.import('app/boot')
.then(null, console.error.bind(console));
</script>
<shift-app>Loading...</shift-app>
Run Code Online (Sandbox Code Playgroud)
具体来说,$ .ServicesFramework用于生成有效的http web.api请求.我想在一个可以注入到使用它的每个组件中的服务中捕获它.
例如(我正在使用打字稿):
import {Injectable} from 'angular2/core';
import {OnInit} from 'angular2/core';
@Injectable() …Run Code Online (Sandbox Code Playgroud)