我有一个带有 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/