gpt4 book ai didi

javascript - Angular 组件服务注入(inject)不适用于 Webpack

转载 作者:行者123 更新时间:2023-11-30 14:49:29 25 4
gpt4 key购买 nike

问题

我遇到一个问题,如果我在组件的构造函数中使用 @Inject 装饰器(即 @Inject(TestService)),Angular 的 DI 似乎才起作用注入(inject)服务时。每当我使用标准 Angular CLI 测试项目时,这似乎工作得很好(不使用 @Inject),但是当我使用 webpack 手动创建没有 Angular CLI 的相同项目时(如下所示),我总是得到:

compiler.js:485 Uncaught Error: Can't resolve all parameters for AppComponent: (?).

代码

./src/app/main.js

import { enableProdMode } from '@angular/core';
import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';

import { AppModule } from './app.module';

platformBrowserDynamic().bootstrapModule(AppModule);

./src/app/test.service.ts

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

@Injectable()
export class TestService {
write() {
console.log('SOMETHING');
}
}

./src/app/app.module.js

import 'zone.js/dist/zone';

import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';

import { AppComponent } from './app.component';

import { TestService } from './test.service';

@NgModule({
imports: [
BrowserModule,
],
declarations: [
AppComponent,
],
providers: [TestService],
bootstrap: [AppComponent],
})
export class AppModule { }

./src/app/app.component.ts

import { Component, Inject } from '@angular/core';
import { TestService } from './test.service';

@Component({
selector: 'app-root',
styles: [],
template: 'Hello',
})
export class AppComponent {
// Works if you uncomment the @Inject decorator below
constructor(/*@Inject(TestService)*/ test: TestService) {
test.write();
}
}

./src/index.html

<!DOCTYPE html>
<html lang="en">
<head>
<title>test</title>
</head>
<body>
<app-root></app-root>
</body>
</html>

./webpack.common.js

const path = require('path');
const webpack = require('webpack');
const CleanWebpackPlugin = require('clean-webpack-plugin');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const CopyWebpackPlugin = require('copy-webpack-plugin');

const isVendorModule = (module) => {
if (!module.context) {
return false;
}

const nodeModule = module.context.indexOf('node_modules') !== -1;
return nodeModule;
};

module.exports = {
devServer: {
contentBase: './src',
historyApiFallback: true,
quiet: true,
stats: 'minimal'
},
entry: {
'app/main': './src/app/main.ts'
},
module: {
rules: [
{
test: /\.tsx?$/,
use: 'ts-loader'
},
{
test: /\.(html)$/,
loader: 'html-loader'
}
]
},
plugins: [
new CleanWebpackPlugin([
path.resolve(__dirname, 'build/*')
]),
new webpack.optimize.CommonsChunkPlugin({
name: 'app/vendor',
chunks: ['app/main'],
minChunks: isVendorModule
}),
new HtmlWebpackPlugin({
template: './src/index.html',
filename: 'index.html',
chunks: ['app/vendor', 'app/main']
})
],
resolve: {
extensions: ['.tsx', '.ts', '.js']
},
output: {
filename: '[name].js',
path: path.resolve(__dirname, 'build')
}
};

./tsconfig.json

{
"compilerOptions": {
"moduleResolution": "node",
"noImplicitAny": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"module": "es6",
"target": "ES2016",
"allowJs": true,
"sourceMap": true,
"types": [],
"baseUrl": ".",
"paths": {}
},
"exclude": [
"node_modules",
"dist",
"build"
]
}

可重现的示例

您可以在GitHub here上获取此问题示例项目的源代码。 。然后运行:

  • npm 安装
  • npm 运行服务器
  • 访问本地主机:8080

最佳答案

app.module.ts 中导入 core-js,这会添加 polyfill。

import 'core-js';

关于javascript - Angular 组件服务注入(inject)不适用于 Webpack,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48393726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com