与 Google App Engine 和 Flask 一起使用时,Angular 路由不起作用 [英] Angular route not working when used with Google App Engine and Flask

查看:26
本文介绍了与 Google App Engine 和 Flask 一起使用时,Angular 路由不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想做什么:

我正在尝试在 Google App Engine 上构建一个 RESTful Flask 应用程序,Angular 处理路由和视图逻辑,而 Flask 处理后端逻辑和资源.

问题:

当我为 GAE 启动开发服务器时,第一页完美加载.问题是,当我单击页面顶部的引用链接时,正在加载的模板不会更改.

到目前为止我所做的

虽然看起来我在下面粘贴了很多代码,但其中大部分都是标记,其中并没有任何复杂的应用程序逻辑,因此略读就足够了

我计划先构建前端,然后构建后端(虽然我已经有一些后端设置).截至目前,该应用程序不依赖于flask 应用程序(它没有任何应用程序逻辑,也没有任何请求处理程序)

这是我的 app.js 文件,到目前为止我所做的只是路由,没有逻辑:

//app.js,项目中只做angular代码,只做路由var rcsApp = angular.module('rcsApp', ['ngRoute']);rcsApp.config(['$routeProvider',功能($routeProvider){$routeProvider.什么时候('/', {templateUrl: 'templates/welcome-page.html'}).当('/索引',{templateUrl: 'templates/welcome-page.html'}).当('/推荐',{templateUrl: '模板/referrals.html'}).当('/404',{templateUrl: 'templates/404.html'}).除此以外({重定向到:'/404'});}]);

这是我的 app.yaml 文件,这是我用来提供静态页面的

# 这个文件指定了你的 Python 应用程序的运行时配置# 包括 URL 路由、版本、静态文件上传等.参见# https://developers.google.com/appengine/docs/python/config/appconfig# 了解详情.# TODO:在下面输入您的应用程序 ID.如果您已经注册# 使用 cloud.google.com/console 为您的应用程序使用项目 ID"# ID.应用:占位符版本:1运行时:python27api_version: 1线程安全:是# 处理程序定义如何将请求路由到您的应用程序.处理程序:# App Engine 提供并缓存包含在所列目录中的静态文件#(和子目录).取消注释并根据需要设置目录.#- 网址:/客户端# static_dir: 客户端- 网址:/cssstatic_dir: 静态/css- 网址:/imgstatic_dir: 静态/img- 网址:/jsstatic_dir: 静态/js- 网址:/模板static_dir: 模板- 网址:/api/.*脚本:main.app- 网址:.*static_files: 模板/app-view-wrapper.html上传:templates/app-view-wrapper.html# 必须列出 App Engine SDK 中包含的第三方库# 如果你想使用它们,请点击此处.看# https://developers.google.com/appengine/docs/python/tools/libraries27 对于# SDK 中包含的库列表.不属于*部分的第三方库# App Engine SDK 不需要在此处列出,而是将它们添加到您的# 项目目录,作为 git 子模块或作为普通子目录.# TODO:在此处列出您可能需要的任何其他 App Engine SDK 库.#图书馆:#- 名称:jinja2# 版本:最新

这是整个应用的基本 html 模板:

模板/app-view-wrapper.html

<!DOCTYPE HTML><!--[如果 IE 9]><html class="lt-ie10" lang="en" ><![endif]--><html class="no-js" lang="en" ng-app="rcsApp"><头><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><link rel="stylesheet" src="//normalize-css.googlecode.com/svn/trunk/normalize.css"/><link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/jquery.slick/1.5.0/slick.css"/><link rel="stylesheet" href="//cdn.jsdelivr.net/foundation/5.5.1/css/foundation.min.css"/><link rel="stylesheet" href="/css/style.css"><script src="//cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js"></script><script src="https://code.angularjs.org/1.3.15/angular.min.js"></script><script src="https://code.angularjs.org/1.3.15/angular-route.min.js"></script><script src="/js/app.js"></script><title>网站标题</title><身体><标题><div class="row"><div class="large-4 columns"><img src="/img/website-logo.png" alt="网站标志"></div><div class="large-8 列"><a href="#" class="button right">555-555-5555</a><a href="#" class="button right">预约</a>

<div class="row" id="nav-row"><nav class="top-bar"><section class="top-bar-section"><ul class="left"><li><a href="/">首页</a></li><li><a href="/">服务</a></li><li><a href="/">见医生</a></li><li><a href="/">患者</a></li><li><a href="/referrals">转诊医生</a></li><li><a href="/">联系我们</a></li></节></nav>

<div ng-view></div></标题><footer class="row"><div class="large-5 列"><h3>位置</h3><div>123 ABC STREET</div><div>电话号码:555-5555555</div><div>电子邮件:email@email.com</div>

<div class="large-4 列"><h3>快速联系</h3><div>工作:555-5555555</div><div>单元格:555-5555555</div><div>传真:555-5555555</div>

<div class="large-3 列">Lorem Ipsum 坐 Dolor Amet

</页脚><script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script><script src="//cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/js/foundation.min.js"></script><script type="text/javascript" src="//cdn.jsdelivr.net/jquery.slick/1.5.0/slick.min.js"></script><script src="/js/script.js"></script><脚本>$(文档).基础();</html>

以下是三个模板:

模板/welcome-page.html

<div><h1>主页面</h1></div>

模板/referrals.html

<div><h1>推荐页面</h1></div>

模板/404.html

<div><h1>404</h1></div>

文件层次结构如下:

- rcsbackend- 模板- 静止的- 图像- js- css- 应用程序.yaml- 主要.py

解决方案

您的解决方案有效,因为默认情况下 html5mode 被禁用,这就是为什么只有 URL# 被角度路由识别之后,并且你在 URL 为你工作之前放置了路由.

解决方案

您需要在您的应用程序中启用 html5mode 以使您的路由工作,只需执行 $locationProvider.html5Mode(true) 在您的 angular 配置阶段.

代码

.config(function($locationProvider) {$locationProvider.html5Mode(true);})

现在你可以回到你的旧代码,将解决你的问题.

更新

要使用相对链接围绕您的应用程序进行链接,您需要在文档的 中设置 .

请参阅此 SO 答案 将在启用 html5mode 在申请中.

What I'm trying to do:

I am trying to build a RESTful Flask application on Google App Engine with Angular handling the routing and view logic while Flask handles back end logic and resources.

The Problem:

When I start the development server for GAE, the first page loads perfectly. The problem is that when I click the Referrals link at the top of the page the template being loaded doesn't change.

What I've done so far

While it seems like I pasted a lot of code below, most of it is markup and there isn't really any complex app logic in there so skimming is enough

I was planning on building the front end first, then the back end (although I have some of the backend setup already). The app does not rely on the flask app as of this moment (it doesn't have any app logic and doesn't have any request handlers)

Here is my app.js file, all I've done so far is routing, no logic:

// app.js, only angular code in project and only does routing
var rcsApp = angular.module('rcsApp', [
    'ngRoute'
]);

rcsApp.config(['$routeProvider',
    function($routeProvider) {
        $routeProvider.
            when('/', {
                templateUrl: 'templates/welcome-page.html'
            }).
            when('/index', {
                templateUrl: 'templates/welcome-page.html'
            }).
            when('/referrals', {
                templateUrl: 'templates/referrals.html'
            }).
            when('/404', {
                templateUrl: 'templates/404.html'
            }).
            otherwise({
                redirectTo: '/404'
            });

}]);

This is my app.yaml file, this is what I use to serve static pages

# This file specifies your Python application's runtime configuration
# including URL routing, versions, static file uploads, etc. See
# https://developers.google.com/appengine/docs/python/config/appconfig
# for details.

# TODO: Enter your application id below. If you have signed up
# using cloud.google.com/console use the "project id" for your application
# id.
application: placeholder
version: 1
runtime: python27
api_version: 1
threadsafe: yes

# Handlers define how to route requests to your application.
handlers:
# App Engine serves and caches static files contained in the listed directories
# (and subdirectories). Uncomment and set the directory as needed.
#- url: /client
#  static_dir: client

- url: /css
  static_dir: static/css

- url: /img
  static_dir: static/img

- url: /js
  static_dir: static/js

- url: /templates
  static_dir: templates

- url: /api/.*  
  script: main.app

- url: .*
  static_files: templates/app-view-wrapper.html
  upload: templates/app-view-wrapper.html

# Third party libraries that are included in the App Engine SDK must be listed
# here if you want to use them.  See
# https://developers.google.com/appengine/docs/python/tools/libraries27 for
# a list of libraries included in the SDK.  Third party libs that are *not* part
# of the App Engine SDK don't need to be listed here, instead add them to your
# project directory, either as a git submodule or as a plain subdirectory.
# TODO: List any other App Engine SDK libs you may need here.
#libraries:
#- name: jinja2
#  version: latest

This is the base html template for the entire app:

templates/app-view-wrapper.html

<!-- templates/app-view-wrapper.html -->
<!DOCTYPE HTML>

<!--[if IE 9]><html class="lt-ie10" lang="en" > <![endif]-->
<html class="no-js" lang="en" ng-app="rcsApp">
    <head>
        <meta charset="utf-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <link rel="stylesheet" src="//normalize-css.googlecode.com/svn/trunk/normalize.css" />
        <link rel="stylesheet" type="text/css" href="//cdn.jsdelivr.net/jquery.slick/1.5.0/slick.css"/>
        <link rel="stylesheet" href="//cdn.jsdelivr.net/foundation/5.5.1/css/foundation.min.css"/>
        <link rel="stylesheet" href="/css/style.css">
        <script src="//cdnjs.cloudflare.com/ajax/libs/modernizr/2.8.3/modernizr.min.js"></script>
        <script src="https://code.angularjs.org/1.3.15/angular.min.js"></script>
        <script src="https://code.angularjs.org/1.3.15/angular-route.min.js"></script>
        <script src="/js/app.js"></script>
        <title>Website Title</title>
    </head>
    <body>
        <header>
            <div class="row">
                <div class="large-4 columns"><img src="/img/website-logo.png" alt="Website logo"></div>
                <div class="large-8 columns">
                        <a href="#" class="button right">555-555-5555</a>
                        <a href="#" class=" button right">Make an Appointment</a>
                </div>
            </div>
            <div class="row" id="nav-row">
                <nav class=" top-bar">
                    <section class=" top-bar-section">
                        <ul class="left">
                            <li><a href="/">Home</a></li>
                            <li><a href="/">Services</a></li>
                            <li><a href="/">Meet the Doctor</a></li>
                            <li><a href="/">Patients</a></li>
                            <li><a href="/referrals">Referring Doctors</a></li>
                            <li><a href="/">Contact Us</a></li>
                        </ul>
                    </section>
                </nav>
            </div>
            <div ng-view></div>
        </header>
        <footer class="row">
            <div class="large-5 columns">
                <h3>Location</h3>
                <div>123 ABC STREET</div>
                <div>Phone Number:  555-5555555</div>
                <div>Email: email@email.com</div>

            </div>
            <div class="large-4  columns">
                <h3>Quick Contact</h3>

                <div>Work:  555-5555555</div>
                <div>Cell:   555-5555555</div>
                <div>Fax: 555-5555555</div>
            </div>
            <div class="large-3 columns">
               Lorem Ipsum Sit Dolor Amet
            </div>

        </footer>


        <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
        <script src="//cdnjs.cloudflare.com/ajax/libs/foundation/5.5.2/js/foundation.min.js"></script>
        <script type="text/javascript" src="//cdn.jsdelivr.net/jquery.slick/1.5.0/slick.min.js"></script>
        <script src="/js/script.js"></script>
        <script>
            $(document).foundation();
        </script>
    </body>
</html>

Below are the three templates:

templates/welcome-page.html

<!-- templates/welcome-page.html -->
<div><h1>MAIN PAGE</h1></div>

templates/referrals.html

<!-- templates/referrals.html -->
<div><h1>REFERRALS PAGE</h1></div>

templates/404.html

<!-- templates/404.html -->
<div><h1>404</h1></div>

The files hierarchy is as follows:

- rcsbackend
  - templates
  - static
    - img
    - js
    - css
  - app.yaml
  - main.py

解决方案

Your solution got worked because by default html5mode is disabled, that is the reason why only the URL after # is recognized by the angular routing, and you putted route before your URL got it worked for you.

Solution

You need to enable html5mode in your application in order make your routing work, just by doing $locationProvider.html5Mode(true) in your configuration phase of angular.

Code

.config(function($locationProvider) {
    $locationProvider.html5Mode(true);
})

Now you could back to your old code, will solve your issue.

Update

To link around your application using relative links, you will need to set a in the of your document.

<base href="/">

Refer this SO answer would help you while enabling html5mode in application.

这篇关于与 Google App Engine 和 Flask 一起使用时,Angular 路由不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
Python最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆