简体   繁体   English

Angular 5 TypeScript-包括ES2015代码

[英]Angular 5 TypeScript- including ES2015 code

I am working on angualar 5 app where I have to include dmn-js library which doesn't have typings available. 我正在研究angualar 5应用程序,我必须包含dmn-js库,它没有可用的类型。 I followed the steps outlined in angular-cli wiki on how to go about including 3rd party libraries, specifically one outlined under heading - "If the library doesn't have typings available at @types/, you can still use it by manually adding typings for it:" 我按照angular-cli wiki中概述的步骤了解如何包括第三方库,特别是在标题下标题 - “如果库在@ types /中没有可用的类型,您仍然可以通过手动添加类型来使用它为了它:”

This is how my code now looks like after - 这就是我的代码现在的样子 -

src/typings.d.ts SRC / typings.d.ts

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

src/app/app.component.ts 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));
  }

}

Now when I compile the code, I get below error. 现在当我编译代码时,我得到以下错误。 I am not sure what needs to be done to resolve the issue since I followed all steps. 自从我遵循所有步骤以来,我不确定要解决这个问题需要做些什么。

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.   

Angular-cli wiki tells how to add, as you have followed it already,now you can access the third party lib, but here dmn-js requires plugins which can support( spread operators,and other internal transforms,etc.). Angular-cli wiki告诉你如何添加,就像你已经按照它一样,现在你可以访问第三方库,但是这里dmn-js需要可以支持的插件(扩展运算符和其他内部转换等)。 and dmn-js uses babel [if you observe that it is having .babelrc files in each folder of dmn* ]. 和dmn-js使用babel [如果你发现它在dmn *的每个文件夹中都有.babelrc文件]。

In order to support the dmn-js we need to configure webpack. 为了支持dmn-js,我们需要配置webpack。 After spending decent amount of time here is the result : 在这里花了相当多的时间后,结果是:

在此输入图像描述

In your.Component.ts 在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');
      }
    });
    });

In your.Component.html 在your.Component.html中

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

In Webpack.config.js (use ng eject , if not exists) Add a rule in module -> rules 在Webpack.config.js中(使用ng eject,如果不存在)在模块 - >规则中添加规则

 { 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"]} 

      }

In index.html add stylesheet links 在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 -> you have added already !! Typings.d.ts - >你已经添加!!

Install the depenencies: 安装附件:

npm i --save-dev babel-plugin-inferno babel-plugin-transform-object-rest-spread babel-plugin-transform-class-properties babel-plugin-transform-object-assign npm i --save-dev babel-plugin-inferno babel-plugin-transform-object-rest-spread babel-plugin-transform-class-properties babel-plugin-transform-object-assign

Hope this helps !!! 希望这可以帮助 !!!

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

Ref2: Error: Missing class properties transform Ref2: 错误:缺少类属性转换

Ref3: https://github.com/webpack/webpack/issues/2902 参考3: https//github.com/webpack/webpack/issues/2902

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM