javascript - 在 ReactJS 中导出 Object.prototype.functions
问题描述
如何将此方法导出到另一个 React 组件?
// utils.js
Array.prototype.last_element = function() {
const last_index = this.length-1;
return this[last_index];
};
export default ... ?
// main.jsx
import ... ? from './utils.js';
const arr = [1 , 2 , 3];
console.log(arr.last_element());
解决方案
你不需要导出。只需在 main.jsx 中导入文件即可。
// main.jsx
import "./utils.js";
但是,由于您使用的是 React,因此您只需在 App.tsx 或 index.tsx 中导入 utils.js 一次,因为您的主要目的是定义 Array 原型。
推荐阅读
- mysql - MySql 无法从局域网连接
- python - 返回 lambda 的函数的类型注释
- c++ - 输入 C++ 中的错误
- html - 将 div 放在另一个具有响应式设计的 div 下
- java - 如何将功能添加到在另一个按钮内创建的按钮?
- c++ - 比率乘法期间的 chrono::duration_cast 问题
- c - 为什么我在 C 代码中得到相同的结果(与第一个结果相同)?
- python-3.x - kafka-python:在 kafka 生产者中指定分区时出错
- prolog - 有什么作用!序言中的符号是什么意思?
- r - 创建集群后如何在 R 中执行 Wilcoxon-Mann-Whitney 检验?