[英]Access variables defined in <head> or external script from React component
[英]React external script method is not defined
我需要在我的React projet上導入BlotterJS ,因此我將它在index.html文件中設置為:
<script crossorigin src="https://rawgit.com/bradley/Blotter/master/build/blotter.min.js"></script>
我在componentWillMount
上使用它:在componentWillMount
我使用我的方法generateText
生成了Blotter文本,但是它需要創建一個新的Blotter
實例。 這是組件:
export class Home extends React.Component {
constructor() {
super()
}
componentWillMount() {
this.text = this.generateText()
}
render() {
return (
<div className="container">
<h1> Benjamin Moquet </h1>
<div id="welcome"></div>
<nav>
<div className={styles.double_buttons}>
<Link to="/about" className={`button ${styles.button_small}`}>About me</Link>
<Link to="/work" className="button button_small">Work</Link>
</div>
<Link to="/contact" className="button button_large">Contact</Link>
</nav>
</div>
)
}
generateText () {
return new Blotter.Text("WELCOME.", {
family : "'EB Garamond', serif",
size : 50,
fill : "#202020"
})
}
// Things here to generate the material and create the scope, but same issue, I can't do this because it needs Blotter too
}
嘗試使用window.Blotter
而不是Blotter
。
看來您的編譯器沒有找到變量定義並拋出錯誤,但是您的邏輯似乎還不錯,所以我想這會解決它。
如果使用的是webpack,則需要將Blotter配置為外部庫。 請參閱webpack文檔: https ://webpack.js.org/configuration/externals/
本質上,您可以將以下內容添加到webpack.config.js中
externals: {
Blotter: 'Blotter'
}
之后,您可以將其用作任何其他庫:
import Blotter from 'Blotter';
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.