1

我正在使用 Angular 5 创建 Outlook 加载项。我使用 Yeoman 生成文件,而不是 angular-cli。我有一个我写的服务,MeetingsService,我试图将它注入我的一个组件,BehaviorsSearchComponent。我在 AppModule 中将我的服务列为提供者,并将其导入到 BehaviorsSearchComponent 中。当我运行我的加载项时,我得到了错误

Can't resolve all parameters for BehaviorsSearchComponent: (?)

如果我这样做,它会起作用

constructor (@Inject(MeetingsService) meetingsService: MeetingsService)

但我希望能够在没有@Inject 的情况下做到这一点。我还尝试在我的 taskpane.ts 文件中导入 core-js/es7/reflect,这是我的根文件。我检查了我的 webpack.config 并为我的条目 polyfill 指定了 babel/polyfill。我不认为这是一个循环依赖问题;我安装了该软件包,但在构建中没有收到任何警告。

有什么我想念的吗?我的 package.lock.json 中的依赖项是否存在问题?我的 @angular 包是 5.2.11 或 5.2.5。

app.module.ts

import { NgModule } from '@angular/core';
import { BrowserModule } from '@angular/platform-browser';
...
import { MeetingsService } from './meetings.service';
import AppComponent from './app.component';
import { BehaviorsSearchComponent } from './behaviors-search/behaviors-search.component';
import { EnableAddInComponent } from './enable-addIn/enable-addIn.component';

@NgModule({
  imports: [
    BrowserModule,
    ...
  ],
  providers: [
    MeetingsService
  ],
  declarations: [
    AppComponent,
    BehaviorsSearchComponent,
    EnableAddInComponent
  ],
  bootstrap: [AppComponent]
})
export default class AppModule { }

会议服务.ts

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

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

行为-search.component.ts

import { Component } from '@angular/core';
import { MeetingsService } from '../meetings.service';
...

@Component({
  templateUrl: 'src/taskpane/app/behaviors-search/behaviors-search.component.html',
  styleUrls: ['src/taskpane/app/shared.css']
})

export class BehaviorsSearchComponent {

  constructor(private meetingsService: MeetingsService) {}

  ...
}

webpack.config.js

const devCerts = require("office-addin-dev-certs");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");
const fs = require("fs");
const webpack = require("webpack");

module.exports = async (env, options) => {
  const dev = options.mode === "development";
  const config = {
    devtool: "source-map",
    entry: {
      polyfill: "@babel/polyfill",
      taskpane: "./src/taskpane/taskpane.ts",
      commands: "./src/commands/commands.ts"
    },
    resolve: {
      extensions: [".ts", ".tsx", ".html", ".js"]
    },
    module: {
      rules: [
        {
          test: /\.ts$/,
          exclude: /node_modules/,
          use: "babel-loader"
        },
        {
          test: /\.tsx?$/,
          exclude: /node_modules/,
          use: "ts-loader"
        },
        {
          test: /\.html$/,
          exclude: /node_modules/,
          use: "html-loader"
        },
        {
          test: /\.(png|jpg|jpeg|gif)$/,
          use: "file-loader"
        }
      ]
    },
    plugins: [
      new CleanWebpackPlugin(),
      new HtmlWebpackPlugin({
        filename: "taskpane.html",
        template: "./src/taskpane/taskpane.html",
        chunks: ["polyfill", "taskpane"]
      }),
      new CopyWebpackPlugin([
        {
          to: "taskpane.css",
          from: "./src/taskpane/taskpane.css"
        }
      ]),
      new HtmlWebpackPlugin({
        filename: "commands.html",
        template: "./src/commands/commands.html",
        chunks: ["polyfill", "commands"]
      })
    ],
    devServer: {
      headers: {
        "Access-Control-Allow-Origin": "*"
      },      
      https: (options.https !== undefined) ? options.https : await devCerts.getHttpsServerOptions(),
      port: process.env.npm_package_config_dev_server_port || 3000
    }
  };

  return config;
};

4

1 回答 1

1

注入 asconstructor (@Inject(MeetingsService) meetingsService: MeetingsService)似乎是唯一的方法,因为使用 Yeoman ( yo office) 生成的项目使用 webpack 而不是 Angular CLI

https://github.com/OfficeDev/Office-Addin-TaskPane-Angular/issues/45#issuecomment-586167019

于 2020-03-27T10:05:29.210 回答