在Angular组件中包含JavaScript库。

huangapple go评论59阅读模式
英文:

Include js library in angular component

问题

我创建了一个新的 anglure cli 项目。我想使用 chessboardjs2 库。
它的文档:https://chessboardjs.com/v2/examples
以及它的 GitHub 地址:https://github.com/oakmac/chessboard2

我成功安装了 npm 包:

npm install @chrisoakman/chessboardjs2

这个包的内容如下:

在Angular组件中包含JavaScript库。

但是我无法找到如何在组件中引入它。

我可以直接在 index.html 中引入该库并使用它:

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Chess board</title>
  <base href="/">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" type="image/x-icon" href="favicon.ico">
  <link rel="preconnect" href="https://fonts.gstatic.com">
  <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&amp;display=swap" rel="stylesheet">
  <link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
  <!-- <link rel="stylesheet" href="https://unpkg.com/@chrisoakman/chessboard2@0.3.0/dist/chessboard2.min.css" integrity="sha384-5cxVYodq78gDJaWQIc5iBCUhFERY+VjHOszl2K7BTbZwBbrzQH2IYhOliWHJy6X3" crossorigin="anonymous"> -->
  <script src="https://unpkg.com/@chrisoakman/chessboard2@0.3.0/dist/chessboard2.min.js" integrity="sha384-v+CI0A3P1tu1MDM6cJaBosdhRHCfZlJrhHUFWBGtckCzH/ChKw9EhDHEWGmPkp8t" crossorigin="anonymous"></script>
</head>
<body class="mat-typography">
  <app-root></app-root>
  <div id="myBoard" style="width: 200px;"></div>
  <script>
    const board = Chessboard2('myBoard')
  </script>
</body>
</html>

但是这种实现方式不允许我在组件中使用它。

我该怎么办?

这是我的第一个 Angular 项目,我可能不是很了解所有内容...
我还尝试将 chessboard2.min.js 和 chessboard2.css 插入到 angular.json 中,但没有成功。

感谢你的帮助。

编辑:我正在寻找在组件中使用 chessboard2.min.js 的 JavaScript 函数的解决方案,而不是在 index.html 的 body 部分使用。

英文:

I created a new anglure cli project. I want to use the chessboardjs2 library.
It's documentation: https://chessboardjs.com/v2/examples
and is github: https://github.com/oakmac/chessboard2

I was able to get the npm package:

npm install @chrisoakman/chessboardjs2

Screen of the content of this package:

在Angular组件中包含JavaScript库。

but then I was unable to find out how to import it in a component.

I was able to import the library directly in index.html and use it:

&lt;!doctype html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
  &lt;meta charset=&quot;utf-8&quot;&gt;
  &lt;title&gt;Chess board&lt;/title&gt;
  &lt;base href=&quot;/&quot;&gt;
  &lt;meta name=&quot;viewport&quot; content=&quot;width=device-width, initial-scale=1&quot;&gt;
  &lt;link rel=&quot;icon&quot; type=&quot;image/x-icon&quot; href=&quot;favicon.ico&quot;&gt;
  &lt;link rel=&quot;preconnect&quot; href=&quot;https://fonts.gstatic.com&quot;&gt;
  &lt;link href=&quot;https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;500&amp;display=swap&quot; rel=&quot;stylesheet&quot;&gt;
  &lt;link href=&quot;https://fonts.googleapis.com/icon?family=Material+Icons&quot; rel=&quot;stylesheet&quot;&gt;
  &lt;!-- &lt;link rel=&quot;stylesheet&quot; href=&quot;https://unpkg.com/@chrisoakman/chessboard2@0.3.0/dist/chessboard2.min.css&quot; integrity=&quot;sha384-5cxVYodq78gDJaWQIc5iBCUhFERY+VjHOszl2K7BTbZwBbrzQH2IYhOliWHJy6X3&quot; crossorigin=&quot;anonymous&quot;&gt;
  &lt;script src=&quot;https://unpkg.com/@chrisoakman/chessboard2@0.3.0/dist/chessboard2.min.js&quot; integrity=&quot;sha384-v+CI0A3P1tu1MDM6cJaBosdhRHCfZlJrhHUFWBGtckCzH/ChKw9EhDHEWGmPkp8t&quot; crossorigin=&quot;anonymous&quot;&gt;&lt;/script&gt; --&gt;
&lt;/head&gt;
&lt;body class=&quot;mat-typography&quot;&gt;
  &lt;app-root&gt;&lt;/app-root&gt;
  &lt;div id=&quot;myBoard&quot; style=&quot;width: 200px;&quot;&gt;&lt;/div&gt;
  &lt;script&gt;
    const board = Chessboard2(&#39;myBoard&#39;)
  &lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

But this implementation does not allow me to use it in a component.

What can I do?

This is my first angular project and I might not understand everything...
I also tryied to insert the chessboard2.min.js and the chessboard2.css in the angular.json without success.

Thanks for your help

EDIT: I am looking for a solution to use the js function of chessboard2.min.js inside a component. Not in the index.html body.

答案1

得分: 1

{
  // angular.json
  "styles": [
    "src/styles.scss",
    "node_modules/@chrisoakman/chessboard2/dist/chessboard2.min.css"
  ],
  "scripts": [
    "node_modules/@chrisoakman/chessboard2/dist/chessboard2.min.js"
  ]
}

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.scss']
})
export class AppComponent implements OnInit {
  title = 'chessboard';

  public ngOnInit(): void {
    const board = (window as any).Chessboard2('board', {
      draggable: true,
      dropOffBoard: 'trash',
      sparePieces: true
    });

    board.start();
  }
}
英文:

You can import each file into the "scripts" and "styles" keys of the angular.json file as follows:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

{
    // angular.json
    &quot;styles&quot;: [
      &quot;src/styles.scss&quot;,
      &quot;node_modules/@chrisoakman/chessboard2/dist/chessboard2.min.css&quot;
    ],
    &quot;scripts&quot;: [
      &quot;node_modules/@chrisoakman/chessboard2/dist/chessboard2.min.js&quot;
    ]
}

<!-- end snippet -->

In your component you can use the package as follows:

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

import { Component, OnInit } from &#39;@angular/core&#39;;

@Component({
  selector: &#39;app-root&#39;,
  templateUrl: &#39;./app.component.html&#39;,
  styleUrls: [&#39;./app.component.scss&#39;]
})
export class AppComponent implements OnInit
{
  title = &#39;chessboard&#39;;

  public ngOnInit(): void
  {
    const board = (window as any).Chessboard2(&#39;board&#39;, {
      draggable: true,
      dropOffBoard: &#39;trash&#39;,
      sparePieces: true
    }); 

    board.start();
  }
}

<!-- end snippet -->

Altought I think it would be a better idea to use some specific package for angular like ngx-chess-board.

huangapple
  • 本文由 发表于 2023年3月7日 01:53:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/75654198.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定