vue挂载到html,Vue挂载的实现

本文详细介绍了Vue组件的挂载过程,包括缓存$mount方法、避免挂载到根节点、转换template为render函数、编译过程、调用mountComponent方法、创建渲染Watcher以及mounted钩子的执行。内容涵盖了Vue组件从模板编译到实际DOM更新的整个流程。
摘要由CSDN通过智能技术生成

1 .首先缓存原型上的 $ mount方法,然后在重新定义这个方法

2 .检查el,避免Vue挂载到body,html这种根节点上。

3 .如果没有定义render方法,就会把el或者template字符串转换成render方法

const mount = Vue.prototype.$mount

Vue.prototype.$mount = function (

el?: string | Element,

hydrating?: boolean

): Component {

el = el && query(el)

/* istanbul ignore if */

if (el === document.body || el === document.documentElement) {

process.env.NODE_ENV !== 'production' && warn(

`Do not mount Vue to or

- mount to normal elements instead.`

)

return this

}

const options = this.$options

// resolve template/el and convert to render function

if (!options.render) {

let template = options.template

if (template) {

if (typeof temp

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值