【发布时间】: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