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

Navigating to non-default state with ui router

本文关键字:默认 状态 导航 ui 路由器 使用      更新时间:2023-09-26
几天

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

*编辑 我开始认为这是因为 app.yaml 正在处理"无论如何.html服务索引",似乎无论 app.yaml 做什么它都会剥离信息,这就是为什么$state有一个 url " 编辑*

我将我的服务器配置为返回索引.html无论 url 是什么,因此它使用我粘贴的 url 仍然在浏览器栏中进入路由逻辑。当我在注入$state的运行块中放置一个断点并$stateParams它显示当前 url 是"时,当它到达带有我的路由的配置块时,它会转到 .else('/') 并将我重定向到应用程序的开头。

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'
         });
}]);

这是我的应用程序.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时,我就可以进入主页。我单击一个链接转到其他链接,最终到达本地主机:8000/a 或/b 等。如果我将该链接复制并粘贴到另一个选项卡中,我最终会到达 localhost:8000。 我将断点放在运行和配置块中,并且 url 到达那里时没有更改,所以我 90% 确定这不是服务器问题,但我不知道它可能是什么。我之前确实遇到了问题,我会去/a,它会返回一个错误无法得到/a。所以我修复了它,服务索引一直在变化,这就是为什么我相当有信心我正确设置了服务器。

据我所有的研究所知道,除了角度侧之外,这应该没有任何配置。就像我说的那样,我可以在我的应用程序中很好地导航,所以看起来我的状态设置正确。

除非我遗漏了一些东西,否则我能找到的具有类似问题的 SO 问题似乎并不适用。 一个是关于我修复的服务索引问题,另一个是他缺少我认为我不认为的"/"。

如何在不先导航到索引的情况下直接进入 ui-router 的状态.html

无法使用 URL 导航到 UI 路由器状态

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

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

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