简体   繁体   中英

How to import this javascript library?

I downloaded easystar.js with npm.

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. What am I doing wrong please?

You should try this way to import it in your js.

If you are using 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. Pure TypeScript code won't work in browsers, it has to be compiled to pure JavaScript prior to that. If you open Dev Tools, you'll probably see some SyntaxError s.

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.

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. something = something .

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

import easystarjs from 'easystarjs'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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