在Browserify应用程序中注入数据 [英] Inject data in a Browserify app

查看:79
本文介绍了在Browserify应用程序中注入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里的任何人都知道如何将数据注入到Browserify应用程序中?

Anyone here knows how to inject data into a Browserify app?

我的意思是,我使用Browserify创建了一个大捆绑包app.js文件。

I mean, I use Browserify to create a big bundle app.js file.

但是,当我的单页应用程序启动时,服务器还会在加载应用程序的HTML页面中添加一些引导数据,从而使应用程序不必向服务器执行Ajax请求

But when my single page application starts, the server also add some bootstrap data into the HTML page that loads the app, so that the app doesn't have to do ajax requests to the server to get these data.

目前,服务器启动应用程序渲染的HTML模板如下所示:

For now the HTML template rendered by the server to start the app looks like that:

<script type="text/javascript" >
window.bootstrapData = @Html(utils.CustomSerializer.serialize(bootstrapData));
</script>

<script type="text/javascript" src="@{reactAppBaseUrl}/app.js"></script>

app.js 内(浏览器单页应用),我们正在使用 window.bootstrapData 将该数据获取到Browserify捆绑包中。

And inside app.js (the Browserified single page app), we are using the window.bootstrapData to get that data inside the Browserify bundle.

推荐答案

使用外部需求

捆绑应用时,请使用-r选项启用对特定服务的外部需求:

When you bundle your app, use the -r option to enable external requiring for a specific service:

browserify -r appBootstrap  > app.js

然后,在导入捆绑包的脚本标签下方,需要该服务并注入数据:

Then, in a script tag below where you've imported your bundle, require the service and inject the data:

<script type="text/javascript" src="@{reactAppBaseUrl}/app.js"></script>

<script>
  var serializedData = @Html(utils.CustomSerializer.serialize(bootstrapData))
  require('appBootstrap').load(serializedData);
</script>

这篇关于在Browserify应用程序中注入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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