gpt4 book ai didi

javascript - 使用 ui 路由器导航到非默认状态

转载 作者:行者123 更新时间:2023-11-29 15:36:03 26 4
gpt4 key购买 nike

几天来,我一直在尝试解决这个问题,我在互联网上阅读的所有内容都表明它应该可以正常工作。只要我在应用程序中,我的应用程序路由就可以正常工作,但是当我尝试复制和粘贴 url 时,它会丢失其状态并重定向到主页。

* 编辑 我开始认为这是因为 app.yaml 正在处理“无论如何提供 index.html”,似乎无论 app.yaml 做什么它都会剥离信息,这就是为什么 $state 的 url 为“”编辑*

我将我的服务器配置为无论 url 是什么都返回 index.html,因此它使用我仍然粘贴在浏览器栏中的 url 进入路由逻辑。当我在注入(inject) $state 和 $stateParams 的运行 block 中放置一个断点时,它显示当前 url 为“”,当它使用我的路由到达配置 block 时,它会转到 .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。我在运行和配置 block 中放置了断点,当它到达那里时 url 没有改变,所以我 90% 确定它不是服务器问题,但我不知道它可能是什么。在我去/a 之前我确实遇到过这个问题,它会返回一个错误 cannot get/a。所以我修复了 serve up index 一直在变化的问题,这就是为什么我非常有信心我正确地设置了服务器。

据我所知,从我所有的研究来看,这应该可以在没有任何配置的情况下工作,除了在 Angular 方面。也就像我说的,我可以在我的应用程序中很好地导航,所以看起来我的状态设置正确。

只有我能找到类似问题的 SO 问题似乎并不适用,除非我遗漏了什么。 1 是关于我修复的服务索引问题,另一个是他遗漏了一个“/”,我认为我没有。

How can I go directly to a state with ui-router without first navigating to index.html

Can't navigate to ui-router state with URL

最佳答案

万一有人想知道问题是正则表达式格式搞砸了

//this is wrong
url: '/{abc:[a|b|c|d]}'

//this is right
url: '/{abc:a|b|c|d}'

我认为它在内部工作正常,因为我将 ui-sref 绑定(bind)到状态,当我尝试复制和粘贴 url 时,它依赖于匹配 url。无论哪种方式它最终工作

关于javascript - 使用 ui 路由器导航到非默认状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28977878/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com