在Angular 1取得很大成功后,我开始使用Angular 2。我学习了Quickstart和Tour of Heroes两门教程,一切都很有魅力。
lite服务器启动了,我看到tsc
在监视模式下运行,我甚至看到BrowserSync连接起来了。伟大的!
然而,我需要开始让事情变得更真实一些。
如何使用flask dev或gunicorn服务器(将初始index.html文件作为呈现的jinja模板)来完成所有这些工作,而不是使用lite服务器?
让flask dev服务器进行一次非常频繁的尝试,我基本上是将教程中的example index.html的内容复制到我的jinja模板中,然后运行npm run tsc:w
,最后启动flask dev服务器,希望一切顺利。一切都很顺利。但在浏览器中我看到了一些问题:
angular2-polyfills.js:332 Error: SyntaxError: Unexpected token <
at ZoneDelegate.invoke (http://127.0.0.1:5000/static/node_modules/angular2/bundles/angular2-polyfills.js:332:29)
at Zone.run (http://127.0.0.1:5000/static/node_modules/angular2/bundles/angular2-polyfills.js:227:44)
at http://127.0.0.1:5000/static/node_modules/angular2/bundles/angular2-polyfills.js:576:58
Evaluating http://127.0.0.1:5000/app/main.js
Error loading http://127.0.0.1:5000/app/main.js`
查看罪犯transpile main.js文件,我看到:
(function(System, SystemJS, require) {<!doctype html>
<html>
<head lang='en'>
所以,是的,那不管用…很明显我的线路乱七八糟。
填隙片、polyfill、reactive extensions、systemjs、angular2本身都有很多黑色魔法,然后加入tsc和lite服务器。诚然,我还没有把这些都搞定,这需要时间,但我希望我的项目能很快进入正常状态。
(我不介意在开发中使用lite服务器(BrowserSync是一个不错的额外功能),只要我可以将其配置为代理真正的flask服务器,它将返回render jinja模板)
更新
下面是实际的模板索引文件,其中包含我所做的一些小更改:
<!doctype html>
<html>
<head lang="en">
{% block head %}
<meta charset="utf-8">
<title>Angular 2 QuickStart</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="styles.css">
<!-- 1. Load libraries -->
<!-- IE required polyfills, in this exact order -->
<script src="node_modules/es6-shim/es6-shim.min.js"></script>
<script src="node_modules/systemjs/dist/system-polyfills.js"></script>
<script src="node_modules/angular2/es6/dev/src/testing/shims_for_IE.js"></script>
<script src="node_modules/angular2/bundles/angular2-polyfills.js"></script>
<script src="node_modules/systemjs/dist/system.src.js"></script>
<script src="node_modules/rxjs/bundles/Rx.js"></script>
<script src="node_modules/angular2/bundles/angular2.dev.js"></script>
<!-- 2. Configure SystemJS -->
<script>
System.config({
packages: {
app: {
format: 'register',
defaultExtension: 'js'
}
}
});
System.import('app/main')
.then(null, console.error.bind(console));
</script>
{% endblock %}
</head>
<!-- 3. Display the application -->
<body>
{% block content %}{% endblock %}
<my-app>Loading...</my-app>
<script>
(function(globals) {
this.MyConfig = {
staticDir: '{{ config["STATIC_DIR"] }}'
};
}(this));
</script>
</body>
</html>
在寻找同一问题的答案时,我被提醒要更改角度1中的插值字符。可以找到两个在角2上的位置,但是它很简单,可以在jinja2上改变。根据需要设置jinja2环境选项字符:
希望我在这里使用兼容的字符串,我想我会发现! (请参见http://jinja.pocoo.org/docs/dev/api/环境选项)
我也遇到了同样的问题,下面是我如何解决的;
步骤1
目录结构如下:
+-MyAppName
+--服务器应用程序
+---//…此处有烧瓶文件
+--客户机app
+---节点模块
+---应用程序
+---//…更多节点+角度应用程序文件
我使用以下代码在flask应用程序的URL
.../client-app/...
中公开了ClientApp文件夹:步骤2
去看看你的
index.html
文件(我把我的放在ServerApp\templates\index.html
这样我就可以做render_template('index.html')
)然后让它看起来像这样:路径前面的“客户端应用程序”是我在
步骤3
将客户端应用程序的包查找程序配置为使用设置的路由(在本例中为“client-app/…”)。我使用system.js,因此我的
systemjs.config.js
文件如下所示:我只修改了
map
变量上帝保佑!
相关问题 更多 >
编程相关推荐