開發中遇到的bug-Property or method “xxxx“ is not defined on the instance but referenced during render.

火腿腸燒烤大賽冠軍發表於2020-10-10

method沒加s加上就好了

<body>
    <div id="app">
        <button @click="sub">-</button>
        <span>{{code}}</span>
        <button @click="add">+</button>
    </div>
    <script>
        var app = new Vue({
            el: "#app",
            data: {
                code: 0
            },
            methods: {
                add: function () {
                    if (this.code < 10) {
                        this.code++;
                    } else {
                        alert('到頭啦!不要再加啦');
                    }
                },
                sub: function () {
                    if (this.code > 0) {
                        this.code--;
                    } else {
                        alert('到頭啦!不要再減啦');
                    }
                }
            }
        })
    </script>
</body>

相關文章