我在一個離子項目中有一個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,但我不知道如何讓我的代碼到這個結構,或者一種可以減少重複百分比的方法,我該怎麼辦?
如果想要重新使用路由配置結構來創建配置,可以編寫一個接受所有需要的參數並創建配置對象的函數,但我不會推薦它。在這種情況下,路線應該清晰並且可讀性比可重用性更重要。如果你擔心你的代碼長度,任何編輯器都可以摺疊代碼塊。 –