首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Vue / TypeScript中使用Google TypeScript SDK?

如何在Vue / TypeScript中使用Google TypeScript SDK?
EN

Stack Overflow用户
提问于 2022-05-14 08:46:48
回答 1查看 1.3K关注 0票数 5

我试图在我的Vue / Quasar / TypeScript应用程序中使用新的Google服务TypeScript SDK来授权一些Google。

作为按照医生的说法,我已经将Google3P授权JavaScript库加载到我的index.template.HTML文件的头中,如下所示:

代码语言:javascript
复制
<script src="https://accounts.google.com/gsi/client" onload="console.log('TODO: add onload function')">  
</script>

现在,在Vue组件中,我有以下内容:

代码语言:javascript
复制
<template>
  <v-btn
    class="q-ma-md"
    design="alpha"
    label="Connect Google Calendar"
    @click="handleGoogleCalendarConnect"
  />
</template>

<script setup lang="ts">
import VBtn from 'src/components/VBtn.vue';

const client = google.accounts.oauth2.initCodeClient({ // <-- TypeScript Error Here
  client_id:
    'MY_CLIENT_API_KEY_GOES_HERE',
  scope: 'https://www.googleapis.com/auth/calendar.readonly',
  ux_mode: 'popup',
  callback: (response: any) => {
    const xhr = new XMLHttpRequest();
    xhr.open('POST', code_receiver_uri, true);
    xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    // Set custom header for CRSF
    xhr.setRequestHeader('X-Requested-With', 'XmlHttpRequest');
    xhr.onload = function () {
      console.log('Auth code response: ' + xhr.responseText);
    };
    xhr.send('code=' + code);
  },
});

const handleGoogleCalendarConnect = () => {
  client.requestCode();
};
</script>

但是我在"google“上发现了一个TypeScript错误,上面写着:Cannot find name 'google'. ts(2304)

也许是因为我错过了谷歌身份服务JavaScript SDK的类型?如果是的话,哪里可以找到他们?

或者问题是别的什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-05-14 10:41:48

找到正确的类型包:

npm i @types/google.accounts

这将消除错误。

票数 11
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72238650

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档