Angular 5 TypeScript-包括ES2015代码

时间:2018-04-25 15:53:54

标签: javascript angular typescript angular-cli

我正在研究angualar 5应用程序,我必须包含dmn-js库,它没有可用的类型。我按照angular-cli wiki中概述的步骤来了解如何包含第三方库,特别是在标题下列出的一个 - “如果库在@ types /中没有可用的类型,您仍然可以使用它通过手动为其添加输入:“

这是我的代码现在的样子 -

src / typings.d.ts

/* SystemJS module definition */
declare var module: NodeModule;
declare module 'dmn-js';
interface NodeModule {
  id: string;
}

的src /应用程序/ app.component.ts

import { Component, OnInit } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import * as DmnJS from 'dmn-js';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'DMN';

  constructor(private http: HttpClient){    
  }

  ngOnInit(): void {
    var viewer = new DmnJS ({ container: 'body' });
    var dmnXML; //DMN 1.1 xml
    viewer.importXML(dmnXML, this.handleError);
  }

   handleError(err: any) {
    if (err) {
      console.warn('Ups, error: ', err);
    }else {
      console.log('rendered');
    }
  }

  load(): void {
    const url = '/assets/dish-decision.dmn';
    this.http.get(url, {
      headers: {observe: 'response'}, responseType: 'text'
    }).subscribe(
      (x: any) => {
        console.log('Fetched XML, now importing: ', x);
        //this.modeler.importXML(x, this.handleError);
      },
      this.handleError
    );
  }

  save(): void {
    //this.modeler.saveXML((err: any, xml: any) => console.log('Result of saving XML: ', err, xml));
  }

}

现在当我编译代码时,我得到以下错误。我不确定自从我遵循所有步骤后需要做些什么来解决这个问题。

ERROR in ./node_modules/dmn-js-drd/lib/Viewer.js                                                                                                                                            
Module parse failed: Unexpected token (175:4)                                                                                                                                               
You may need an appropriate loader to handle this file type.                                                                                                                                
|     additionalModules,                                                                                                                                                                    
|     canvas,                                                                                                                                                                               
|     ...additionalOptions                                                                                                                                                                  
|   } = options;                                                                                                                                                                            
|                                                                                                                                                                                           
ERROR in ./node_modules/dmn-js-shared/lib/base/Manager.js                                                                                                                                   
Module parse failed: Unexpected token (292:16)                                                                                                                                              
You may need an appropriate loader to handle this file type.                                                                                                                                
|   }                                                                                                                                                                                       
|                                                                                                                                                                                           
|   _viewsChanged = () => {                                                                                                                                                                 
|     this._emit('views.changed', {                                                                                                                                                         
|       views: this._views,                                                                                                                                                                 
ERROR in ./node_modules/dmn-js-decision-table/lib/Viewer.js                                                                                                                                 
Module parse failed: Unexpected token (75:6)                                                                                                                                                
You may need an appropriate loader to handle this file type.                                                                                                                                
|       modules,                                                                                                                                                                            
|       additionalModules,                                                                                                                                                                  
|       ...config                                                                                                                                                                           
|     } = options;                                                                                                                                                                          
|                                                                                                                                                                                           
ERROR in ./node_modules/dmn-js-literal-expression/lib/Viewer.js                                                                                                                             
Module parse failed: Unexpected token (77:6)                                                                                                                                                
You may need an appropriate loader to handle this file type.                                                                                                                                
|       modules,                                                                                                                                                                            
|       additionalModules,                                                                                                                                                                  
|       ...config                                                                                                                                                                           
|     } = options;                                                                                                                                                                          
|                                                                                                                                                                                           

webpack: Failed to compile.   

1 个答案:

答案 0 :(得分:3)

  

Angular-cli wiki告诉你如何添加,就像你现在已经关注它一样   你可以访问第三方库,但这里dmn-js需要插件   这可以支持(传播运营商和其他内部   变换等)。和dmn-js使用babel [如果你发现它是   在dmn *]的每个文件夹中都有.babelrc文件。

为了支持dmn-js,我们需要配置webpack。 花了相当多的时间在这里结果是:

enter image description here

在your.Component.ts

constructor(private http: HttpClient ){
    this.http.get('../assets/val.xml',{responseType: 'text'}).subscribe(x=>{
     var xml= x; // my DMN 1.1 xml
     //var myContainer = document.getElementsByClassName('canvas');
    var viewer = new Viewer({
      container: '.canvas'
    });

    viewer.importXML(xml, function(err) {
     console.log('*********************');
      if (err) {
        console.log('error rendering', err);
      } else {
        viewer
        .getActiveViewer()
        .get('canvas')
          .zoom('fit-viewport');
      }
    });
    });

在your.Component.html

<body >
<div class="canvas" style="width:100vh;height:100vh ;padding-left:100px"></div>
  </body>
  

在Webpack.config.js中(使用ng弹出,如果不存在)    在模块中添加规则 - &gt;规则

 { test: /\.js$/, 
        exclude: /node_modules\/(?!(dmn-js|dmn-js-drd|dmn-js-shared|dmn-js-decision-table|table-js|dmn-js-literal-expression|diagram-js)\/).*/,
        loader: 'babel-loader',query: {presets: ["react","es2015","stage-0"]} 

      }

在index.html中添加样式表链接

  <link rel="stylesheet" href="https://unpkg.com/dmn-js@4.3.0/dist/assets/dmn-js-drd.css">
  <link rel="stylesheet" href="https://unpkg.com/dmn-js@4.3.0/dist/assets/dmn-js-decision-table.css">
  <link rel="stylesheet" href="https://unpkg.com/dmn-js@4.3.0/dist/assets/dmn-js-literal-expression.css">
  <link rel="stylesheet" href="https://unpkg.com/dmn-js@4.3.0/dist/assets/dmn-font/css/dmn.css">

Typings.d.ts - &gt;你已经添加!!

安装附加费:

  

npm i --save-dev babel-plugin-inferno   巴贝尔-插件变换对象,其余的扩频   巴别-插件变换级的属性   巴别-插件变换对象指派

希望这有帮助!!!

参考1:https://github.com/bpmn-io/dmn-js-examples/tree/master/bundling

参考2:Error: Missing class properties transform

REF3:https://github.com/webpack/webpack/issues/2902