在 React 组件中加载 Google 脚本
Loading Google script in React Component
我正在尝试访问 YouTube 数据 API v3,但我在弄清楚如何在 React 中加载脚本时遇到了一些问题。 this.init()
触发,但我得到 gapi = {}
。奇怪的是,当我在控制台中输入 gapi
时,它显示为适当的对象。
TestPage = React.createClass({
componentDidMount() {
$.getScript("https://apis.google.com/js/client.js", this.handleClientLoad);
},
handleClientLoad(data, textStatus) {
Utils.logObj(data, "data");
Utils.logObj(textStatus, "textStatus");
Utils.logObj(gapi, "gapi");
Utils.logObj(window.gapi, "window.gapi");
},
render() {
return (
<div>This is the test page.</div>
);
}
});
你的代码工作正常检查控制台:http://jsfiddle.net/ferahl/ocpz7pbm/
var App = React.createClass({
componentDidMount() {
$.getScript("https://apis.google.com/js/client.js", this.handleClientLoad);
},
handleClientLoad() {
console.log(window.gapi);
},
render() {
return (
<div>This is the test page.</div>
);
}
});
也许您的 Utils.logObj
没有按预期工作?
我正在尝试访问 YouTube 数据 API v3,但我在弄清楚如何在 React 中加载脚本时遇到了一些问题。 this.init()
触发,但我得到 gapi = {}
。奇怪的是,当我在控制台中输入 gapi
时,它显示为适当的对象。
TestPage = React.createClass({
componentDidMount() {
$.getScript("https://apis.google.com/js/client.js", this.handleClientLoad);
},
handleClientLoad(data, textStatus) {
Utils.logObj(data, "data");
Utils.logObj(textStatus, "textStatus");
Utils.logObj(gapi, "gapi");
Utils.logObj(window.gapi, "window.gapi");
},
render() {
return (
<div>This is the test page.</div>
);
}
});
你的代码工作正常检查控制台:http://jsfiddle.net/ferahl/ocpz7pbm/
var App = React.createClass({
componentDidMount() {
$.getScript("https://apis.google.com/js/client.js", this.handleClientLoad);
},
handleClientLoad() {
console.log(window.gapi);
},
render() {
return (
<div>This is the test page.</div>
);
}
});
也许您的 Utils.logObj
没有按预期工作?