reactjs - 将对象及其方法传递给另一个函数
问题描述
我有一个导入到我的应用程序中的 npm 库,见下文:
import Chess from 'chess.js';
let chess = new Chess();
以下链接中显示了一种使用方法chess.moves()
:
https://github.com/jhlywa/chess.js/blob/master/README.md
上面两行代码在我的文件 App.js 中。我有一个实用程序.js 文件,我想在其中放置一个函数,该函数是从 App.js 中的函数调用的。但是,我希望能够chess.moves()
在utilities.js 中的函数中使用。
如何使用chess.moves()
utilities.js 的内部?
解决方案
在您的导入函数中传递国际象棋对象对您有用吗?
在你的 utilities.js
let myNewFunction = (chess)=>{
chess.moves();
}
export myNewFunction;
在你的 app.js
import Chess from 'chess.js';
import {myNewFunction } from './utilities';
let chess = new Chess();
....
myNewFunction(chess);
....
推荐阅读
- hazelcast - Hazelcast Scheduled Executor 总是在最新成员上启动任务
- kubernetes - 如何设置 istio sidecar 生命周期
- amazon-web-services - 每个 Lambda 别名的 Cloudwatch 警报
- django - 我如何在 django 模板中显示另一个网站数据
- node.js - 如何在 mongoDB 中插入对象数组?
- sql - SQL - 从不同的数据库中动态选择信息到变量中
- css - 如何在blogspot的中心对齐列
- node.js - 如何监控 NPM 包许可类型的变化?
- android - Android API 21 中的 Webview 为空白/白色
- python - 网页抓取文本返回 0