Instance Vue if element exists?

前端 未结 2 674
长情又很酷
长情又很酷 2021-02-13 00:17

I\'m building an app that a page has some vms, in others not. When we change from one page to another show the following warning:

[Vue warn]: Cannot find element:

2条回答
  •  被撕碎了的回忆
    2021-02-13 00:45

    This is a bit of an old question. I found this Vue example being useful

    https://vuejs.org/v2/api/#Vue-extend

    So you can create a Vue class and initiate it only if the dom element is present in the DOM.

    // create constructor
    var Profile = Vue.extend({
      template: '

    {{firstName}} {{lastName}} aka {{alias}}

    ', data: function () { return { firstName: 'Walter', lastName: 'White', alias: 'Heisenberg' } } }) // create an instance of Profile and mount it on an element if(document.getElementById("element-id")){ new Profile().$mount('#element-id') }

提交回复
热议问题