Мне на самом деле пришлось столкнуться с той же самой проблемой вчера, поэтому хорошее время;) В моем случае представление на первой странице состоит из двух разделов, один из которых содержит переключатели и из-за требований бизнеса, зависящих от выбора переключателя пользователя, ввод текстас картами Google автозаполнение будет включено (или останется отключенным)
В этом сценарии было гораздо более эффективно загружать страницу без библиотек карт Google, а затем динамически загружать код gmaps после полной визуализации веб-компонента, что приводитдо 50% времени, затрачиваемого на интерактив :) Вот что я в итоге сделал.
ПРИМЕЧАНИЕ. Метод loadGoogleMaps () и объявление переменной initCalled находятся вне класса и, следовательно, за пределами веб-компонента (я поставилих под заявлениями на импорт).Я также пропустил большую часть кода класса из примера, так как он не относился к вашему вопросу:)
import { html } from '@polymer/lit-element';
import { PageViewElement } from './page-view-element.js';
import { SharedStyles } from './shared-styles.js';
import '@vaadin/vaadin-radio-button/vaadin-radio-button.js';
import '@vaadin/vaadin-radio-button/vaadin-radio-group.js';
import { spinner } from './my-icons.js';
let initCalled;
function loadGoogleMaps() {
//Only load gmaps if it has not been loaded before (tracked by the initCalled flag)
if (!initCalled) {
//Dynamically import the library and append it to the document header
const script = document.createElement('script');
script.type = 'text/javascript';
script.async = true;
script.onload = function () {
//Code to execute after the library has been downloaded parsed and processed by the browser starts here :)
initCalled = true;
//TODO: Refactor this DOM traversing logic
const searchAutocomplete = document.querySelector('my-app').shadowRoot.querySelector("home-property-view")
.shadowRoot.querySelector('home-property').shadowRoot.querySelector("input[type='text']");
const autocomplete = new google.maps.places.Autocomplete(
searchAutocomplete, {
types: ['address'],
componentRestrictions: { //Limit to only US addresses
'country': 'us'
}
});
autocomplete.addListener('place_changed', function () {
const place = autocomplete.getPlace();
dispatchEvent(new CustomEvent('propertyAddressChanged', {
bubbles: true,
composed: true,
detail: place
}));
});
};
//Specify the location of the gmaps library
script.src = '//maps.googleapis.com/maps/api/js?v=3.33&key=<YOUR-API-KEY-GOES-HERE>&libraries=places';
//Append it to the document header
document.head.appendChild(script);
}
}
class HomeProperty extends PageViewElement {
//....omitted class code for brevity...
_didRender(props, changedProps, prevProps) {
loadGoogleMaps();
}
//....omitted class code for brevity...
}