2016-11-18 54 views
0

我正在使用ui-router使用ui-router,並且收到以下錯誤消息。UI路由器無法解析狀態錯誤消息

Error: Could not resolve 'static.about' from state 'static'

我不知道爲什麼。我想要實現的是在我的應用程序中有2個部分,靜態部分,主頁,關於,登錄,註冊和應用程序端,這些會像儀表板,用戶配置文件等,我認爲我需要設置我的ui-這樣的路由器,

app 
.config(['$stateProvider', '$urlRouterProvider', 
function($stateProvider, $urlRouterProvider){ 
// any unknown URLS go to 404 
$urlRouterProvider.otherwise('/404'); 
// no route goes to index 
$urlRouterProvider.when('', '/home'); 
// use a state provider for routing 
$stateProvider 
    .state('static', { 
     url: '/home', 
     templateUrl: '', 
     views : { 
      header : { 
       templateUrl : 'app/components/shared/header.html' 
      }, 
      main: { 
       templateUrl : 'app/components/home/views/home.view.html' 
      } 
     } 
    }) 
    .state('static.about', { 
     // we'll add another state soon 
     url: '/about', 
     templateUrl: 'app/components/about/views/about.view.html', 
    }) 
    .state('app', { 
     abstract: true, 
     templateUrl: '' 
    }) 
    .state('app.dashboard', { 
     url: 'app/dashboard', 
     templateUrl : '', 
    })   
}]); 

但是,這會返回已經提到的錯誤。我想這樣設置它的原因是,應用程序的兩面也有非常不同的佈局,所以我認爲我應該能夠將不同的佈局插入我的?

然而,我遇到的最大的問題是錯誤,其次是鏈接正在生成,當我點擊它應該去/ about,但是它會去/ home/about。

所有我想在早期階段實現的是對我的「靜態」的頁面共享一個導航,並有可更換的主要部分,而對於「應用程序」頁面有一個完全地新的佈局/父模板

回答

0

您需要在子視圖上設置parent屬性。

.state('static.about', { 
    url   : '/about', 
    templateUrl : 'app/components/about/views/about.view.html', 
    parent  : 'static' 
}) 

另外檢查這個問題的答案Angular ui-router : Parent & Child Views

我想,當你做$state.go(static.about)它只是去homepage,因爲你正在做otherwise或類似的東西。

像:$urlRouterProvider.when('', '/home');

Angular-Ui-Router解決的主要route當事情是不行的。

另請注意,您在templateUrl的末尾添加了額外的,

0

上面的狀態結構是可以的。我創建了一個plunker to demonstrate it here

在根狀態「靜態」我們只是不使用templateUrl ..它會跳過無論如何,因爲我們定義views : {}

.state('static', { 
     url: '/home', 
     // we want to have views : {} so avoid this shorthand template 
     // templateUrl: '', 
     views: { 
     header: { 
      templateUrl : 'app/components/shared/header.html', 
     }, 
     main: { 
      templateUrl : 'app/components/home/views/home.view.html', 
     } 
     } 
    }) 

和一個孩子的狀態'靜態的。關於」可以使用'^'領先的標誌,它的網址從根本

.state('static.about', { 
     // here we use a switch to inform - this url starts from root 
     //url: '/about', 
     url: '^/about', 
     templateUrl: 'app/components/about/views/about.view.html', 
    }) 

並與這些電話重置,所有的工作

<a ui-sref="static"> 
<a ui-sref="static.about"> 

檢查它在行動here

的錯誤消息:

Error: Could not resolve 'static.about' from state 'static'

如果我們在狀態定義(或調用方)中有一個錯誤,通常會發生。但這不是問題。或者,如果我們忽略加載定義文件...

相關問題