<div >
    <div class="addbar">
        <input type="text" v-model="inputVal"><button @click="addVal">添加</button>
    </div>  
    <div>共有:{{data.length}}条数据</div>
    <ul>
        <li v-for="(item,index) in data" :key="item"><button @click="deleteVal(index)" style="margin-right:20px;">删除</button>{{item}}</li>
    </ul> 
</div>
      
<script src="../dist/vue.global.js"></script>
      <script>
        // 创建实例方式变化了
        const { createApp, reactive, computed, ref, toRefs } = Vue
        const app = createApp({
          setup() {
            // reactivity api 
            // message相关
            const data = reactive({
              data: ['hello,vue3','this is a todolist demo'],
            })
            // count相关
            // const counter = reactive({
            //   count: 0,
            //   doubleCount: computed(() => counter.count * 2)
            // })
      
            function addVal() {
                data.data.push(inputVal.value)
                inputVal.value = ''
            }
            function deleteVal(index){
                data.data.splice(index,1)
                console.log(data.data)
            }
            // 单值响应式, ref()返回Ref对象,如果要修改它的值,访问value属性
            const inputVal = ref('')
            
            return { ...toRefs(data), inputVal,addVal,deleteVal }
          }
        }).mount('#app')
      </script>

 

相关文章:

  • 2022-02-25
  • 2022-01-09
  • 2021-09-05
  • 2021-11-01
  • 2022-12-23
  • 2021-09-17
  • 2022-02-13
猜你喜欢
  • 2022-01-12
  • 2021-12-01
  • 2021-12-18
  • 2021-12-17
  • 2021-05-21
相关资源
相似解决方案