vue 中 this.$options.data() 重置
<!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>Document</title>
</head>
<body>
<div id="app">
<button @click="btnClick">click</button>
<button @click="btnReset">reset</button>
<p><input type="text" v-model="message"></p>
<p>{{message}}</p>
</div>
<script src="./vue.js"></script>
<script>
let vm = new Vue({
el: "#app",
data() {
return {
message: ''
}
},
methods: {
btnClick() {
this.message = 'bbbbbbbbbb'
},
btnReset() {
Object.assign(this.$data, this.$options.data.call(this))
}
},
})
</script>
</body>
</html>
注意:这里的data是一个函数,而不是对象
文章来自:https://www.cnblogs.com/samsara-yx/p/12192276.html