如何在webpack中注入Angular模块?

时间:2015-02-21 14:55:56

标签: angularjs commonjs webpack

我从 npm 安装 ngRoute ,我的webpack默认加载default.js

webpack.config.js

'use strict';

var path = require('path'),
    webpack = require("webpack"),
    AngularPlugin = require('angular-webpack-plugin');

module.exports = {
  resolve: {
    root: [path.join(__dirname, "bower_components")]
  },
  plugins: [
  new webpack.ResolverPlugin(
    [new webpack.ResolverPlugin.DirectoryDescriptionFilePlugin("bower.json", ["main"])],
    ["normal", "loader"]
  ),
  new webpack.ProvidePlugin({
      $: "jquery",
      jQuery: "jquery",
      "windows.jQuery": "jquery",
      _: "underscore"
  }),
  new AngularPlugin()
  ],
  entry: {
    all: "./app/assets/javascript/default",
  },
  output: {
    path: __dirname + '/dist/assets/javascript',
    filename: "[name].bundle.js",
    chunkFilename: "[id].bundle.js"
  },
}

default.js

 // default.js
 'use strict';

 require('angular-route');
 require('./angularLoad')();

angularLoad.js

// angularLoad.js
module.exports = function () {
   var app = angular.module('myApp', ['ngRoute']);
   app.controller('ctroe', function(){
     console.log("aaaaaaaaaaaa");
   });
};

当我运行webpack时,收到错误消息:

  

找不到模块:错误:无法解析模块' ngRoute'在....

角度注入时出现此错误。有什么建议吗?感谢大家。 :)

1 个答案:

答案 0 :(得分:3)

您可以像这样设置ngRouteangular-route的别名:

resolve: {
    alias: {
        'ngRoute': 'angular-route'
    }
}