gpt4 book ai didi

angular - 无法将数据映射库导入 Angular 2 应用程序的 HTML

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

我正在构建一个用于学习目的的小型 Angular 2 应用程序,我计划使用 datamaps用于 map 界面。

但是这个库还没有指令,所以我想自己热线。

我已经安装了数据 map

npm install datamaps

我正在使用 angular-cli 的 ng serve,但我无法让它工作,我尝试直接在 HTML 中导入它(只是为了看看它是否工作)但它找不到库。

我是通过直接 HTML 导入得到的,即使文件位于正确的位置,它也不会发送到浏览器

datamaps.world.hires.min.js:1 Uncaught SyntaxError: Unexpected token <

我将它添加到我的 package.json 中并尝试在我的组件中使用它,但也找不到它。

在我的 package.json 中就像这样

"datamaps": "^0.5.8"

如果我这样使用它,我的 html 应该怎么做才能看到它:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>AskTheWorld</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico">
<script src="//cdnjs.cloudflare.com/ajax/libs/d3/3.5.3/d3.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/topojson/1.6.9/topojson.min.js"></script>
<script src="../node_modules/datamaps/dist/datamaps.world.hires.min.js"></script>
</head>
<body>
<app-geochart-component>Loading...</app-geochart-component>
<div id="container" style="position: relative; width: 500px; height: 500px;"></div>
<script>
var map = new Datamap({element: document.getElementById('container')});
</script>
</body>
</html>

另外,将它导入我的组件的最佳方式是什么?我已经看到数据映射没有声明类型或我能找到的模块(我使用的是 TS 2)

到目前为止,这是我的组件

从'@angular/core'导入{Component, OnInit, ViewChild}; 从 '../../../node_modules/datamaps/dist/???????????????' 导入 { Datamap };

@Component({
selector: 'app-geochart-component',
templateUrl: './geochart-component.component.html',
styleUrls: ['./geochart-component.component.css']
})
export class GeochartComponentComponent implements OnInit {

@ViewChild('container') canvas;

constructor() { }

ngOnInit() {
var map = new Datamap(this.canvas);
}

}

}

最佳答案

所以你知道包含脚本的方法了。现在要从 Angular 使用它,您有几个选择。

选项 1(快速而肮脏但有效):在组件顶部创建一个 const Datamap(假设库的 JavaScript 对象称为“Datamap”):

const Datamap;

然后在调用新数据映射时使用该对象:

ngOnInit() {
var map = new Datamap(this.canvas);
}

选项 2(更复杂但更好的方法。我没有对该组件执行此操作,但所有外部组件的想法都是相同的。)通过将 Datamap 组件注入(inject)到 Angular 组件中,在应用程序启动时引导它.

此处也记录了有关选项的更多信息:How to pass parameters rendered from backend to angular2 bootstrap method

关于angular - 无法将数据映射库导入 Angular 2 应用程序的 HTML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40309778/

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