Flask默认路由如何避免重定向?

时间:2022-10-30 18:07:04

I hava a list page:

我有一个列表页面:

$routeProvider.when('/', {
    redirectTo:'/1',            // so default will be page 1
}).when('/:page',{
    controller:'listCtrl',
    templateUrl:'xxxxx'
})

In the controller will use $route.current.params.page to do the logic.

在控制器中将使用$ route.current.params.page来做逻辑。

The question is:redirectTo will cause a browser 301 which I don't want.(bad performance) And I also don't want everytime one click the main nav button, it would cause a 301.

问题是:redirectTo会导致我不想要的浏览器301。(性能不佳)而且我也不希望每次单击主导航按钮,都会导致301。

How can I archive the same goal without use redirectTo?

如何在不使用redirectTo的情况下存档相同的目标?

It's something like: if I use the route same as '/:page'(write the same controller / templateUrl), how can I pass the page number(1) to the controller without route param?

它类似于:如果我使用与'/:page'相同的路径(编写相同的controller / templateUrl),如何在没有路由参数的情况下将页码(1)传递给控制器​​?

EDIT:

Thanks to the accepted answer.

感谢接受的答案。

Changed the title to Flask related.

将标题更改为Flask相关。

Found that the 301 was caused by Flask's defaults route:

发现301是由Flask的默认路由引起的:

@bp.route('/list', defaults={'page':1})
@bp.route('/list/<int:page>')
def lst(page):

when the request uri is /list/1, will be 301 to /list... don't know how to resolve yet, but this is another question.

当请求uri是/ list / 1时,将301到/ list ...不知道如何解决,但这是另一个问题。

1 个解决方案

#1


2  

redirectTo in $route doesn't actually do an HTTP redirect - it just redirects logically to the right ng-view. So, there isn't any performance hit.

$ route中的redirectTo实际上并没有进行HTTP重定向 - 它只是在逻辑上重定向到正确的ng-view。因此,没有任何性能损失。

But just for completeness-sake, you can pass a parameter in the resolve property:

但为了完整起见,您可以在resolve属性中传递一个参数:

.when('/:page?',{
    controller:'listCtrl',
    templateUrl:'xxxxx',
    resolve: {
      page: function($route) { return $route.current.params.page || 1; }
    }
})

#1


2  

redirectTo in $route doesn't actually do an HTTP redirect - it just redirects logically to the right ng-view. So, there isn't any performance hit.

$ route中的redirectTo实际上并没有进行HTTP重定向 - 它只是在逻辑上重定向到正确的ng-view。因此,没有任何性能损失。

But just for completeness-sake, you can pass a parameter in the resolve property:

但为了完整起见,您可以在resolve属性中传递一个参数:

.when('/:page?',{
    controller:'listCtrl',
    templateUrl:'xxxxx',
    resolve: {
      page: function($route) { return $route.current.params.page || 1; }
    }
})