我在react上做了一些关于设置脚本的研究,比如下面这个例子:
componentDidMount () {
const script = document.createElement("script");
script.src = "https://www.example.com/example.js";
script.async = true;
document.body.appendChild(script);
}
我需要在其中设置data-client-key
,但react似乎无法对其进行编译。有人能帮我吗?
发布于 2019-03-19 23:05:10
试试setAttribute
script.setAttribute('data-client-key', 'Your Key')?
您的componentDidMount将如下所示
componentDidMount() {
let s = document.createElement("script");
s.src = "https://google.com";
s.setAttribute("data-some-thing", 12);
document.body.appendChild(s);
}
https://stackoverflow.com/questions/55243964
复制相似问题