2017-08-22 130 views
1

我有一個AngularJS前端和一個Flask後端的應用程序。AngularJS + 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')) 

我然後用角StateProvider客戶端側的路由:

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

如果我開始在我的網站的根目錄,然後點擊一個鏈接帶我去聯繫頁這個偉大的工程。但是,如果我想通過鍵入mysite.com/contact直接轉到聯繫頁面,則會重定向主頁。這是預期的。

我該如何允許它轉到客戶端路由(如果可用),否則重定向到主頁?

+0

你是什麼意思「預計哪個」?它必須告訴你contact.html對不對? – user3411864

+0

我的意思是它的預期,因爲服務器端路由規則說重定向首頁,因爲它不知道/聯繫。 – DJDMorrison

+0

我明白了。正確的答案已經在這種情況下:) – user3411864

回答

1

從服務器端,如果您遇到家庭或未知路由(404),您應該返回index.html,因爲您不知道客戶端是否可以處理此路由。在加載index.html後,客戶端路由應接管並決定是否重定向到Not found或以其他方式處理此路由。

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

這是有效的。渲染index.html是正確的,而不是路由到主頁並從那裏呈現index.html。謝謝@日裏維納斯! – DJDMorrison