如何使用SvelteKit加载Google API客户端库



我是SvelteKit的新手,正在尝试了解如何为Javascript加载谷歌客户端库。

谷歌告诉我这样做:

<head>
<script src="https://apis.google.com/js/api.js"></script>
<script>
function start() {
// Initializes the client with the API key and the Translate API.
gapi.client.init({
'apiKey': 'YOUR_API_KEY',
'discoveryDocs': ['https://www.googleapis.com/discovery/v1/apis/translate/v2/rest'],
}).then(function() {
// Executes an API request, and returns a Promise.
// The method name `language.translations.list` comes from the API discovery.
return gapi.client.language.translations.list({
q: 'hello world',
source: 'en',
target: 'de',
});
}).then(function(response) {
console.log(response.result.data.translations[0].translatedText);
}, function(reason) {
console.log('Error: ' + reason.result.error.message);
});
};
// Loads the JavaScript client library and invokes `start` afterwards.
gapi.load('client', start);
</script>
</head>

问题是SvelteKit不允许在一个页面上有2个或更多的脚本标记(我不希望它成为布局页面(。

<script src="https://apis.google.com/js/api.js"></script>
<script>
import { onMount } from 'svelte';

gapi.client.init({...
</script>  

这导致以下错误消息:

A component can only have one instance-level <script> element

由于我的意图是使用Workbox创建一个渐进式web应用程序(PWA(,我不想像这里描述的那样导入谷歌库,因为包含这个库的包会变得太重。

你知道如何加载谷歌客户端库吗?也许有一种Workbox的方法可以做到这一点?在谷歌或YouTube上找不到SvelteKit示例。

提前感谢

svelte:head标记允许您在加载组件时向文档头添加资源。这个例子应该有效:

<script>
const start = async () => {
// Initializes the client with the API key and the Translate API.
// @ts-ignore
gapi.client.init({
'apiKey': 'YOUR_API_KEY',
'discoveryDocs': ['https://www.googleapis.com/discovery/v1/apis/translate/v2/rest'],
}).then(function() {
// Executes an API request, and returns a Promise.
// The method name `language.translations.list` comes from the API discovery.
return gapi.client.language.translations.list({
q: 'hello world',
source: 'en',
target: 'de',
});
}).then(function(response) {
console.log(response.result.data.translations[0].translatedText);
}, function(reason) {
console.log('Error: ' + reason.result.error.message);
});
};
const initializeGapi = async () => {
gapi.load('client', start);
}
</script>
<svelte:head>
<script src="https://apis.google.com/js/api.js" on:load={initializeGapi}></script>
</svelte:head>

我做了这样的东西。将其保存为GoogleMap.svelte到您的lib文件夹中。并像这样使用它;

<GoogleMap
{map}
globally
on:load={() => {
console.log('MAP SAYS IM LOADED');
}}
/>
  • Map是一个引用对象
  • globally将其定义为window.map
<script>
import { onMount } from 'svelte';
import { createEventDispatcher } from 'svelte';
const dispatch = createEventDispatcher();

//import mapStyles from './map-styles'; // optional

export let globally = false;
export let map;
let src = '';
const key = ''; 

// @ts-ignore
let container;
let zoom = 8;
let center = { lat: 37.5742776, lng: 43.7260158 };

onMount(() => {
Object.assign(window, {
mapLoaded: () => {
// @ts-ignore
map = new google.maps.Map(container, {
zoom,
center
// styles: mapStyles
});
dispatch('load', true);
if (globally) {
Object.assign(window, { map });
}
}
});
//Assign
src = `https://maps.googleapis.com/maps/api/js?key=${key}&callback=mapLoaded`;
});
</script>

<!-- This is tailwind css class change with whatever fits to your case. -->
<div class="w-full h-full" bind:this={container} />
<svelte:head>
{#if src}
<script {src}></script>
{/if}
</svelte:head>

最新更新