[Vuejs]-Mount view component only on a single Rails View

0👍

Because Vue tries to render the component (on #product_search) when document is ready, meaning on every page of your application.

You can add a condition to prevent the null error:

document.addEventListener('DOMContentLoaded', () => {
  let element = document.querySelector('#product_search')
  if (element) {
    document.body.appendChild(document.createElement('app'))
    console.log('caricato Vue');
    const app = new Vue({
      render: h => h(Product)
    }).$mount('#product_search')
  }
})

Leave a comment