Webpack和Angular Error无法读取属性' setGlobalVar'

时间:2017-05-10 19:23:15

标签: angular typescript webpack

运行我的程序的UI时,开发人员工具给出了错误Cannot read property 'setGlobalVar' of null ; Zone: <root> ; Task: Promise.then ; Value: TypeError: Cannot read property 'setGlobalVar' of null.

我的webpack.config.js:

const webpack = require('webpack');
const path = require('path');
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {
    entry: {
        polyfills: './app/polyfills.ts',
        app: './app/main.ts'
    },
    output: {
        path: path.resolve(__dirname, './dist'),
        filename: '[name].js',
        sourceMapFilename: '[file].map'
    },
    module: {
        rules: [
            //Order of loaders is important!
            {
                test: /\.ts(x?)$/,
                exclude: ['node_modules'],
                use: [{ loader: 'babel-loader' },{ loader: 'ts-loader' }, { loader: 'angular-router-loader' }, { loader: 'angular2-template-loader' }],
            },
            {
                test: /\.sass$/,
                exclude: ['node_modules'],
                use: [{ loader: 'css-to-string-loader' }, { loader: 'raw-loader' }, { loader: 'sass-loader', options: { sourceMap: true, sourceMapContents: true } }]
            },
            {
                test: /\.css$/,
                exclude: ['node_modules'],
                use: [{ loader: 'css-to-string-loader' }, { loader: 'raw-loader' }, { loader: 'css-loader' }]
            },
            {
                test: /\.html$/,
                exclude: ['node_modules', 'index.html'],
                use: [{ loader: 'raw-loader' }]
            },
            {
                test: /\.(otf|eot|svg|png|ttf|woff|woff2).*$/,
                exclude: ['node_modules'],
                use: [{ loader: 'url-loader' }]
            }
        ]
    },
    resolve: {
        extensions: ['.tsx', '.ts', '.js', '.css'],
        modules: ["node_modules", "."]
    },
    plugins: [

      new webpack.optimize.CommonsChunkPlugin({
          names: ['vendor'],
          minChunks: function (module, count) {
              // creates a common vendor js file for libraries in node_modules
              return module.context && module.context.indexOf('node_modules') !== -1;
          }
      }),

      new webpack.optimize.CommonsChunkPlugin({
          names: ['meta'],
          chunks: ['vendor', 'app']
      }),

      new HtmlWebpackPlugin({
          template: 'index.cshtml',
          filename: 'index.cshtml',
          inject: 'head'
          , chunks: ['app', 'vendor', 'polyfills', 'meta']
          , chunksSortMode: 'dependency'
          , hash: false
      }),

      new webpack.ContextReplacementPlugin(
        /angular(\\|\/)core(\\|\/)@angular/, //Update for Angular 4
        root('./app'),
        {}
      ),
    ]
};
function root(__path) {
    return path.join(__dirname, __path);
}

我正在使用Angular 4.1.0并且此问题错误仅在从Angular 2更新到Angular 4之后出现。在进行Google搜索时对此问题的支持非常少,因此非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

信不信由你,这个问题与你的Webpack配置无关。

要通过错误,请评论或删除环境配置文件中的disableDebugTools()调用,通常是environment.ts文件。