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>