【问题标题】:Can't resolve $element after minification缩小后无法解析 $element
【发布时间】:2017-09-28 23:49:39
【问题描述】:

我有一个奇怪的错误,它只在 karaf 中运行我的 web 应用程序时出现,而不是在 webpack-dev-server 上。当我打开对话框时从 Karaf 运行 Web 应用程序时,我在浏览器控制台中收到此错误

angular.js:14516 Error: [$injector:unpr] Unknown provider: nProvider <- n

http://errors.angularjs.org/1.6.3/$injector/unpr?p0=nProvider%20%3C-%20n

组件负责显示一个表格。这些列应该是可编辑的,所以我已经从角度材料演示中实现了一个库存对话框。请参阅 -> https://material.angularjs.org/latest/demo/dialog 和带有搜索选项的多选 -> https://material.angularjs.org/latest/demo/select

代码如下:

    ctrl.editColumns = (event) => {
      $mdDialog.show({
        template: require('./edit-column-dialog.template.html'),
        clickOutsideToClose: true,
        scope: $scope,
        preserveScope: true,
        controller: EditColumnsDialogController
      })
    }

    function EditColumnsDialogController ($element) {
      ctrl.searchTerm = ''
      ctrl.copySelectedColumns = ctrl.selectedColumns

      // The md-select directive eats keydown events for some quick select
      // logic. Since we have a search input here, we don't need that logic.
      $element.find('input').on('keydown', (ev) => {
        ev.stopPropagation()
      })
    }

我使用 $element 的原因在上面的评论中详述。我无法理解的是,这在 web pack 开发服务器上运行良好。 webpack设置的唯一区别是:

// Add build specific plugins
if (isProd) {
  config.plugins.push(

  // Only emit files when there are no errors
  new webpack.NoEmitOnErrorsPlugin(),

  // Minify all javascript, switch loaders to minimizing mode
  new webpack.optimize.UglifyJsPlugin({
    sourceMap: true
  }),

  // Copy assets from the public folder
  // Reference: https://github.com/kevlened/copy-webpack-plugin
  new CopyWebpackPlugin([{
    from: path.join(__dirname, '/app/public')
  }])
)}

非常感谢任何答案.. 或隔离问题的建议!

【问题讨论】:

    标签: angularjs webpack angular-material webpack-dev-server apache-karaf


    【解决方案1】:

    缩小器会破坏使用Implicit Annotation 的代码。相反,请使用$inject Property Annotation:

      //USE $inject property annotation
      EditColumnsDialogController.$inject = ["$element"];
    
      function EditColumnsDialogController ($element) {
          ctrl.searchTerm = ''
          ctrl.copySelectedColumns = ctrl.selectedColumns
    
          // The md-select directive eats keydown events for some quick select
          // logic. Since we have a search input here, we don't need that logic.
          $element.find('input').on('keydown', (ev) => {
            ev.stopPropagation()
          })
      }
    

    来自文档:

    隐式注释

    小心:如果您打算minify您的代码,您的服务名称将被重命名并破坏您的应用程序。

    — AngularJS Developer Guide - Implicit Dependency Annotation

    【讨论】:

    • 谢谢。有趣的是,如果我删除隐式注释并只使用它不起作用的 $inject 属性注释 - 我需要同时使用两者!
    猜你喜欢
    • 2022-10-07
    • 2018-04-29
    • 2017-11-12
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-20
    • 1970-01-01
    相关资源
    最近更新 更多