实例方法

1.$mount方法

是用来挂载我们的扩展的,这里我们做了 Cat 的扩展,用 $mount 方法把它挂载到我们的Dom上,我们也生成了一个Vue实例
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>实例方法</title>
    <script src="../assets/js/vue.js" type="text/javascript"></script>
    <script src="../assets/js/jquery.js" type="text/javascript"></script>
</head>
  <body>
    <h1>实例方法</h1>
    <hr />
    <div id="app">
    </div>
    <script>
        var Cat=Vue.extend({
            template:`<h3>{{message}}</h3>`,
            data:function(){
                return{
                    message:'hello,I am Cat'
                }
            },
            mounted(){
                console.log('mounted被创建')
            }
        })

        var vm = new Cat().$mount('#app')
    </script>
  </body>
</html>

2.$destroy()卸载方法
在这里插入图片描述

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

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>实例方法</title>
    <script src="../assets/js/vue.js" type="text/javascript"></script>
    <script src="../assets/js/jquery.js" type="text/javascript"></script>
</head>

<body>
    <h1>实例方法</h1>
    <hr />
    <div id="app">

    </div>
    <p>
        <button onclick="destroy()">卸载</button>
    </p>
    <script>
        var Cat = Vue.extend({
            template: `<h3>{{message}}</h3>`,
            data: function () {
                return {
                    message: 'hello,I am Cat'
                }
            },
            mounted() {
                console.log('mounted被创建')
            },
            destroyed(){
                console.log('destroy 销毁之后')
            }
        })

        var vm = new Cat().$mount('#app')
        function destroy(){
            vm.$destroy()
        }
    </script>
</body>

</html>

3.$forceUpdate()更新方法
在这里插入图片描述

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

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>实例方法</title>
    <script src="../assets/js/vue.js" type="text/javascript"></script>
    <script src="../assets/js/jquery.js" type="text/javascript"></script>
</head>

<body>
    <h1>实例方法</h1>
    <hr />
    <div id="app">

    </div>
    <p>
        <button onclick="destroy()">卸载</button>
    </p>
    <p>
        <button onclick="update()">刷新</button>
    </p>
    <script>
        var Cat = Vue.extend({
            template: `<h3>{{message}}</h3>`,
            data: function () {
                return {
                    message: 'hello,I am Cat'
                }
            },
            mounted() {
                console.log('mounted被创建')
            },
            destroyed() {
                console.log('destroy 销毁之后')
            },
            updated(){
                console.log('update 刷新之后')
            }
        })

        var vm = new Cat().$mount('#app')
        function destroy() {
            vm.$destroy()
        }
        function update(){
            vm.$forceUpdate()
        }
    </script>
</body>

</html>

4.$nextTick()数据修改方法
在这里插入图片描述

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

<head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>实例方法</title>
    <script src="../assets/js/vue.js" type="text/javascript"></script>
    <script src="../assets/js/jquery.js" type="text/javascript"></script>
</head>

<body>
    <h1>实例方法</h1>
    <hr />
    <div id="app">

    </div>
    <p>
        <button onclick="destroy()">卸载</button>
    </p>
    <p>
        <button onclick="update()">刷新</button>
    </p>
    <p>
        <button onclick="tick()">修改</button>
    </p>
    <script>
        var Cat = Vue.extend({
            template: `<h3>{{message}}</h3>`,
            data: function () {
                return {
                    message: 'hello,I am Cat'
                }
            },
            mounted() {
                console.log('mounted被创建')
            },
            destroyed() {
                console.log('destroy 销毁之后')
            },
            updated() {
                console.log('update 刷新之后')
            }
        })

        var vm = new Cat().$mount('#app')
        function destroy() {
            vm.$destroy()
        }
        function update() {
            vm.$forceUpdate()
        }
        function tick(){
            vm.message='update message info'
            vm.$nextTick(function(){
                console.log('message更新完后被我调用了')
            })
        }
    </script>
</body>

</html>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值