【发布时间】:2017-10-05 16:11:35
【问题描述】:
在我的反应组件中,我试图触发对以下函数中创建的超链接的点击:
getUsers(e){
e.preventDefault();
const { userIds } = this.props;
BatchRoleActions.getAllRoleUsers(userIds)
.then((users) => {
const link = document.createElement('a');
link.setAttribute('id', 'csvLink');
link.setAttribute('download', 'roles.csv');
link.setAttribute('href', users);
document.getElementById('csvLink').onClick();
});
}
函数运行时出现以下错误: Uncaught (in promise) TypeError: Cannot read property 'onClick' of null
如何触发点击开始下载?
BatchRoleActions.getAllRoleUsers(userIds) 返回一个数组,其中包含要导出到 csv 文件的数据。编码URI(data:text/csv;charset=utf-8,\uFEFF${data});
我正在尝试寻找解决方案:ReactJs - How to complete onClick before download - href
【问题讨论】:
-
您将
OnClick拼写错误,大写字母 O,但我们需要更多信息来帮助您 -
你还没有将它插入到 DOM 中。无论如何,不要将原生 DOM 方法与 React 混合使用......
-
最好的方法是什么?
标签: javascript reactjs