vue3--简单尝试

1.安装vue/cli

npm i -g @vue/cli
  1. 创建项目目录
vue create mycode
  1. 编写配置文件(针对指定的IP和post)
touch vue.config.js
vim vue.config.js
const HOST= process.env.HOST;

module.exports = {
    devServer :{
        host: HOST||'0.0.0.0',
        port: 8080,
        disableHostCheck: true,
    }
}
  1. 启动命令
cd mycode
npm run serve
  1. todolist(vue3版本的简单demo)
<template>
    <div class="todo-list">
    <div class="header">todolist</div>
    <input type="text" v-model="name" placeholder="请输入名字">
    &nbsp;&nbsp;
    <input type="text" v-model="sex" placeholder="请输入性别">
    &nbsp;&nbsp;
    <button @click="add" v-if="index1.index === null">添加</button>
    <button @click="update" v-else>更新</button>
    <button @click="clear">清空数据</button>
    <ul>
    <li v-for="(item, index) in list" :key="index">
    <span>姓名: {{item.name}}</span>
    <span>性别: {{item.sex}}</span>
    &nbsp;
    <button @click="del(index)">删除</button>
    &nbsp;
    <button @click="edit(index)">编辑</button>
    </li>
    </ul>
    </div>
</template>
<script>
import { ref,reactive } from 'vue';
export default {
    name : "todolist",
    setup(){
        let list = ref([]);
        let name = ref("");
        let sex = ref("");
        let index1 = reactive({
            index:null
        });
        const add = ()=>{
            if(!name.value||!sex.value){
                return;
            }
            list.value.push({
                name: name.value,
                sex: sex.value,
            });
            name.value = "";
            sex.value = "";
        };
        const edit = (index)=>{
            let item = list.value[index];
            name.value = item.name;
            sex.value = item.sex;
            index1.index = index;
            console.log(index);
        };
        const update = ()=>{
            if (!name.value||!sex.value){
                return
            }
            list.value[index1.index].name = name.value;
            list.value[index1.index].sex = sex.value;
            sex.value = "";
            name.value = "";
            index1.index = null;
        };
        const del = (index)=>{
            list.value.splice(index, 1)
            index1.index = null;
            name.value = "";
            sex.value = "";
            
        };
        const clear=()=>{
            list.value.length = 0;
        }


        return{
            list,
            name,
            sex,
            index1,
            add,
            edit,
            update,
            del,
            clear,
        }
    }
}
</script>
<style lang="scss" scoped>
  .todo-list {
    width: 600px;
    margin: auto;
    .header {
      height: 60px;
      line-height: 60px;
      background-color: #3e3e3e;
      color: #dddddd;
      font-size: 48px;
      margin-bottom: 20px;
    }
    button {
      display: contents;
      cursor: pointer;
    }
    ul {
      padding: 0;
      li {
        text-align: left;
        height: 30px;
        margin-bottom: 10px;
        background-color: #ffffff;
        line-height: 30px;
        border-left: 5px solid #629a9c;
        border-radius: 5px;
        padding-left: 30px;
        padding-right: 30px;
        color: #ffdfa5;
        .btndiv {
          float: right;
        }
        .btn {
          margin-right: 20px;
          display: contents;
          cursor: pointer;
          text-align: right;
        }
      }
    }
  }

  li {
    list-style: none;
  }
</style>
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容