Angular在使用webpack时不解析构造函数依赖项

问题描述 投票:0回答:1

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

错误

enter image description here

Webpack配置

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 {}

任何人都可以帮我解决这个问题吗?

angular webpack angular6 webpack-4
1个回答
0
投票

我的问题是Typescript没有解析装饰器。为了解决这个问题,我在tsconfig.json文件中设置了emitDecoratorMetadata = true

enter image description here

Read more about emitDecoratorMetadata

© www.soinside.com 2019 - 2024. All rights reserved.