Angular 2包括ng2-ace包

时间:2016-10-08 17:00:15

标签: angular npm ace-editor


我一直试图让npm包ng2-ace工作,因为在div上使用ace-editor标签,但我总是错误'找不到模块ng2-ace'。


所以这是我的app.component.ts

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

import { AceEditorDirective } from 'ng2-ace';

@Component({
  selector: 'my-app',
  directives: [AceEditorDirective],
  templateUrl: 'app/app.component.html'
})

export class AppComponent { }


这是我的app.component.ts

/**
 * System configuration for Angular samples
 * Adjust as necessary for your application needs.
 */
(function (global) {
  System.config({
    paths: {
      // paths serve as alias
      'npm:': 'node_modules/',
    },
    // map tells the System loader where to look for things
    map: {
      // our app is within the app folder
      app: 'app',
      // angular bundles
      '@angular/core': 'npm:@angular/core/bundles/core.umd.js',
      '@angular/common': 'npm:@angular/common/bundles/common.umd.js',
      '@angular/compiler': 'npm:@angular/compiler/bundles/compiler.umd.js',
      '@angular/platform-browser': 'npm:@angular/platform-browser/bundles/platform-browser.umd.js',
      '@angular/platform-browser-dynamic': 'npm:@angular/platform-browser-dynamic/bundles/platform-browser-dynamic.umd.js',
      '@angular/http': 'npm:@angular/http/bundles/http.umd.js',
      '@angular/router': 'npm:@angular/router/bundles/router.umd.js',
      '@angular/forms': 'npm:@angular/forms/bundles/forms.umd.js',
      // other libraries
      'rxjs':                      'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',

      'brace': 'npm:brace',
      'w3c-blob': 'npm:w3c-blob',
      'buffer': 'npm:buffer-shims',
      'ng2-ace': 'npm:ng2-ace'
    },
    // packages tells the System loader how to load when no filename and/or no extension
    packages: {
      app: {
        main: './main.js',
        defaultExtension: 'js'
      },
      rxjs: {
        defaultExtension: 'js'
      },
      'angular-in-memory-web-api': {
        main: './index.js',
        defaultExtension: 'js'
      },

      'ng2-ace': {
        format: 'cjs',
        defaultExtension: 'js',
        main: './index.js'
      },

      'w3c-blob': {
        format: 'cjs',
        defaultExtension: 'js',
        main: './index.js'
      },

      'brace': {
        format: 'cjs',
        defaultExtension: 'js',
        main: './index.js'
      },

      'buffer': {
        format: 'cjs',
        defaultExtension: 'js',
        main: './index.js'
      },
    }
  });
})(this);

大括号是ng2-ace的依赖,它具有依赖性w3c-blob,它也具有依赖性缓冲区。
我刚开始使用Angular 2,所以我不太熟悉工作流程。

无论如何,提前谢谢!

2 个答案:

答案 0 :(得分:2)

您可以尝试以下步骤配置ace编辑器。

第1步

安装包

npm i brace w3c-blob buffer base64-js ieee754 --save

第2步

创建指令:

import { Directive, ElementRef, EventEmitter, Input, Output } from '@angular/core';

import 'brace';
import 'brace/theme/monokai';
import 'brace/mode/javascript';
declare var ace: any;

@Directive({
  selector: '[ace-editor]'
})
export class AceEditorDirective {
  _readOnly: any;
  _theme: any;
  _mode: any;

  editor: any;
  oldVal: any;

  @Input() set options(value) {
    this.editor.setOptions(value || {});
  }

  @Input() set readOnly(value) {
    this._readOnly = value;
    this.editor.setReadOnly(value);
  }

  @Input() set theme(value) {
    this._theme = value;
    this.editor.setTheme(`ace/theme/${value}`);
  }

  @Input() set mode(value) {
    this._mode = value;
    this.editor.getSession().setMode(`ace/mode/${value}`);
  }

  @Input() set text(value) {
    if(value === this.oldVal) return;
    this.editor.setValue(value);
    this.editor.clearSelection();
    this.editor.focus();
  }

  @Output() textChanged = new EventEmitter();
  @Output() editorRef = new EventEmitter();

  constructor(private elementRef: ElementRef) {
    const el = elementRef.nativeElement;
    el.classList.add('editor');

    this.editor = ace.edit(el);

    setTimeout(() => {
      this.editorRef.next(this.editor);
    });

    this.editor.on('change', () => {
      const newVal = this.editor.getValue();
      if(newVal === this.oldVal) return;
      if(typeof this.oldVal !== 'undefined') {
        this.textChanged.next(newVal);
      }
      this.oldVal = newVal;
    });
  }
}

第3步

配置systemjs.config.js

  map: {
    ...

    'brace': 'npm:brace@0.8.0',
    'w3c-blob': 'npm:w3c-blob/index.js',
    'buffer': 'npm:buffer/index.js',
    'base64-js': 'npm:base64-js/index.js',
    'ieee754': 'npm:ieee754/index.js'   
  },
  packages: {
    ...
    brace: {
      main: './index.js',
      defaultExtension: 'js'
    }
  }

第4步

在模块的declarations列表中包含AceEditorDirective

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

<强> Enjoy

答案 1 :(得分:1)

因为 .fourth-block { width: 100%; height: 300px; } #myCarousel { height: 300px; } fifth-block { background: #F2EADC; padding-top: 30px; height: 200px; background-image: url("../images/pic3.jpg"); background-repeat: repeat; } .proof-text { font-family: SummerFontLight; font-size: 20px; text-align: center; } 错过了Typescript定义。因此Angular编译器抛出一个错误:ng2-ace。我认为您可以从'Cannot find module ng2-ace'创建自定义指令,以便在项目中使用。

希望这有帮助!