【转】vue 手动挂载$mount() 获取 $el

原文:https://www.cnblogs.com/CyLee/p/8425183.html

手动挂载$mount()

如果没有挂载的话,没有关联的 DOM 元素。是获取不到$el的。

https://vuejs.org/v2/api/#vm-mount

var MyComponent = Vue.extend({
  template: '<div>Hello!</div>'
})

// create and mount to #app (will replace #app)
new MyComponent().$mount('#app')

// the above is the same as:
new MyComponent({ el: '#app' })

// or, render off-document and append afterwards:
var component = new MyComponent().$mount()
document.getElementById('app').appendChild(component.$el)

  

原文地址:https://www.cnblogs.com/gzh4455/p/11149662.html