错误:在angular7 webpack应用中无法解析位置:(?)的所有参数

时间:2018-10-20 12:09:46

标签: angular webpack angular6 webpack-4 angular7

在本地运行我未使用angular-cli创建的node.js-angular7-webpack4.21应用程序时,在浏览器中出现以下错误:

Error: Can't resolve all parameters for Location: (?).

我已经广泛研究了这个问题。首先,我没有任何组件,服务或其他名为“ Location”的东西,因此我不知道编译器很难解决哪个实体的参数。其次,我的整个应用程序中只有4个服务,彼此之间没有任何指向,因此这不是循环依赖项。我也没有忘记将'@Injectable()'装饰器正确放置在两个服务文件中。确实,经过三重和八重检查后,这不是问题。

此外,我已经用一个空的angular6项目(由0个服务和只有1个组件,在其模板中的div内仅包含单词)重现了此问题,在这种情况下,我仅对“ ApplicationModule”(而不是“ Location”)遇到相同的错误'。因此,即使我没有webpack错误,我也认为问题出在我的webpack配置中,因此我将把webpack.config.js和tsconfig.js文件放在下面,希望有人能对它们找到一些非常愚蠢的东西。 >

这是我的tsconfig.json:

{
  "compilerOptions": {
  "target": "es5",
  "lib": ["dom", "es2017", "es5", "es6", "es7"],
  "outDir": "dist",
  "module": "commonjs",
  "moduleResolution": "node",
  "sourceMap": true,
  "emitDecoratorMetadata": true,
  "experimentalDecorators": true,
  "removeComments": true,
  "noImplicitAny": false,
  "typeRoots": ["types"],
  "types": ["node"]
},
  "exclude": ["node_modules"]
}

这是我的webpack.config.js:

var webpack = require('webpack');
var helpers = require('./helpers');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const path = require('path'); // needed ?

module.exports = {

    mode: 'development',

    devtool: 'source-map',

    entry: {
        'main': helpers.root('app/main.ts'),
        'vendor': helpers.root('vendor.ts'),
        'polyfills': helpers.root('polyfills.ts')
    },

    output: {
        path: helpers.root('dist'),
        filename: '[name].js'
    },

    resolve: {
        extensions: ['.ts', '.js']
    },

    module: {
        rules: [{
                test: /\.ts$/,
                use: [{
                    loader: 'awesome-typescript-loader',
                    options: {
                        configFileName: helpers.root('tsconfig.json')
                    }
                }, 'angular2-template-loader?keepUrl=true']
            },
            {
                test: /\.html$/,
                use: 'html-loader'
            },
            {
                test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|ico)$/,
                use: 'file-loader?name=assets/[name].[hash].[ext]'
            },
            {
                test: /\.css$/,
                use: [
                    // 'style-loader',
                    MiniCssExtractPlugin.loader,
                    // 'to-string-loader',
                    'css-loader'
                ],
                exclude: [helpers.root('stylesheets')]
            },
            {
                test: /\.css$/,
                use: 'raw-loader',
                include: [helpers.root('stylesheets')]
            }
        ]
    },

    plugins: [
        new HtmlWebpackPlugin({
            template: 'index.html'
        }),
        new MiniCssExtractPlugin({
            // Options similar to the same options in webpackOptions.output
            // both options are optional
            filename: "[name].css",
            chunkFilename: "main.css"
        }),
        new webpack.ContextReplacementPlugin(
            // The (\\|\/) piece accounts for path separators in *nix and Windows
            // For Angular 5, see also https://github.com/angular/angular/issues/20357#issuecomment-343683491
            /\@angular(\\|\/)core(\\|\/)fesm5/,
            helpers.root('app'), // location of your src
            {
                // your Angular Async Route paths relative to this root drectory
            }
        )
    ]

};

谢谢你们。

2 个答案:

答案 0 :(得分:2)

将角度6.1.10升级到7.0.6和webpack 4.19.1时遇到了这个问题。 我做了this答案中描述的一些更改,并且该更改有效。简而言之:

添加到您的polyfill.ts

import 'core-js/es7/reflect';

我希望这会有所帮助。

答案 1 :(得分:0)

尝试从 package.json 中删除或升级 reflect-metadata

就我而言,我将其从文件中删除并从头开始重新安装节点模块。请注意,为了防止出现模块冲突,您可能还需要删除 package.json.lock

rimraf node_modules
npm i