簡體   English   中英

使用ui路由器導航到非默認狀態

[英]Navigating to non-default state with ui router

我一直在試圖解決這個問題幾天,我在互聯網上看到的一切都說它應該可行。 只要我在應用程序中,我的應用程序路由就可以正常運行,但是當我嘗試復制並粘貼網址時,它會丟失其狀態並重定向到主頁。

*編輯 我開始認為這是因為app.yaml正在處理“不管是什么”的“提供index.html”,它似乎無論app.yaml是什么,它都會消除信息,這就是為什么$ state有一個“”的網址 編輯*

我的服務器配置為返回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。 我在運行和配置塊中放置斷點,並且它到達那里時url沒有改變,所以我90%確定它不是服務器問題,但我不知道它可能是什么。 我之前確實遇到了問題,我會去/ a並且它會返回錯誤無法獲取/ a。 所以我一直修改了服務索引,這就是為什么我相當自信我正確設置了服務器。

據我所有的研究而言,這應該只在角度方面沒有任何其他配置。 也像我說我可以在我的應用程序中導航很好,所以似乎我的狀態設置正確。

除非我遺漏了某些東西,否則我遇到類似問題的SO問題似乎並不適用。 1是關於我修復的服務索引問題,另一個是他錯過了'/'我不認為我是。

如何在沒有首先導航到index.html的情況下直接進入使用ui-router的狀態

無法使用URL導航到ui-router狀態

如果有人想知道問題是正則表達式格式搞砸了

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

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

我認為它內部工作正常,因為我有ui-sref綁定到狀態,當我嘗試復制並粘貼網址時,它依賴於匹配網址。 無論哪種方式,它最終工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM