【问题标题】:Angular 5 TypeScript- including ES2015 codeAngular 5 TypeScript - 包括 ES2015 代码
【发布时间】:2018-10-06 04:22:42
【问题描述】:

我正在开发 angualar 5 应用程序,我必须在其中包含没有可用类型的 dmn-js 库。我按照 angular-cli wiki 中概述的步骤介绍了如何包含第 3 方库,特别是标题下概述的步骤 - “如果库在 @types/ 上没有可用的类型,您仍然可以手动使用它为其添加类型:"

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

src/typings.d.ts

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

src/app/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.   

【问题讨论】:

  • 看起来你正在使用 webpack 但没有告诉 webpack 加载 .js 文件。
  • 问题在于加载 web pack。也许这个链接可以帮助stackoverflow.com/questions/33469929/…
  • 我使用 angular-cli 创建项目。我没有看到 angular-cli 创建的 webpack.config.js。 wiki 不会让 webpack 单独了解 .js 文件。我使用 npm install dmn-js 安装库。
  • 使用 angular.cli 时,webpack 设置将为您隐藏。所以你可以使用 ng 弹出(但在它之前阅读)或使用 angular-cli.json 部分脚本。在那里你可以包含 javascrpt 文件。
  • 您应该将此问题告知图书馆所有者。将 es6 代码作为库提供并不是最佳实践,因为大多数工具/设置都将 node_modules 排除在编译之外,而 angular cli 就是其中之一。

标签: javascript angular typescript angular-cli


【解决方案1】:

Angular-cli wiki 告诉您如何添加,因为您已经遵循它,现在 您可以访问第三方库,但这里 dmn-js 需要插件 可以支持(传播运算符,和其他内部 变换等)。并且 dmn-js 使用 babel [如果你观察到它是 在 dmn* ] 的每个文件夹中都有 .babelrc 文件。

为了支持 dmn-js,我们需要配置 webpack。 在这里花费大量时间后,结果如下:

在 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');
      }
    });
    });

在你的.Component.html

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

在 Webpack.config.js 中(如果不存在,请使用 ngject) 在模块中添加规则 -> 规则

 { 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 -> 你已经添加了!!

安装依赖:

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

希望对你有帮助!!!

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

Ref2:Error: Missing class properties transform

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

【讨论】:

  • 供您参考创建 git repo:github.com/HariDongli/dmn-js-angular-stack !!!
  • 这太棒了! -) 非常感谢您花时间解决。它现在正在工作。我必须下载其他 npm 依赖项才能使其工作 - npm install --save-dev babel-loader babel-preset-stage-2 babel-preset-es2015
  • 谢谢你!!!,碰到了babel,还没用过!!!您可以将此添加到 dnm-js forum.bpmn.io/t/using-dmn-js-with-angular-5/2057/6 的论坛 :)
  • 我实际上只是在您的解决方案中发布了 ref :-)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多