小编Mic*_*ski的帖子

TypeScript 和 Angular 5 抽象类中的依赖注入

我得到了BaseComponent注入了一些依赖项的 。第一个依赖EntityService是正确且必要的。

AnyOtherService仅用于抽象内部BaseComponent。相反,将其注入里面的ChildComponent,它不使用,我想只有内部注入它BaseComonent

为什么我必须将它ChildComponent推向BaseComponent?最好的解决方案是将其封装在BaseComponent.

基础.component.ts

export abstract class BaseComponent {
  constructor(
    protected entityService: EntityService,
    // protected anyOtherService: AnyOtherService // @todo add this
  ) {
  }
}
Run Code Online (Sandbox Code Playgroud)

子组件.ts

@Component()
export class ChildComponent extends BaseComponent {

  constructor(
    private firstService: FirstService,
    private secondService: SecondService,
    protected anyOtherService: AnyOtherService // @todo remove this
  ) {
    super(
      firstService,
      anyOtherService // @todo remove this
    ); 
  }
}
Run Code Online (Sandbox Code Playgroud)

inheritance dependency-injection typescript angular

5
推荐指数
1
解决办法
3580
查看次数

角度6:无法使用JIT编译在动态组件中注入服务

我试图在要创建的动态组件中注入服务,但是当我尝试注入服务时出现错误。我可以在使用AOT的所有其他组件中注入服务,但在使用JIT时不能。这是代码。

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

@Injectable
export class ApplicantSvc
{
 name:string;  
}

private createComponentFromRaw(template: string){

  const tmpCmp = Component({ template })(class {

    constructor(private app :ApplicantSvc){}

  });

  // Now, also create a dynamic module.
  const tmpModule = NgModule({
    declarations: [tmpCmp],
    imports: [CommonModule],
    providers: [ApplicantSvc],

  })(class {});


  this.compiler.compileModuleAndAllComponentsAsync(tmpModule)
    .then((factories) => {
      const f = factories.componentFactories[0];
      this.cmpRef = f.create(this.injector, [], null, this.moduleRef);
      this.cmpRef.instance.name = 'my-dynamic-component';
      this.vc.insert(this.cmpRef.hostView);
    });
}
Run Code Online (Sandbox Code Playgroud)

在上面的代码中,我为动态模块中的提供程序添加了ApplicantSvc,然后将其注入动态组件构造函数中,但是每当尝试这样做时,都会出现错误

错误错误:无法解析class_1的所有参数:(?)。..... ..... at JitCompiler.push ../ node_modules/@angular/compiler/fesm5/compiler.js.JitCompiler._loadModules(compiler.js:22570)

javascript typescript angular

5
推荐指数
1
解决办法
499
查看次数

按正态分布排序数字数组(高斯分布)

有一个数字数组,setOfNumbers = [0, 3, 3, 2, 7, 1, -2, 9]我想对这个数组进行排序,使得最后和开始时的数字最小,并且排序集合中心的最大数字就像这样sortedSetNumbers = [0, 2, 3, 9, 7, 3, 1, -2].

const setOfNumbers = [0, 3, 3, 2, 7, 1, -2, 9];
const result = [0, 2, 3, 9, 7, 3, 1, -2];

function sortNormal(a, b) {
  return true; // Please, change this line
}

const sortedSetNumbers = setOfNumbers.sort((a, b) => sortNormal(a, b));



if (sortedSetNumbers === result) {
  console.info('Succeeded Normal Distributed');
} else {
  console.warn('Failed Normal Distribution');
} …
Run Code Online (Sandbox Code Playgroud)

javascript sorting normal-distribution gaussian

4
推荐指数
1
解决办法
206
查看次数

webpack 错误:找不到或无法读取要导入的文件: bourbon ,如何修复?

我正在尝试将 Bourbon 包加载到我的 SCSS 文件中。我正在使用 Angular 2,这是我的 webpack 3.0 配置:

var path = require("path");
var webpack = require("webpack");

module.exports = {
  devServer: {
    contentBase: path.join(__dirname, "build"),
    compress: true,
    port: 9000
  },
  node: {
    fs: 'empty'
  },
  cache: true,
  devtool: "eval", //or cheap-module-eval-source-map
  entry: {
    app: path.join(__dirname, "client/app", "app.js")
  },
  output: {
    path: path.join(__dirname, "buildf"),
    filename: "ha.js",
    chunkFilename: "[name].js"
  },
  plugins: [
    //Typically you'd have plenty of other plugins here as well
    new webpack.DllReferencePlugin({
      context: path.join(__dirname, "client"),
      manifest: require("./build/vendor-manifest.json")
    }),
  ], …
Run Code Online (Sandbox Code Playgroud)

javascript webpack-2 angular webpack-3

3
推荐指数
1
解决办法
5759
查看次数