Javascript Angularjs ui路由器状态定义
在angular ui路由器中,所有状态都在同一个js文件中定义Javascript Angularjs ui路由器状态定义,javascript,angularjs,angular-ui-router,Javascript,Angularjs,Angular Ui Router,在angular ui路由器中,所有状态都在同一个js文件中定义 myApp.config(function($stateProvider) { // An array of state definitions var states = [ { name: 'hello', url: '/hello', component: 'hello' }, { name: 'about', url: '/about', component: 'about' }, {
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文件来定义状态,就像我对组件、模板、服务
例如,我可能有:
- home.stt.js(针对州)
- home.srv.js(用于服务)
- home.cpt.js(用于组件)
- home.html(用于查看)
这是一种好的做法吗?或者最好将所有状态定义在同一个文件中?将它们拆分为单独的文件更容易阅读、理解和组织。假设您有一个良好的目录结构,所以主要区域将有自己的目录和子目录等,您的州配置文件可以进入它们以遵循相同的层次结构
这是我自己在几个大中型项目中的经验,这些项目的结构对于易用性非常重要。在同一个文件中定义所有状态不是很好哈哈,艾迪,你再次=)我真的想知道你心里在想什么。你的方法是“不同的”:P