【发布时间】:2016-10-09 04:58:00
【问题描述】:
我在 ionic 项目中有一个 AngularJS 路由文件,我有非常多的路由,大约有一百条。如下图。
(function() {
'use strict';
angular.module('application').config(function ($stateProvider, $urlRouterProvider) {
$stateProvider
.state('app', {
url: '/app',
abstract: true,
templateUrl: 'templates/menu.html',
controller: 'menuController'
})
.state('app.signup', {
url: '/signup',
views: {
'menuContent': {
templateUrl: 'templates/signup.html',
controller: 'signupController'
}
}
})
.state('app.home', {
url: '/home',
views: {
'menuContent': {
templateUrl: 'templates/home.html',
controller: 'homeController'
}
}
})
.state('app.user', {
url: '/user',
views: {
'menuContent': {
templateUrl: 'templates/user.html',
controller: 'userController'
}
}
});
// if none of the above states are matched, use this as the fallback
$urlRouterProvider.otherwise('/app/signup');
});
})();
但是,我想更好地解决高比例的代码重复问题,这在声纳上显示。我在这里看到了一个模型https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#style-y270 但是我不知道如何让我的代码变成这个结构,或者有什么方法可以减少重复的百分比,我该怎么做?
【问题讨论】:
-
如果想重用路由配置结构来创建配置,你可以编写一个函数来接受所有需要的参数并创建配置对象,但我不推荐它。在这种情况下,路线应该清晰,可读性比可重用性更重要。如果您担心代码长度,任何编辑器都可以折叠代码块。
标签: javascript angularjs sonarqube angular-routing