WEB前端:vuejs全家桶(17): 方法(1): vm.$mount() vm.$destroy() vm.$nextTick(callback)

本文介绍了Vue.js中实例的手动挂载方法及其在DOM更新中的应用,通过具体示例展示了如何使用$mount方法手动挂载Vue实例到指定元素,并解释了$nextTick方法在确保DOM更新完成后的回调调用。

方法


    vm.$mount()
    vm.$destroy()
    vm.$nextTick(callback)

   

vm.$mount()

方法1

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>vue</title>
    <script src="js/vue.js"></script>
</head>

<body>
    <div class="itany">
        {{msg}}

    </div>

</body>
<script>
    var vm = new Vue({
        // el: '.itany',
        data: {
            msg: 'welcome to itany'
        },


    });
    // 方法
    //vm.$mount()  手动挂载vue实例
    vm.$mount('.itany');
</script>

</html>

方法2

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>vue</title>
    <script src="js/vue.js"></script>
</head>

<body>
    <div class="itany">
        {{msg}}

    </div>

</body>
<script>
    var vm = new Vue({
        data: {
            msg: '欢迎你!',
            name: 'car',
        }
    }).$mount('.itany')
</script>

</html>

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <title>vue</title>
    <script src="js/vue.js"></script>
</head>

<body>
    <div class="itany">
        {{msg}}
        <hr>
        <h1 ref="title">标题:{{name}}</h1>
    </div>

</body>
<script>
    var vm = new Vue({
            data: {
                msg: '欢迎你!',
                name: 'car',
            }
        }).$mount('.itany')
        //vm.$destroy() 销毁实例
        // vm.$destroy();

    // vm.$nextTick(callback) 在DOM更新完成后再执行回调函数, 一般在修改数据之后使用该方法,以便获取更新后的DOM
    vm.name = '面条';
    //DOM还没更新完,Vue实现响应式并不是数据发生改变之后DOM立即变化,需要按一定的策略进行DOM更新,需要时间!!
    // console.log(vm.$refs.title.textContent);
    vm.$nextTick(function() {
        //DOM更新完成,更新完成后再执行此代码
        console.log(vm.$refs.title.textContent);
    })
</script>

</html>

在这里插入图片描述

async createDialog (title, component, config = {}){ //vuejs.org/v2/guide/render-function.html const props = config.props || {} const events = config.events || {} let vm let { childComponent, options } = await this.loadChildComponent(component, props, events, function (){ return vm }) return new Promise((resolve, reject) => { vm = new Vue({ router: this.$router, route: this.$route, store, i18n, render (createElement) { let props = { customClass: "thin-dialog", showClose: true, //显示关闭按钮 closeOnClickModal: false, closeOnPressEscape: false, modalAppendToBody: true, appendToBody: true, visible: true, modal: true, title: title, width: "66%", top: "15vh" } console.log(props) Object.keys(props).forEach(key=>{ if(config.hasOwnProperty(key)){ if(key === "customClass"){ props[key] = `${props[key]} ${config[key]}` }else{ props[key] = config[key] } } }) if(config.modal === false){ props.customClass += " non-modal-dialog" } props.beforeClose = function (done){ const child = vm.$children[0].$children[0] if(config.beforeClose && typeof config.beforeClose === "function"){ return config.beforeClose(done, child, vm) } return done() } const dialog = createElement( ElementUI.Dialog, { attrs: {}, directives: [ { name: "dialogDrag" } ], props, on: { close: async () => { vm.remove(true, "cancel") }, } }, [createElement(childComponent, options)] ) return dialog } }).$mount() if(config.parentVm){ vm.$parent = config.parentVm } document.body.appendChild(vm.$el) this.$nextTick(() => { if(config.padding){ try{ vm.$el.querySelector(".el-dialog__body").style.padding = config.padding }catch(error){ console.error(error) } } }) vm.remove = (error, data) => { try{ document.body.removeChild(vm.$el) vm.$destroy() if (error) { return reject(data) } resolve(data) }catch(error){ console.error("createDialog on remove", error) } } if(config.beforeCreate && typeof config.beforeCreate === "function"){ return config.beforeCreate(vm) } return vm }) },
12-03
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值