Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

117
Visualizações
I can't use a Client Plugins to render an element in a Client Only Tags in Nuxt

I'm using Nuxt on a project that has a search input in the header.

I Implemented it using Mabpox Geocoder and it works fine but on first page load the search input appears like this enter image description here

When the mounted hook gets executed and the plugin loads the geocoder it looks fine enter image description here

I wanted to use the <client-only> tag to put a placeholder until the real search is fetched But I get this error:

500 can't access property "querySelector", geocoderContainer is null

Search.vue:

<template>
  <div> 
    <client-only>
      <div :id="geocoderID" @resultFound="resultFound"></div>
    </client-only>
  </div>
</template>

<script>
export default {
  computed: {
    geocoderID() {
      return `mapbox-geocoder-${this.containerID}`;
    },
  },

  mounted() {
    this.$mapboxMaps.createGeocoder(this.geocoderID);
  },
}
</script>

My Mapbox.client.js plugin:

import "mapbox-gl/dist/mapbox-gl.css";
const mapboxgl = require("mapbox-gl");
import MapboxGeocoder from "@mapbox/mapbox-gl-geocoder";
import "@mapbox/mapbox-gl-geocoder/dist/mapbox-gl-geocoder.css";

export default function (context, inject) {
  inject("mapboxMaps", { createGeocoder });
  mapboxgl.accessToken = <my-access-token>

  function createGeocoder(containerID) {
    // Only add the search box if there's none (avoid duplications of boxes)
    const geocoderContainer = document.getElementById(containerID);
    if (geocoderContainer.querySelector(".mapboxgl-ctrl-geocoder")) return;

    const geocoder = new MapboxGeocoder({
      accessToken: mapboxgl.accessToken,
      enableEventLogging: false,
      mapboxgl: mapboxgl,
    });

    geocoder.addTo(geocoderContainer);
  }

When I try to log the div to which i want to attach the search using the following

mounted() {
    console.log(this.geocoderID);
    console.log(document.getElementById(this.geocoderID))
    this.$mapboxMaps.createGeocoder(this.geocoderID);
  },

It logs this

mapbox-geocoder-nav-search

undefined

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

I found the answer in the docs enter image description here

This is what I did

methods: {
  renderSearch(count = 10) {
    this.$nextTick(() => {
      if (document.getElementById(this.geocoderID)) {
        this.$mapboxMaps.createGeocoder(this.geocoderID);
      } else if (count > 0) {
        this.initClientOnlyComp(count - 1);
      }
    });
  },
},

mounted() {
  this.renderSearch();
},

It worked fine, I logged the count just for curiousity and the element was rendered at count = 9.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda