Vue

Vue中this.$options.data() 重置data里的数据

Joe
Joe
2021-02-20 / 0 评论 / 24 阅读 / 正在检测是否收录...
<!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>
1

评论 (0)

取消