简体   繁体   中英

Run vue component only if element loaded

I have a small vue.js component defined on a javascript.

Is any elegant way to instance the vue component only when the element is loaded?

The issue is I am receiving these warnings in other htmls where the element is not present:

vue.js:485 [Vue warn]: Cannot find element: #item

Because it's running this js everywhere

  new Vue({
    el: '#item',
  });

How is it taken care of in vue.js?

Test for the element before creating the instance.

if(document.getElementById("#item")){
  new Vue({
    el: '#item',
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM