简体   繁体   English

如何导入这个 javascript 库?

[英]How to import this javascript library?

I downloaded easystar.js with npm.我用 npm 下载了easystar.js

Its code looks like this:它的代码如下所示:

export const TOP: 'TOP'
export const TOP_RIGHT: 'TOP_RIGHT'
export const RIGHT: 'RIGHT'
export const BOTTOM_RIGHT: 'BOTTOM_RIGHT'
export const BOTTOM: 'BOTTOM'
export const BOTTOM_LEFT: 'BOTTOM_LEFT'
export const LEFT: 'LEFT'
export const TOP_LEFT: 'TOP_LEFT'

type Direction = 'TOP' | 'TOP_RIGHT' | 'RIGHT' | 'BOTTOM_RIGHT' | 'BOTTOM' | 'BOTTOM_LEFT' | 'LEFT' | 'TOP_LEFT'

export class js {

  /**
   * Sets the collision grid that EasyStar uses.
   *
   * @param {Array|Number} tiles An array of numbers that represent
   * which tiles in your grid should be considered
   * acceptable, or "walkable".
   */
  setAcceptableTiles(tiles: number[] | number): void

.
.
.
  removeAllDirectionalConditions(): void
}

I've tried importing in all these ways:我试过以所有这些方式导入:

import * as easystar from 'easystarjs';
import 'easystarjs';
import {js} from 'easystarjs';

but I can't see anything called easystar or js in my window object if I put a breakpoint there.但是如果我在窗口对象中放置了一个断点,我就看不到任何名为 easystar 或 js 的东西。 What am I doing wrong please?请问我做错了什么?

You should try this way to import it in your js.您应该尝试这种方式将其导入到您的 js 中。

If you are using node js :如果您使用node js

var easystarjs = require('easystarjs');
var easystar = new easystarjs.js();

First of all, I'm 99.99% sure, this code is TypeScript, not JavaScript.首先,我 99.99% 肯定,这段代码是 TypeScript,而不是 JavaScript。 Pure TypeScript code won't work in browsers, it has to be compiled to pure JavaScript prior to that.纯 TypeScript 代码在浏览器中不起作用,必须在此之前将其编译为纯 JavaScript。 If you open Dev Tools, you'll probably see some SyntaxError s.如果您打开 Dev Tools,您可能会看到一些SyntaxError

Then, there's the name of the library and the file, – you are being pretty ambiguous on that.然后,还有库和文件的名称——你在这方面非常含糊。 Both easystar , easystar.js , as well as easystarjs could be three separate libraries, completely independent from each other. easystareasystar.js以及easystarjs都可以是三个独立的库,彼此完全独立。

Lastly (assuming all the above issues are fixed), importing an entity doesn't mean putting it at window object, – you still have to explicitly say window. something = something最后(假设上述所有问题都已解决),导入实体并不意味着将其放在window对象上, - 您仍然必须明确地说window. something = something window. something = something . window. something = something

如果你在 nodejs 中制作"type": "module"那么这是有效的:

import easystarjs from 'easystarjs'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM