在Angular 1取得巨大成功之后,我开始使用Angular 2.我遵循了Quickstart和Tour of Heroes教程,一切都像魅力一样 .
Lite服务器开始启动,我看到 tsc
在监视模式下运行,我甚至看到BrowserSync被连接起来 . 大!
但是,我需要开始让事情变得更加现实 .
Instead of using lite server, how do I get all this working using a flask dev or gunicorn server, serving the initial index.html file as a rendered jinja template?
给烧瓶开发服务器一个非常有用的尝试,我基本上将示例index.html的内容从教程复制到我的jinja模板,然后运行 npm run tsc:w
,最后启动我的烧瓶开发服务器并希望最好 . 事情编译得很好 . 但是在浏览器中我看到了问题:
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,反应扩展,systemjs,angular2本身有很多黑魔法,然后在tsc和lite服务器中抛出 . 不可否认,我还没有完成所有这些工作,这需要时间,但我希望能够让我的项目很快进入理智状态 .
(我不介意在开发中使用lite服务器(BrowserSync是一个很好的特权),只要我可以配置它代理将返回渲染jinja模板的真正的烧瓶服务器 . )
Update
这是实际的模板索引文件,我做了一些小的改动:
<!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>
2 回答
I had the same problem and here is how I solved it;
第1步
目录结构如下:
--- //...flask文件在这里
---节点模块
--- app
--- //...更多节点角度应用程序文件
我使用以下代码在URL
.../client-app/...
中公开了我的烧瓶应用程序中的ClientApp文件夹:第2步
转到你的
index.html
文件(我把我放在ServerApp\templates\index.html
以便我可以简单地做render_template('index.html')
)并使它看起来像这样:the 'client-app' prepended to the paths is the route I chose to expose my client_app_folder() function at
第3步
在这种情况下配置客户端应用程序's package finder to use the set route (' client-app / ...' . 我使用system.js,因此我的
systemjs.config.js
文件看起来像这样:I only modified the map variable
一帆风顺!
在寻找同一个问题的答案时,我被提醒要改变角度1中的插值字符 . 可以找到两个角度2的位置,但它很容易在jinja2侧改变 . 用你想要的设置jinja2环境选项字符:
希望我在这里使用兼容的字符串,我猜我会发现! (见http://jinja.pocoo.org/docs/dev/api/环境选项)