Angular 2 个包括 ng2-ace 包
Angular 2 include ng2-ace package
我一直在尝试让 npm 包 ng2-ace 工作,以便在 div 上使用 ace-editor 标签,但我总是出现错误 'Cannot find module 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);
brace 是 ng2-ace 的依赖项,它具有依赖项 w3c-blob,后者也具有依赖项缓冲区。
我刚开始使用 Angular 2,所以我不太习惯工作流程。
无论如何提前致谢!
因为ng2-ace
缺少 Typescript 定义。所以 Angular 编译器抛出一个错误:'Cannot find module ng2-ace'
。我认为您可以从 ng2-ace
创建自定义指令以在您的项目中使用。
希望对您有所帮助!
您可以尝试以下步骤来配置ace-editor。
步骤 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 { }
我一直在尝试让 npm 包 ng2-ace 工作,以便在 div 上使用 ace-editor 标签,但我总是出现错误 'Cannot find module 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);
brace 是 ng2-ace 的依赖项,它具有依赖项 w3c-blob,后者也具有依赖项缓冲区。
我刚开始使用 Angular 2,所以我不太习惯工作流程。
无论如何提前致谢!
因为ng2-ace
缺少 Typescript 定义。所以 Angular 编译器抛出一个错误:'Cannot find module ng2-ace'
。我认为您可以从 ng2-ace
创建自定义指令以在您的项目中使用。
希望对您有所帮助!
您可以尝试以下步骤来配置ace-editor。
步骤 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 { }