在Angular 2中更改(ng2-)codemirror的模式

时间:2016-10-04 07:32:37

标签: angular codemirror

我设法让ng2-codemirror编辑器在我的Angular 2应用程序中完美运行,但是有一个小细节让我感到困惑 - 我无法更改其mode因为它需要导入我无法完成的.js文件。我需要从路径script导入node_modules\codemirror\mode\clike\clike.js,但似乎我不知道该怎么做。我尝试在index.html中导入它:

<script src="node_modules/codemirror/mode/clike/clike.js"></script>

但是我收到以下错误:

  

未捕获的ReferenceError:未定义CodeMirror

我会简化我的代码,以便轻松理解问题:

system.config.js

(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',
      '@angular/upgrade': 'npm:@angular/upgrade/bundles/upgrade.umd.js',

      // other libraries
      'rxjs': 'npm:rxjs',
      'angular-in-memory-web-api': 'npm:angular-in-memory-web-api',
      'ng2-codemirror': 'npm:ng2-codemirror',
      'codemirror': 'npm:codemirror'
    },
    // 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-codemirror': { main: 'lib/Codemirror.js', defaultExtension: 'js' },
      'codemirror': { main: 'lib/codemirror.js', defaultExtension: 'js' }
    }
  });
})(this);

模块

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

import { myComponent } from './myComponent.cmp';

import { SharedModule } from '../shared/shared.module';

import { ROUTING } from './browseClasses.routing';

import { myService } from './myService .service';

import { CodemirrorModule } from 'ng2-codemirror';

@NgModule({
    imports: [
        SharedModule,
        ROUTING,
        CodemirrorModule
    ],
    exports: [],
    declarations: [
        myComponent
    ],
    providers: [
        myService
    ]
})

export class BrowseClassesModule { }

组件

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

@Component({
    selector: 'my-component',
    templateUrl: 'app/my_component/myComponent.html'
})
export class MyComponent implements OnInit {

    constructor() { }

    codeToDisplay: string;
    codeConfig = {
    lineNumbers: true,
    readOnly: true,
    mode: "text/x-java" <--- I can't make this work
}

    ngOnInit() {
        this.codeToDisplay = this.getCode();
    }

    getCode() {
        //...
    }
}

模板

<div *ngIf="codeToDisplay">
    <codemirror [(ngModel)]="codeToDisplay" [config]="codeConfig"></codemirror>
</div>

的index.html

<!DOCTYPE html>
<html>

<head>
  <title>ClassLoadingS1</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" href="favicon.ico" type="image/x-icon" />

  <!-- Polyfill(s) for older browsers -->
  <script src="node_modules/core-js/client/shim.min.js"></script>
  <script src="node_modules/zone.js/dist/zone.js"></script>
  <script src="node_modules/reflect-metadata/Reflect.js"></script>
  <script src="node_modules/systemjs/dist/system.src.js"></script>
  <script src="node_modules/codemirror/lib/clike.js"></script>

  <link rel="stylesheet" type="text/css" href="app/css/styles.css" />
  <link rel="stylesheet" type="text/css" href="node_modules/primeui/themes/omega/theme.css" />
  <link rel="stylesheet" type="text/css" href="node_modules/primeui/primeui-ng-all.min.css" />
  <link rel="stylesheet" type="text/css" href="node_modules/bootstrap/dist/css/bootstrap.css" />
  <link rel="stylesheet" type="text/css" href="node_modules/codemirror/lib/codemirror.css" />

  <script src="systemjs.config.js"></script>
  <script>
  System.import('app').catch(function(err){ console.error(err); });
    </script>
</head>

<body>
  <my-app>
    <div class="center-div">
      <img src="app/css/loading.gif">
    </div>
  </my-app>
</body>

</html>

2 个答案:

答案 0 :(得分:10)

好的,我只是设法让它工作,你需要做以下事情:

  1. <script src="node_modules/codemirror/lib/codemirror.js"></script>添加到index.html(非常感谢@PierreDuc)
  2. 导入要在您的组件中使用的mode,在我的情况下是clike模式:

    import 'codemirror/mode/clike/clike';

答案 1 :(得分:2)

如果使用angular-cli,您只需要将文件添加到angluar-cli.json并在组件中执行导入。无需将脚本添加到index.html