javascript - 如何在 b.js 的按钮中使用 a.js 中的函数?
问题描述
当用户点击按钮时,我正在尝试打开上传文档对话框。我从另一个文件(“upload-recording-button.js”)导入函数来执行此操作,并尝试在按钮中使用它。但是,我遇到了一些错误,例如“_uploadRecordingButton.uploadRecording 未定义”。</p>
基于其他相关的 Stack Overflow 页面,我尝试将按钮的 OnPress 更改为 this.uploadRecording、uploadRecording(this)、this.uploadRecording(),可能还有其他一些变体。它们都因“功能不存在”的变体而失败。</p>
这是我正在调用的函数的文件的一部分:
上传-录音-button.js
import React, {Component} from 'react';
import { DocumentPicker } from 'expo';
async function uploadRecording() {
let result = await DocumentPicker.getDocumentAsync({type: '*/*'});
if (!result.cancelled) {
//upload function(result.uri);
}
}
export default uploadRecording;
主文件
import { uploadRecording } from '../components/upload-recording-button';
export default class HomeScreen extends React.Component {
static navigationOptions = {
//header: null,
title: 'Recents',
// uploadRecording(this)
headerRight: (
<Button
onPress={uploadRecording()}
title="Add"
color="#007AFF"
/>
),
};
解决方案
您需要从中导出功能upload-recording-button.js
import React, {Component} from 'react';
import { DocumentPicker } from 'expo';
export async function uploadRecording() {
let result = await DocumentPicker.getDocumentAsync({type: '*/*'});
if (!result.cancelled) {
//upload function(result.uri);
}
}
如果您只导出一个函数,upload-recording-button.js
则可以使用默认导出
export default function-name
并将其导入您要使用的文件中
import anyName from file-fath
推荐阅读
- c# - Xmlserializer 到 C# 对象,存储原始 XML 元素
- laravel - Laravel 雄辩删除
- amazon-web-services - 设置向导菜单时无法连接詹金斯
- python - Tensorflow分配内存:分配38535168超过系统内存的10%
- amazon-dynamodb - 如何通过控制台在 DynamoDB 中添加项目?
- r - 在 R 中将日期时间标记为早上、下午和晚上
- c# - AppService 返回具有空 Id 的 DTO
- javascript - 连字符类在 javascript 中不起作用
- javascript - 元素宽度以匹配其他元素的水平位置
- scala - 在 SparkTypes 上使用类型类