【问题标题】:How I can write better routers of angular我如何编写更好的角度路由器
【发布时间】: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


【解决方案1】:

说实话:您要修复的是完全有效的代码。这是某种配置 - 所以难怪 SonarQube 将其检测为复制粘贴。但这并不意味着您应该修复某些东西。

相反,我建议您将 SonarQube 配置为忽略此特定文件。您可以在 SonarQube 的项目管理设置中执行此操作:“Analysis Scope > Duplications”。

请阅读"Narrowing the Focus" 文档页面以了解更多信息。

【讨论】:

  • 非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-12-29
  • 2017-03-25
  • 1970-01-01
  • 2016-06-22
  • 1970-01-01
  • 1970-01-01
  • 2018-10-12
相关资源
最近更新 更多