【问题标题】:ui-router not working after minify with Gulp使用 Gulp 缩小后 ui-router 无法正常工作
【发布时间】:2023-04-08 01:06:01
【问题描述】:

我使用 Angular 制作应用程序并使用 Gulp 来缩小所有文件 css、javascript。但是如果我运行gulp build 来缩小然后在 dist 文件夹中运行文件'index.html',ui-router 不会加载模板。在文件 html 中,仍然像这样标记:<a ui-sref='link'> LINK </a>。你能帮助我真是太好了! 这是链接代码:

app.js https://gist.github.com/quyen91/d4ca009bb0be0e42a5a9dbded76ec45a

gulpfile.js https://gist.github.com/quyen91/bd04688302847a964e16586f9b468d29

错误:https://cdn.pbrd.co/images/1EjGpl8T.png

【问题讨论】:

  • 控制台有错误吗?
  • 错误:找不到模块“未定义”错误:[$injector:modulerr] 无法实例化模块 angularShopingCartApp 由于:我添加了两个链接 gulpfile.js 和 app.js。请查看并帮助我。
  • 检查文件拼接顺序是否正确。
  • 使用gulp-useref 保留连接文件的顺序。

标签: angularjs angular-ui-router minify


【解决方案1】:

Ui 路由器和许多工厂通常会停止工作,因为当我们不缩小时,我们总是将其提供者的正确名称注入到控制器或配置或运行或其他任何东西的参数中。但是当缩小时,会发生什么

run(function($state,$rootScope,$http){})
would become 
run(function(x,y,z){})

这会导致问题..

因此,单独编写提供程序的名称始终是一个好习惯。 这样函数就变成了

run(['$state','$rootScope','$http',function($state,$rootScope,$http){}]);
to 
run(['$state','$rootScope','$http',function(x,y,z){}]);

但这里的变量提供者是正确的,所以不会导致任何错误。

在你的情况下,运行方法应该是这样的

.run(['$rootScope', '$state', 'auth', 'jwtHelper', '$location', 'store',function($rootScope, $state, auth, jwtHelper, $location, store) {


    $rootScope.$on('$locationChangeStart', function() {
    // Get the JWT that is saved in local storage
    // and if it is there, check whether it is expired.
    // If it isn't, set the user's auth state
    var token = store.get('token');
    if (token) {
      if (!jwtHelper.isTokenExpired(token)) {
        if (!auth.isAuthenticated) {
          auth.authenticate(store.get('profile'), token);
        }
      }
    }
    else {
      // Otherwise, redirect to the home route
      $location.path('/about');
    }
  });

并且配置应该开始为

.config(['$stateProvider', '$urlRouterProvider', 'authProvider', '$httpProvider', '$locationProvider',
  'jwtInterceptorProvider',function ($stateProvider, $urlRouterProvider, authProvider, $httpProvider, $locationProvider,
  jwtInterceptorProvider) {
.....
}]);

【讨论】:

  • 我试过了。但它不起作用。缩小订单文件似乎有问题。我正在努力寻找解决方案。
  • gulp 中压缩文件的顺序应该与您的 index.html 脚本 src="" 中的顺序完全相同。遵循此命令可以确保它在未缩小时可以正常工作。如果在缩小后无法正常工作,则该命令不会成为问题..
  • 那么,你能检查一下我的 gulpfile.js 吗?你认为什么是问题?
  • 缩小后唯一可能出现的问题是 unpr 或未知提供者之一......那是因为我告诉过你.. 缩小会更改变量名称,但作为字符串提到的内容不会改变根本......所以最重要的检查是你必须声明你所有的工厂或服务和控制器以及任何其他东西,如上述方式的指令。
  • @babie 我浏览了你的 gulp 文件和 app.js .. 错误肯定是注入器的...缩小后名称有问题。
【解决方案2】:

您的 .run.config 块不是缩小安全的。

试试这个.run

.run(['$rootScope', '$state', 'auth', 'jwtHelper', '$location', 'store', function($rootScope, $state, auth, jwtHelper, $location, store) {


    $rootScope.$on('$locationChangeStart', function() {
    // Get the JWT that is saved in local storage
    // and if it is there, check whether it is expired.
    // If it isn't, set the user's auth state
    var token = store.get('token');
    if (token) {
      if (!jwtHelper.isTokenExpired(token)) {
        if (!auth.isAuthenticated) {
          auth.authenticate(store.get('profile'), token);
        }
      }
    }
    else {
      // Otherwise, redirect to the home route
      $location.path('/about');
    }
  });

}]);

config 执行相同操作。

【讨论】:

  • 谢谢。但是当我这样改变时它仍然不起作用。
  • @babie 似乎也缺少 angular 和 firebase,您是否引用了这些库?
  • 愚蠢的问题,但是当 minfication 被注释掉时,您的应用程序是否正常工作?
  • here 清理一下,解决了吗?
  • 好的,最后一个想法,如果你使用 git 并且缩小在某个时候工作(并提交),你可以使用 git bisect 来找出问题所在
猜你喜欢
  • 2016-10-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多