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

246
Visualizações
Vue not rendering fetched html

I am using vue in shopify and am working on a collection page. When I click on a filter, it‘s an href and it updates the url and reloads the page.

So I have a product grid

<div class="grid-wrapper">
  <template v-for="(product, index) in collection.products">
    <div class="product-item"></div>
  </template>
</div>

And my idea was to just use the same url with fetch so the page doesn‘t reload.

I did this

fetch('/collections?variant=black')
  .then(res => res.text())
  .then(html => new DOMParser().parseFromText(html, 'text, html'))
  .then(data => {
    document.querySelector('.grid-wrapper').innerHTML = data.querySelector('.grid-wrapper').innerHTML
  })

This does not work because I get back the actual <template v-for…> as the new innerHTML and vue isnt taking over. How can I solve this

In shopify I converted the object like so

const collection = (() => {
  const collection = {{ collection | json }}
  const products = {{ collection.products | json }}
  collection.products = products
  return collection
})();

Then in my vue instance

new Vue.createApp({
  data() {
    collection: collection
  }
}).mount('#app')
about 4 years ago · Santiago Gelvez
1 Respostas
Responde à pergunta

0

You're approaching this in the traditional JavaScript way of manipulating the DOM directly. In Vue, we set state which can then be rendered by your template.

Instead:

  1. Create a data attribute to store your state
  2. Under methods, write a function to fetch your data, then update the components data
  3. Call your function in the components created hook
  4. In you're template render the results
    • Don't forget to check it's present first, with v-if
    • You can use v-for to iterate over, and render lists

Here's a working demo


I don't have access to your API endpoint, so for demo purposes, am just using the GitHub API, to fetch and render a list of all repos in the Vue.js organization.

Here's what it looks like:

Vue.config.devtools = false;
    Vue.config.productionTip = false;
    new Vue({
      el: '#app',
      name: 'dbzx10299-demo',
  data() {
    return {
      loaded: false,
      response: null,
    }
  },
  methods: {
    fetchData() {
      const demoEndpoint = 'https://api.github.com/orgs/vuejs/repos';
      fetch(demoEndpoint)
      .then(response => response.json())
      .then(data => {
        this.response = data;
        this.loaded = true;
      })
    },
  },
  mounted() {
    this.fetchData();
  },
    })
<script src="https://unpkg.com/vue@2.x/dist/vue.js"></script>

<div id="app">
<div class="hello">
  <h2>Vue Repo List - Data fetching example</h2>
  <div v-if="!loaded">Loading...</div>
  <ul v-else>
    <li v-for="(repo, index) in response" :key="index">
      <a :href="repo.html_url" :title="repo.description" target="_blank">
        {{ repo.name }}
      </a>
      <i>★ {{ repo.stargazers_count }}</i>
    </li>
  </ul>
</div>
</div>

about 4 years ago · Santiago Gelvez 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