使用 vue cli 重新制作一个 todolist

本文介绍如何在Vue.js项目中使用单文件组件(SFC)创建一个待办事项列表应用,包括启动项目、数据绑定、组件拆分及父子组件间的数据传递与事件触发。

首先,进入工程的目录,使用命令启动该工程 npm run start / npm run dev [可以参考工程目录下的 package.json文件]

(其中, x.vue 文件就是一个单文件组件)

下一步,更改单文件组件 x.vue.

  注意,Vue语法要求,.vue文件中,<template>标签中,只能包含一个根元素。

首先将template 中的结构写上:

<template>
  <div>
    <div>
      <input />
      <button>提交</button>
    </div>
    <div>
      <ul>
      </ul>
    </div>
  </div>
</template>

然后,进行数据绑定:

其中,data 在 vue cli 中,作为一个组件,data 以函数的形式存在,不再像以前,以对象形式存在。函数的返回值,为具体的数据。

<script>
  export default{
    data () {
      return {
        inputValue: ''
      }
    }
  }
</script>

数据绑定:

<template>
  <div>
    <div>
      <input v-model='inputValue'/>
      <button @click='handleSubmit'>提交</button>
    </div>
    <div>
      <ul>
      </ul>
    </div>
  </div>
</template>

<script>
  export default{
    data () {
      return {
        inputValue: '',
        list: []
      }
    },
    methods:{
      handleSubmit (){
        this.list.push(this.inputValue);
        this.inputValue = '';
      }
    }
  }
</script>

ul li 组件 拆分

不同与之前,Vue cli 拆分组件: 在 src 目录下 components 下使用 xxx.vue 

  引用该组件:

<template>
  <div>
    <div>
      <input v-model='inputValue'/>
      <button @click='handleSubmit'>提交</button>
    </div>
    <div>
      <ul>
        <todo-item></todo-item>
      </ul>
    </div>
  </div>
</template>

<script>
  import TodoItem from './components/Todoitem'
  export default{
    // 局部组件
    components:{
      'todo-item': TodoItem
    },
    data () {
      return {
        inputValue: '',
        list: []
      }
    },
    methods:{
      handleSubmit (){
        this.list.push(this.inputValue);
        this.inputValue = '';
      }
    }
  }
</script>

todo-list 向子组件传值

<template>
  <div>
    <div>
      <input v-model='inputValue'/>
      <button @click='handleSubmit'>提交</button>
    </div>
    <div>
      <ul>
        <todo-item
          v-for='(item,index) of list'
          :content="item"
        ></todo-item>
      </ul>
    </div>
  </div>
</template>

<script>
  import TodoItem from './components/Todoitem'
  export default{
    // 局部组件
    components:{
      'todo-item': TodoItem
    },
    data () {
      return {
        inputValue: '',
        list: []
      }
    },
    methods:{
      handleSubmit (){
        this.list.push(this.inputValue);
        this.inputValue = '';
      }
    }
  }
</script>

子组件中,申明使用的属性,并使用插值表达式使用:

<template>
  <li>{{content}}</li>
</template>

<script>
export default {
  props: ['content']
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>

删除子组件:

子组件文件:

<template>
  <li @click="handleDelete">{{content}}</li>
</template>

<script>
export default {
  props: ['content', 'index'],
  methods: {
  	handleDelete () {
  		this.$emit('delete',this.index);
  	}
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>

</style>

父组件文件:

<template>
  <div>
    <div>
      <input v-model='inputValue'/>
      <button @click='handleSubmit'>提交</button>
    </div>
    <div>
      <ul>
        <todo-item
          v-for='(item,index) of list'
          :key="item"
          :content="item"
          :index="index"
          @delete="handleDelete"
        ></todo-item>
      </ul>
    </div>
  </div>
</template>

<script>
  import TodoItem from './components/Todoitem'
  export default{
    // 局部组件
    components:{
      'todo-item': TodoItem
    },
    data () {
      return {
        inputValue: '',
        list: []
      }
    },
    methods:{
      handleSubmit (){
        this.list.push(this.inputValue);
        this.inputValue = '';
      },
      handleDelete (index){
        this.list.splice(index, 1);
      }
    }
  }
</script>

 

好的,以下是使用 Vue CLI 构建一个三层边框的 todolist 的步骤: 1. 安装 Vue CLI 首先需要安装 Vue CLI,可以使用以下命令进行安装: ``` npm install -g @vue/cli ``` 2. 创建一个新的 Vue 项目 使用以下命令创建一个新的 Vue 项目: ``` vue create todolist ``` 然后根据提示选择需要的配置项。 3. 创建一个 TodoList 组件 在 src/components 目录下创建一个 TodoList.vue 文件,然后编写以下代码: ```html <template> <div class="todo-list"> <div class="todo-list-header">Todo List</div> <div class="todo-list-content"> <!-- Todo List Content Here --> </div> <div class="todo-list-footer"> <input type="text" placeholder="Add Todo" /> <button>Add</button> </div> </div> </template> <script> export default { name: "TodoList", }; </script> <style> .todo-list { border: 2px solid #ccc; border-radius: 4px; padding: 20px; margin: 20px; } .todo-list-header { font-size: 24px; font-weight: bold; margin-bottom: 10px; } .todo-list-content { border: 1px solid #ccc; border-radius: 4px; padding: 10px; height: 400px; overflow-y: scroll; margin-bottom: 10px; } .todo-list-footer { display: flex; justify-content: space-between; align-items: center; } .todo-list-footer input[type="text"] { flex: 1; border: 1px solid #ccc; border-radius: 4px; padding: 5px; margin-right: 10px; } .todo-list-footer button { background-color: #4caf50; color: #fff; border: none; border-radius: 4px; padding: 5px 10px; cursor: pointer; } </style> ``` 这里我们创建了一个 TodoList 组件,包括一个头部、一个内容区域和一个底部输入框。我们使用了 CSS3 的边框样式,实现了三层边框的效果。 4. 引入 TodoList 组件 在 App.vue 文件中引入 TodoList 组件: ```html <template> <div id="app"> <TodoList /> </div> </template> <script> import TodoList from "./components/TodoList.vue"; export default { name: "App", components: { TodoList, }, }; </script> ``` 5. 运行项目 使用以下命令启动项目: ``` npm run serve ``` 在浏览器中访问 http://localhost:8080,就可以看到我们创建的三层边框的 todolist 了。 希望这个回答能够帮到您!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值