话不多说上代码

  • vue>src>App.vue
<template>
<div id="app">
<!-- header -->
<Header/>
<AddTodo v-on:handleAdd="handleAdd"/>
<Todos :todos="todos" @handleDelete="handleDelete"/> </div> </template> <script>
import Todos from "./components/Todos";
import Header from "./components/layout/Header";
import AddTodo from "./components/layout/AddTodo";
export default { name:"app",
data(){
return{
msg:"hello",
todos:[
{
id:,
title:"代办事项1",
completed:false
},
{
id:,
title:"代办事项2",
completed:false
},
{
id:,
title:"代办事项3",
completed:false
},
]
}
},
components:{
Todos,
Header,
AddTodo,
},
methods:{
handleDelete(id){
// console.log(id);
this.todos=this.todos.filter(todo =>todo.id !==id)
},
handleAdd(newTodo){
// this.todos.unshift(newTodo);
this.todos=[...this.todos,newTodo]
}
}
}
</script> <style>
*{
box-sizing:border-box;
margin: ;
padding: ;
} body{
font-family: Arial, Helvetica, sans-serif;
line-height: 1.4;
}
</style>

vue>src>commponents>Todoo.vue

<template>
<div>
<div :key="todo.index" v-for="(todo) in todos">
<!-- <Todoitem :todo="todo" @deleteItem="deleteItem"/> -->
<Todoitem :todo="todo" @deleteItem="$emit('handleDelete',todo.id)"/>
</div>
</div>
</template> <script>
import Todoitem from './Todoitem';
export default {
name:"Todos",
props:{
todos:{
type:Array
}
},
components:{
Todoitem
},
methods:{
// deleteItem(id){
// console.log(id);
// }
}
};
</script> <style scoped> </style>
  • vue>src>commponents>Todooitem.vue
<template>
<div class="todo-item" :class="{'is-complete':todo.completed}">
<p>
<input type="checkbox" @change="markComplete">
{{todo.title}}
<button class="del" @click="$emit('deleteItem',todo.id)">x</button>
</p>
</div>
</template> <script>
export default {
name:"Todos",
props:["todo"],
methods:{
markComplete(){
// console.log(this.todo);
this.todo.completed = !this.todo.completed;
}
} }
</script> <style scoped>
.todo-item{
background: #f4f4f4;
padding: 10px;
border-bottom: 1px #ccc dotted; }
.is-complete{
text-decoration: line-through;
}
.del{
background: #ff0000;
color: #fff;
border: none;
padding: 5px 9px;
border-radius: %;
cursor: pointer;
float: right;
} </style>
  • vue>src>commponents>AddTodo.vue
<template>
<div>
<form @submit="addTodo">
<input v-model="title" type="text" name="title" placeholder="请添加代办事项……"/>
<input type="submit" value="添加" class="btn">
</form>
</div>
</template> <script>
import uuid from "uuid";
export default {
name:"AddTodo",
data(){
return{
title:""
}
},
methods:{
addTodo(e){
e.preventDefault();
// console.log('title:', this.title)
const newTodo={
id:uuid.v4(),
title:this.title,
completed:false
};
console.log('tag',newTodo )
//注册事件
this.$emit("handleAdd",newTodo)
this.title="";
}
}
}
</script>
<style scoped>
form{
display:flex;
}
input[type="text"]{
flex:;
padding: 5px;
}
input[type="submint"]{
flex: ;
}
.btn{
display: inline-block;
border: none;
background: #;
color:#fff;
padding:7px 20px;
cursor: pointer;
}
.btn:hover{
background: #;
}
</style>
  • vue>src>commponents>Header.vue
<template>
<header class="header">
<h1>代办事项</h1>
</header>
</template> <script>
export default {
name: 'Header', }
</script> <!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.header{
background: #;
color:#fff;
text-align: center;
padding:;
}
</style>

运行效果

一入前端深似海

Vue中的$emit组件事件运用的更多相关文章

  1. Vue中利用$emit实现子组件向父组件通信

    Vue中利用$emit实现子组件向父组件通信 父组件 <template> <div> <p>我是父组件</p> <child :isShow=& ...

  2. 【Vue】Vue中的父子组件通讯以及使用sync同步父子组件数据

    前言: 之前写过一篇文章<在不同场景下Vue组件间的数据交流>,但现在来看,其中关于“父子组件通信”的介绍仍有诸多缺漏或者不当之处, 正好这几天学习了关于用sync修饰符做父子组件数据双向 ...

  3. VUE中 $on, $emit, v-on三者关系

    VUE中 $on, $emit, v-on三者关系 每个vue实例都实现了事件借口 使用$on(eventName)监听事件 使用$emit(eventName)触发事件 若把vue看成家庭(相当于一 ...

  4. vue中的父子组件相互调用

    vue中的父子组件相互调用: 1.vue子组件调用父组件方法:子组件:this.$emit('xx'); 父组件:定义yy方法,并在引用子组件时传参,如@xx="yy" 2.vue ...

  5. vue中router-link的click事件失效的解决办法

    title: vue中router-link的click事件失效的解决办法 toc: false date: 2018-12-04 16:28:49 categories: Web tags: vue ...

  6. vue中8种组件通信方式, 值得收藏!

    vue是数据驱动视图更新的框架, 所以对于vue来说组件间的数据通信非常重要,那么组件之间如何进行数据通信的呢? 首先我们需要知道在vue中组件之间存在什么样的关系, 才更容易理解他们的通信方式, 就 ...

  7. vue中兄弟之间组件通信

    我们知道Vue中组件之间的通信有很多方式,父子之间通信比较简单,当我们使用vuex时候,兄弟组件之间的通信也很好得到解决 当我们项目较小时候,不使用vuex时候Vue中兄弟组件之间的通信是怎样进行的呢 ...

  8. Vue中iframe和组件的通信

    最近的项目开发中用到了Vue组件中嵌套iframe,相应的碰到了组件和HTML的通信问题,场景如下:demo.vue中嵌入 test.html 由于一般的iframe嵌套是用于HTML文件的,在vue ...

  9. 关于Vue中页面(父组件)下拉,页面中的子组件加载更多数据的实现方法

    一个项目中存在很多这种情况:父组件(页面)中的子组件需要做下拉加载更多的需求,但是这个下拉到底部的动作只能通过监控页面(父组件)来完成 这就需要父子组件之间的通信,代码如下: 1. 建立一个用于父子组 ...

随机推荐

  1. 2.9_Database Interface ADO结构组成及连接方式实例

    说通俗点OLE DB和ODBC都是最底层的东西,而ADO对象给我们提供了一个“可视化”和应用层直接交互的组件,ADO对象T通过OLE DB间接取得数据库中的数据,如下图: 从上面看出,可以说ADO是应 ...

  2. DuplexChannel

    [ServiceContract(Namespace = "http://xx.com", CallbackContract = typeof(Ipub_c))] public i ...

  3. angularjs 动态加载指令------编译服务$compile

    场景: 我们写了一个自定义的指令,这条指令需要一些数据,而这些数据需要在某些操作之后才能就绪,这时候,我们就需要在数据就绪之后,动态加载指令. 示例: js: $scope.$watch('repor ...

  4. 父元素设置min-height子元素设置100%问题

    问题:父元素设置min-height子元素高度设置100%取不到值,这是因为子元素 div设置 height:100%: 只有当父级元素满足min-height:1000px;设置的条件才触发: 浏览 ...

  5. 【转载】C#中List集合使用Reverse方法对集合中的元素进行倒序反转

    在C#的List集合操作中,有时候需要对List集合中的元素的顺序进行倒序反转操作,此时就可使用到List集合中的Reverse方法来实现此功能,Reverse方法的签名为void Reverse() ...

  6. 纯 CSS 画 iphone

    好几天没有更新了,直接上效果吧,哈哈!(我想这个应该大部分都会!哈哈哈!) 代码如下: html: <div class="container"> <div cl ...

  7. 【Linux下Hadoop-eclipse-plus-3.2.0】编译Hadoop连接eclipse的插件遇见的一系列错误,崩溃的操作

    2019-09-02 23:35:22 前言:首先,我想吐槽下自己,居然花费了4到5个夜晚和中午的时间来做这件事情,直到刚才才顺利解决,我也挺佩服自己的! 我在这个过程中参考其他人的博客,非常感谢他们 ...

  8. PHP传引用/作用域 问题

    $arr = [1,2,3]; foreach($arr as &$v) { //nothing todo. } foreach($arr as $v) { //nothing todo. } ...

  9. php基本数据类型

    trim()函数,用于去除字符串首尾空格和特殊字符返回的是去掉的空格和特殊字符后的字符串 string trim(string str [,string charlist]); str 要操作的字符串 ...

  10. LFS7.10——构造临时Linux系统

    参考:LFS编译——准备Host系统 前言 在准备好Host环境后,接下来构造一个临时Linux系统.该系统包含****构建所需要的工具.构造临时Linux系统分两步: 构建一个宿主系统无关的新工具链 ...