python - AngularJS + Flask 客户端路由

标签 python angularjs flask

我有一个带有 AngularJS 前端和 Flask 后端的应用程序。

我在 Flask 中有以下路由来将所有请求发送到 index.html 文件(以及 API 路由的一些路由):

@app.route('/')
def home():
    return render_template('index.html')

@app.route('/api/colours')
def colours():
    ...

@app.route('/<path:page>')
def fallback(page):
    return redirect(url_for('home'))

然后我使用 Angular StateProvider 进行客户端路由:

$stateProvider
    .state('home', {
        url: '/',
        templateUrl: 'static/templates/home.html'
    })
    .state('contact', {
        url: '/contact', 
        templateUrl: 'static/templates/contact.html'
    });

如果我从网站的根目录开始并单击链接将我带到联系页面,这会非常有效。但是,如果我想通过输入 mysite.com/contact 直接进入联系页面,它会重定向主页。这是预期的。

如果可用,我如何允许它进入客户端路由,否则重定向到主页?

最佳答案

从服务器端,如果遇到主路由或未知路由 (404),您应该返回 index.html,因为您不知道客户端是否可以处理此路由。加载 index.html 后,客户端路由应接管并决定是否重定向到 Not find 或以其他方式处理此路由。

@app.route('/<path:page>')
def fallback(page):
    return render_template('index.html')

关于python - AngularJS + Flask 客户端路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45819586/

相关文章:

python - 如何通过逐行读取文件来制作json字符串?

python - .py 不从内容文件夹中读取

javascript - 创建 Angular Directive(指令)以重用代码 - 创建 html 时解析错误

html - 每个页面刷新时新的 MySQL 查询

python - WTForms 等同于 &lt;input type ="datetime-local">

python - Tkinter,canvas.rectangle 中的标签/文本 [python]

python pandas cumsum 与 groupby 和条件

javascript - 混合应用程序 : Change HTTP user agent header with AngularJS

css - AngularJS 样式问题 IE

python - Sqlalchemy 如何过滤同时包含 Null 值和整数值的列