gpt4 book ai didi

javascript - Angular 5 TypeScript——包括 ES2015 代码

转载 作者:太空狗 更新时间:2023-10-29 18:23:43 29 4
gpt4 key购买 nike

我正在开发 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.

最佳答案

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.). and dmn-js uses babel [if you observe that it is having .babelrc files in each folder of dmn* ].

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

enter image description here

在你的.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>

In Webpack.config.js (use ng eject , if not exists) Add a rule in module -> rules

 { 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

引用 2:Error: Missing class properties transform

引用 3:https://github.com/webpack/webpack/issues/2902

关于javascript - Angular 5 TypeScript——包括 ES2015 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50026540/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com