2017-04-05 185 views
1

在角度ui路由器tutorial中,所有状态都在同一个js文件中定义。Angularjs ui路由器状态定义

myApp.config(function($stateProvider) { 
    // An array of state definitions 
    var states = [ 
    { name: 'hello', url: '/hello', component: 'hello' }, 
    { name: 'about', url: '/about', component: 'about' }, 

    { 
     name: 'people', 
     url: '/people', 
     component: 'people', 
     resolve: { 
     people: function(PeopleService) { 
      return PeopleService.getAllPeople(); 
     } 
     } 
    }, 

    { 
     name: 'people.person', 
     url: '/{personId}', 
     component: 'person', 
     resolve: { 
     person: function(people, $stateParams) { 
      return people.find(function(person) { 
      return person.id === $stateParams.personId; 
      }); 
     } 
     } 
    } 
    ] 

    // Loop over the state definitions and register them 
    states.forEach(function(state) { 
    $stateProvider.state(state); 
    }); 
}); 

但是,当创建一个大的应用程序时,我们可能会有很多状态。通常情况下,一个国家会调用一个调用模板并可能使用服务等的组件。

因此,我使用一个单独的js文件来定义状态,同为我组件,模板,做服务,...

所以我可以有例如:

  • 家.stt.js(用于状态)
  • home.srv.js(服务)
  • home.cpt.js(对于成分)
  • home.html的(用于视图)

这是一个很好的做法吗?或者最好是在同一个文件中定义所有状态?

+0

在同一个文件中定义的所有状态都不好 – Akashii

+0

哈哈埃迪,你再次=)我真的很想知道你心中是怎么回事。你的方法是“不同的”:P – lin

回答

1

它更具可读性,易于理解和组织,可以将它们拆分为单独的文件。据推测,你有一个很好的目录结构,所以主要领域将有自己的目录和子目录等,你的国家配置文件可以进入他们遵循相同的层次结构。

这是我自己从几个中型到大型项目的经验,其中结构对于易用性如此重要。