2017-04-26 82 views
0

阵营应用只读分配错误我有以下微创官能阵营组分:类型错误:与在Safari

import React from 'react'; 
import { Button } from 'react-bootstrap'; 

var downloadBlobAsFile = (function closure() { 
    var a = document.createElement("a"); 
    a.style = "display: none"; 
    return function downloadBlobAsFile(blob, fileName) { 
    var objectURL = URL.createObjectURL(blob); 
    a.href = objectURL; 
    a.download = fileName; 
    document.body.appendChild(a); 
    a.click(); 
    setTimeout(function(){ 
     document.body.removeChild(a); 
     window.URL.revokeObjectURL(objectURL); 
    }, 100); 
    }; 
})(); 

class MyWidget extends React.Component { 

    constructor(props) { 
    super(props); 
    this.state = { 
     someData: "abcd1234..." 
    }; 
    this.handleDownloadClick = this.handleDownloadClick.bind(this); 
    } 

    handleDownloadClick() { 
    var d = new Date(); 
    var n = d.toISOString(); 
    var fn = "result-" + n + ".txt"; 
    if (this.state.someData) { 
     downloadBlobAsFile(new Blob([this.refs.dataContainer.innerHTML], {type: "plain/text"}), fn); 
    } 
    } 

    render() { 
    return (
     <div> 
     <Button value="download" onClick={this.handleDownloadClick}>Download</Button>; 
     <div className="hidden-container" ref="dataContainer">{this.state.someData}</div> 
     </div> 
    ) 
    } 
} 

export default MyWidget; 

的WebPack可以构建包含此组件束。

然而,在Safari 10.1,开发者控制台报告以下致命错误:

TypeError: Attempted to assign to readonly property 

,应用程序不会在Safari中渲染。

这与下面的代码块:

var downloadBlobAsFile = (function closure() { 
    var a = document.createElement("a"); 
    a.style = "display: none"; 
    return function downloadBlobAsFile(blob, fileName) { 
    var objectURL = URL.createObjectURL(blob); 
    a.href = objectURL; 
    a.download = fileName; 
    document.body.appendChild(a); 
    a.click(); 
    setTimeout(function(){ 
     document.body.removeChild(a); 
     window.URL.revokeObjectURL(objectURL); 
    }, 100); 
    }; 
})(); 

如果我注释掉的代码块,Safari浏览器将加载和渲染应用程序(虽然没有从这个函数的功能)。

我不需要为Chrome或Firefox客户端注释掉此块,而只需Safari。

这个错误的原因是什么?我可以采取哪些步骤来修改我的组件以允许在Safari客户端上使用此功能?

回答

1

的代码行中downloadBlobAsFile()功能改变的是:

a.style = "display: none"; 

要:

a.style.display = "none"; 

这解决了只读错误在Safari和其他客户端保留功能。

+0

非常感谢您跟进您自己的问题。这也解决了我的问题。 'domNode.style ['font-weight'] ='bold''与'domNode.style =“font-weight:bold”'相比。 – Trevor