Vue3写法

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/vue@3"></script>
</head>
<body>
    <div id="app">
        <h3>count的值为:{{state.count}}</h3>
        <button v-on:click="addCount">+1</button>
        <button @click="state.count+=1">+1</button>
        <button v-on:click="negativeCount">-1</button>
    </div>

    <!-- <script>
        const vm ={
            data:function(){
                return {
                    count:0
                }
            },
            methods:{
                addCount(){
                    this.count+=1
                }
            }
        }

        Vue.createApp(vm).mount('#app')
    </script> -->
    <script>
        const {createApp,reactive} = Vue;
        createApp({
            setup(){
                const state = reactive({
                    count:0
                })

                const addCount = ()=>{
                    state.count+=1
                }

                const negativeCount = ()=>{
                    state.count-=1
                }
                
                return {
                    state,
                    addCount,
                    negativeCount
                }
            }
        }).mount('#app');
    </script>
</body>
</html>

 

posted @ 2025-10-01 00:52  火军刀  阅读(8)  评论(0)    收藏  举报