如何解决在Vue应用程序中何处包含API的javascript客户端,以便您可以在主要App组件的函数中调用其方法?
我想在我的Vue应用程序中使用API客户端(例如Google的javascript客户端)。 我显然可以将它放在index.html的脚本标签中,并在另一个脚本标签的函数中使用它的方法,但这不允许我在组件中发出API请求:
INDEX.HTML
...
<script src="https://apis.google.com/js/api.js"></script>
<script>
...
function searchSomething() {
return gapi.client.youtube.channels.list({
"forUsername": "pewdiepie"
})}
</script>
我的目的是在我的主要App组件的方法的函数中使用这些gapi.method,以便通过事件发射表单简化API请求。
<template>
<div id="app">
<api-search-form @sent-input="searchGAPI"></api-search-form>
</div>
</template>
//I tried including the client via an extra script tag in the component,//but vue single file components can only have one script tag,so this didn't work:
<script src="https://apis.google.com/js/api.js"></script>
<script>
import ApiSearchForm from './components/ApiSearchForm.vue';
export default {
name: "App",components: {
ApiSearchForm
},methods: {
searchGAPI(queryObj) {
gapi.client.youtube.channels.list({
"forUsername": queryObj
}).then(function(response) {
console.log("Response",response);
},function(err) { console.error("Execute error",err); });
}
},mounted() {
//Some people said to include in in a mounted lifecycle function so the client loads
//before your page,but this still gives eslint errors where gapi is undefined in the above
//method functions
}
};
</script>
我已经看到它建议使用安装的生命周期挂钩函数在您的组件中创建脚本元素,但这仍然不能让我用gapi客户端进行api调用,因为在构建时仍未在components方法中对其进行定义
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。