几天来,我一直试图断断续续地解决这个问题,我在互联网上读到的所有内容都表明它应该可以正常工作。只要我在应用程序中,我的应用程序路由就可以正常工作,但是当我尝试复制和粘贴 URL 时,它会丢失其状态并重定向到主页。
* 编辑 我开始认为这是因为 app.yaml 正在处理“无论如何都提供 index.html”,似乎无论 app.yaml 做什么都会剥离信息,这就是为什么 $state 的 url 为“” 编辑 *
我将我的服务器配置为返回 index.html,无论 url 是什么,因此它使用我粘贴在浏览器栏中的 url 进入路由逻辑。当我在注入 $state 和 $stateParams 的运行块中放置断点时,它显示当前 url 是“”,当它使用我的路由到达配置块时,它会转到 .otherwise('/') 并重定向我到应用程序的开头。
app.run([
'$rootScope', '$state', '$stateParams', function($rootScope, $state, $stateParams){
$rootScope.$state = $state;
$rootScope.$stateParams = $stateParams;
console.log($rootScope);
}])
.config([
'$stateProvider', '$urlRouterProvider', '$locationProvider', function($stateProvider, $urlRouterProvider, $locationProvider){
$locationProvider.html5Mode(true);
$urlRouterProvider.otherwise('/');
$stateProvider
.state('home', {
url: '/',
templateUrl: 'app/layout/home.html',
controller: function($stateParams){
debugger;
}
})
.state('other', {
url: '/{abc:[a|b|c|d]}',
templateUrl: 'app/layout/other.html'
});
}]);
这是我的 app.yaml
application: app
version: 1
runtime: python27
api_version: 1
threadsafe: no
handlers:
- url: /(.*\.(gif|png|jpg|ico|js|css|html))
static_files: \1
upload: (.*\.(gif|png|jpg|ico|js|css|html))
- url: /robots.txt
static_files: robots.txt
upload: robots.txt
- url: .*
static_files: index.html
upload: index.html //I am relying on this as a fallback for any 404's, it seems to work
skip_files:
- ^(.*/)?#.*#$
- ^(.*/)?.*~$
- ^(.*/)?.*\.py[co]$
- ^(.*/)?.*/RCS/.*$
- ^(.*/)?\..*$
- node_modules/*
- grunt/*
- Gruntfile.js
- less/*
- lib/*
这是我的 main.py,它只是谷歌给你的默认值
import os
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from google.appengine.ext.webapp import template
class MainHandler(webapp.RequestHandler):
def get (self, q):
if q is None:
q = 'index.html'
path = os.path.join (os.path.dirname (__file__), q)
self.response.headers ['Content-Type'] = 'text/html'
self.response.out.write (template.render (path, {}))
def main ():
application = webapp.WSGIApplication ([('/(.*html)?', MainHandler)], debug=True)
util.run_wsgi_app (application)
if __name__ == '__main__':
main ()
因此,当我访问 localhost:8000 时,我就可以正常访问主页。我单击一个链接转到其他链接,最终到达 localhost:8000/a 或 /b 等。如果我将该链接复制并粘贴到另一个选项卡中,我最终到达 localhost:8000。我在 run 和 config 块中设置了断点,并且 url 到达那里时还没有改变,所以我 90% 确定它不是服务器问题,但我不知道它可能是什么。在我去 /a 之前我确实遇到过这个问题,它会返回一个错误,无法获取 /a。所以我修复了这个问题,服务索引一直在变化,这就是为什么我相当有信心我正确地设置了服务器。
据我从我所有的研究中可以看出,这应该只是在没有任何配置的情况下在角度方面工作。就像我说的,我可以很好地在我的应用程序中导航,所以看起来我的状态设置正确。
只有我能找到类似问题的 SO 问题似乎并不适用,除非我遗漏了什么。1 是关于我修复的服务索引问题,另一个是他缺少一个我认为不是的“/”。