如何将vue.js与gapi连接起来

时间:2019-02-13 16:44:07

标签: vue.js gapi

我尝试了太多事情,但无法解决此问题。我正在尝试将vue.js与gapi连接起来,以便在此之后使用Google Spreadsheet。 问题是我认为脚本中的api.js没有加载,我不知道原因。每次控制台说:“未定义gapi”时,我都尝试将setTimeout放入gapi.load中,什么也不做。 如果有人可以帮助我..

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.0/vue.min.js"></script>
<script src="https://apis.google.com/js/platform.js" async defer></script>
<script src="https://apis.google.com/js/platform.js"></script>
<script>
    import * as http from "http";
    import Vue from "vue";

    export default {
        data() {
            return {
                entries: []
            };
        },
        mounted() { 

             // Client ID and API key from the Developer Console
      var CLIENT_ID = 'AIzaSyBcN4pjE5yK9ZXphpfe7yJddrvYTxCUhYU';

      // Array of API discovery doc URLs for APIs used by the quickstart
      var DISCOVERY_DOCS = ["https://www.googleapis.com/discovery/v1/apis/sheets/v4/rest"];

      // Authorization scopes required by the API; multiple scopes can be
      // included, separated by spaces.
      var SCOPES = "https://www.googleapis.com/auth/spreadsheets.readonly";

        const gauthOption = {
        discoveryDocs: DISCOVERY_DOCS,
        clientId: CLIENT_ID,
        scope: SCOPES
        };

         gapi.load('client:auth2', gauthOption);

1 个答案:

答案 0 :(得分:0)

在执行代码之前,您可能需要等待gapi,然后才能在脚本标记中使用onload查询参数,如下所示:

<script src="https://apis.google.com/js/platform.js?onload=onLoadCallback"  async defer></script>
<script>
window.onLoadCallback = function(){
  gapi.load('client:auth2', gauthOption);
}