使用webpack时Angular无法解析构造函数依赖项

时间:2018-08-30 20:04:30

标签: angular webpack angular6 webpack-4

我已经使用webpack 4成功地用示例组件加载了Angular应用程序,但是当我在构造函数中传递依赖项时,出现以下错误。最初,我传递了 ActivateRoute 参数,并且我认为此错误特定于此特定依赖项,但是后来我尝试使用创建自己的示例服务也失败了

错误

enter image description here

Web Pack配置

module.exports = {
    mode: 'development',
    entry: {
        'main.microfrontendhost.bundle': ['main.ts', "./somestyle.css"]
    },
    optimization: {
        splitChunks: {

               cacheGroups: {
                commons: {
                    test: /[\\/]node_modules[\\/]/,
                    name: 'vendors',
                    chunks: 'all'
                }
            }
        }
    },
    output: {
        publicPath: '/dist/',
        filename: '[id].js',
        chunkFilename: "[name].js",
        path: path.resolve(__dirname, 'dist'),

    },
    module: {
        rules: [{
                test: /\.(sa|sc|c)ss$/,
                use: [
                    'exports-loader?module.exports.toString()',
                    {
                        loader: MiniCssExtractPlugin.loader,
                    },
                    'css-loader',
                    'sass-loader',
                ]
            },
            {
                test: /\.js$/,
                exclude: [path.resolve(__dirname, 'node_modules')],
                loader: 'babel-loader',
            },
            {
                // This plugin will allow us to use html templates when we get to the angularJS app
                test: /\.html$/,
                exclude: /node_modules/,
                loader: 'html-loader',
            },
            {
                test: /\.tsx?$/,
                loader: 'ts-loader',
            }
        ]
    },
    node: {
        fs: 'empty'
    },
    resolve: {
        modules: [
            __dirname,
            'node_modules',
        ],
        extensions: [".ts", ".tsx", ".js"]
    },
    plugins: [
        new CleanWebpackPlugin(['dist']),
        new HashOutput({
            validateOutput: false,
        }),
        new MiniCssExtractPlugin({
            filename: 'microfrontend.bundle.css',
            chunkFilename: '[name].css'
        })
    ],
    devtool: 'source-map',
    externals: [],
    devServer: {
        historyApiFallback: true
    }
};

组件

import { Component, OnInit,Injector } from "@angular/core";
import { MicroFrontEndHostService } from "./microfrontendhost.service";
@Component({
  selector: "microfrontend-host",
  template: `
    <div class="app2class" style="margin-top: 100px;">
      Angular 
    </div>

  `
})
export class MicroFrontEndHostComponent implements OnInit {
  constructor(
    //services failing here
    private microFrontEndHostService:MicroFrontEndHostService
  ) {

  }
  ngOnInit(): void {

  }
}

服务

import { Injectable } from "@angular/core";

@Injectable()
export class MicroFrontEndHostService {
  constructor() {}

}

AppModule

@NgModule({
  imports: [
    BrowserModule,
    RouterModule.forRoot(
      [
        {
          path: "",
          redirectTo: "Portal",
          pathMatch: "full",
        },
        {
          path: "Portal",
          component: MicroFrontEndHostComponent,
          data: { applicationId: "0002bc92-240b-4f6a-9c26-1b8ba57c964c" }
        }
      ],
      { enableTracing: true, useHash: true } // <-- debugging purposes only
    )
  ],
  providers: [MicroFrontEndHostService],
  schemas: [],
  exports:[],
  declarations: [HostComponent, MicroFrontEndHostComponent],
  bootstrap: [HostComponent]
})
export default class HostModule {}

有人可以帮助我解决此问题吗?

1 个答案:

答案 0 :(得分:0)

我的问题是Typescript无法解析装饰器。要解决此问题,我在 tsconfig.json 文件

中设置了 emitDecoratorMetadata = true

enter image description here

Read more about emitDecoratorMetadata