昆明网站的优化免费开网店的app
news/
2025/9/24 7:22:23/
文章来源:
昆明网站的优化,免费开网店的app,wifi网络服务商电话,如何设计自己的网站组件的自定义事件 一种组件间通讯的方式#xff0c;适用于#xff1a;子组件 — 父组件 绑定自定义事件 第一种方式:在父组件中写单标签1.Demo 事件名方法2.Demo v-on:事件名方法/第二种方式:使用ref绑定(配置在生命周期中)this…组件的自定义事件 一种组件间通讯的方式适用于子组件 — 父组件 绑定自定义事件
第一种方式:在父组件中写单标签1.Demo 事件名方法2.Demo v-on:事件名方法/第二种方式:使用ref绑定(配置在生命周期中)this.$ refs.demo.$on(‘事件名’,方法)Demo refdemo例(2)配置在生命周期中:mounted() {this.$refs.demo.$on(atguigu,this.test) }
若想让自定义事件只能触发一次可以使用once修饰符或once方法
触发事件 触发自定义事件 this. $emit(事件名,数据) 解绑事件 解绑自定义事件 this. $off(事件名) 绑定原生事件 组件上也可以绑定原生事件 需要使用native修饰符 如 click.nativeshow 上面绑定自定义事件即使绑定的是原生事件也会被人为是自定义的需要加native,加了后就将此事件给组件的根元素 注意 通过this.r e f s . x x x . refs.xxx.refs.xxx.on(‘事件名’,回调函数) 绑定自定义事件时回调函数要么配置在methods中要么用箭头函数 否则this指向会出问题 src/App.vue
templatediv classapph1{{msg}}学生姓名是:{{studentName}}/h1!-- 通过父组件给子组件传递函数类型的props实现子给父传递数据 --School :getSchoolNamegetSchoolName/!-- 通过父组件给子组件绑定一个自定义事件实现子给父传递数据第一种写法使用或v-on --!-- Student atguigugetStudentName demom1/ --!-- 通过父组件给子组件绑定一个自定义事件实现子给父传递数据第二种写法使用ref --Student refstudent click.nativeshow//div
/templatescriptimport Student from ./components/Studentimport School from ./components/Schoolexport default {name:App,components:{School,Student},data() {return {msg:你好啊,studentName:}},methods: {getSchoolName(name){console.log(App收到了学校名,name)},getStudentName(name,...params){console.log(App收到了学生名,name,params)this.studentName name},m1(){console.log(demo事件被触发了)},show(){alert(123)}},mounted() {//绑定自定义事件this.$refs.student.$on(atguigu,this.getStudentName)//绑定自定义事件一次性this.$refs.student.$once(atguigu,this.getStudentName) },}
/scriptstyle scoped//样式.app{background-color: gray;padding: 5px;}
/stylesrc/components/Student.vue
templatediv classstudenth2学生姓名{{name}}/h2h2学生性别{{sex}}/h2h2当前求和为{{number}}/h2button clickadd点我number/buttonbutton clicksendStudentlName把学生名给App/buttonbutton clickunbind解绑atguigu事件/buttonbutton clickdeath销毁当前Student组件的实例(vc)/button/div
/templatescriptexport default {name:Student,data() {return {name:张三,sex:男,number:0}},methods: {add(){console.log(add回调被调用了)this.number},sendStudentlName(){//触发Student组件实例身上的atguigu事件this.$emit(atguigu,this.name,666,888,900)// this.$emit(demo)// this.$emit(click)},unbind(){this.$off(atguigu) //解绑一个自定义事件// this.$off([atguigu,demo]) //解绑多个自定义事件// this.$off() //解绑所有的自定义事件},death(){this.$destroy() //销毁了当前Student组件的实例销毁后所有Student实例的自定义事件全都不奏效。}},}
/scriptstyle langless scoped.student{background-color: pink;padding: 5px;margin-top: 30px;}
/stylesrc/components/School.vue
templatediv classschoolh2学校名称{{name}}/h2h2学校地址{{address}}/h2button clicksendSchoolName把学校名给App/button/div
/templatescriptexport default {name:School,props:[getSchoolName],data() {return {name:尚硅谷,address:北京,}},methods: {sendSchoolName(){this.getSchoolName(this.name)}},}
/scriptstyle scoped.school{background-color: skyblue;padding: 5px;}
/style使用自定义事件优化Todo-List
src/App.vue
templatediv idrootdiv classtodo-containerdiv classtodo-wrapMyHeader addTodoaddTodo/MyList :todostodos :checkTodocheckTodo :deleteTododeleteTodo/MyFooter :todostodos checkAllTodocheckAllTodo clearAllTodoclearAllTodo//div/div/div
/templatescriptimport MyHeader from ./components/MyHeaderimport MyList from ./components/MyListimport MyFooter from ./components/MyFooter.vueexport default {name:App,components:{MyHeader,MyList,MyFooter},data() {return {//由于todos是MyHeader组件和MyFooter组件都在使用所以放在App中状态提升todos:JSON.parse(localStorage.getItem(todos)) || []}},methods: {//添加一个todoaddTodo(todoObj){this.todos.unshift(todoObj)},//勾选or取消勾选一个todocheckTodo(id){this.todos.forEach((todo){if(todo.id id) todo.done !todo.done})},//删除一个tododeleteTodo(id){this.todos this.todos.filter( todo todo.id ! id )},//全选or取消全选checkAllTodo(done){this.todos.forEach((todo){todo.done done})},//清除所有已经完成的todoclearAllTodo(){this.todos this.todos.filter((todo){return !todo.done})}},watch: {todos:{deep:true,handler(value){localStorage.setItem(todos,JSON.stringify(value))}}},}
/scriptstyle/*base*/body {background: #fff;}.btn {display: inline-block;padding: 4px 12px;margin-bottom: 0;font-size: 14px;line-height: 20px;text-align: center;vertical-align: middle;cursor: pointer;box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);border-radius: 4px;}.btn-danger {color: #fff;background-color: #da4f49;border: 1px solid #bd362f;}.btn-danger:hover {color: #fff;background-color: #bd362f;}.btn:focus {outline: none;}.todo-container {width: 600px;margin: 0 auto;}.todo-container .todo-wrap {padding: 10px;border: 1px solid #ddd;border-radius: 5px;}
/stylesrc/components/MyHeader.vue
templatediv classtodo-headerinput typetext placeholder请输入你的任务名称按回车键确认 v-modeltitle keyup.enteradd//div
/templatescriptimport {nanoid} from nanoidexport default {name:MyHeader,data() {return {//收集用户输入的titletitle:}},methods: {add(){//校验数据if(!this.title.trim()) return alert(输入不能为空)//将用户的输入包装成一个todo对象const todoObj {id:nanoid(),title:this.title,done:false}//通知App组件去添加一个todo对象this.$emit(addTodo,todoObj,1,2,3)//清空输入this.title }},}
/scriptstyle scoped/*header*/.todo-header input {width: 560px;height: 28px;font-size: 14px;border: 1px solid #ccc;border-radius: 4px;padding: 4px 7px;}.todo-header input:focus {outline: none;border-color: rgba(82, 168, 236, 0.8);box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);}
/stylesrc/components/MyFooter.vue
templatediv classtodo-footer v-showtotallabel!-- input typecheckbox :checkedisAll changecheckAll/ --input typecheckbox v-modelisAll//labelspanspan已完成{{doneTotal}}/span / 全部{{total}}/spanbutton classbtn btn-danger clickclearAll清除已完成任务/button/div
/templatescriptexport default {name:MyFooter,props:[todos],computed: {//总数total(){return this.todos.length},//已完成数doneTotal(){//此处使用reduce方法做条件统计/* const x this.todos.reduce((pre,current){console.log(,pre,current)return pre (current.done ? 1 : 0)},0) *///简写return this.todos.reduce((pre,todo) pre (todo.done ? 1 : 0) ,0)},//控制全选框isAll:{//全选框是否勾选get(){return this.doneTotal this.total this.total 0},//isAll被修改时set被调用set(value){// this.checkAllTodo(value)this.$emit(checkAllTodo,value)}}},methods: {/* checkAll(e){this.checkAllTodo(e.target.checked)} *///清空所有已完成clearAll(){// this.clearAllTodo()this.$emit(clearAllTodo)}},}
/scriptstyle scoped/*footer*/.todo-footer {height: 40px;line-height: 40px;padding-left: 6px;margin-top: 5px;}.todo-footer label {display: inline-block;margin-right: 20px;cursor: pointer;}.todo-footer label input {position: relative;top: -1px;vertical-align: middle;margin-right: 5px;}.todo-footer button {float: right;margin-top: 5px;}
/style使用自定义事件优化Todo-List
src/App.vue
templatediv idrootdiv classtodo-containerdiv classtodo-wrapMyHeader addTodoaddTodo/MyList :todostodos :checkTodocheckTodo :deleteTododeleteTodo/MyFooter :todostodos checkAllTodocheckAllTodo clearAllTodoclearAllTodo//div/div/div
/templatescriptimport MyHeader from ./components/MyHeaderimport MyList from ./components/MyListimport MyFooter from ./components/MyFooter.vueexport default {name:App,components:{MyHeader,MyList,MyFooter},data() {return {//由于todos是MyHeader组件和MyFooter组件都在使用所以放在App中状态提升todos:JSON.parse(localStorage.getItem(todos)) || []}},methods: {//添加一个todoaddTodo(todoObj){this.todos.unshift(todoObj)},//勾选or取消勾选一个todocheckTodo(id){this.todos.forEach((todo){if(todo.id id) todo.done !todo.done})},//删除一个tododeleteTodo(id){this.todos this.todos.filter( todo todo.id ! id )},//全选or取消全选checkAllTodo(done){this.todos.forEach((todo){todo.done done})},//清除所有已经完成的todoclearAllTodo(){this.todos this.todos.filter((todo){return !todo.done})}},watch: {todos:{deep:true,handler(value){localStorage.setItem(todos,JSON.stringify(value))}}},}
/scriptstyle/*base*/body {background: #fff;}.btn {display: inline-block;padding: 4px 12px;margin-bottom: 0;font-size: 14px;line-height: 20px;text-align: center;vertical-align: middle;cursor: pointer;box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);border-radius: 4px;}.btn-danger {color: #fff;background-color: #da4f49;border: 1px solid #bd362f;}.btn-danger:hover {color: #fff;background-color: #bd362f;}.btn:focus {outline: none;}.todo-container {width: 600px;margin: 0 auto;}.todo-container .todo-wrap {padding: 10px;border: 1px solid #ddd;border-radius: 5px;}
/stylesrc/components/MyHeader.vue
templatediv classtodo-headerinput typetext placeholder请输入你的任务名称按回车键确认 v-modeltitle keyup.enteradd//div
/templatescriptimport {nanoid} from nanoidexport default {name:MyHeader,data() {return {//收集用户输入的titletitle:}},methods: {add(){//校验数据if(!this.title.trim()) return alert(输入不能为空)//将用户的输入包装成一个todo对象const todoObj {id:nanoid(),title:this.title,done:false}//通知App组件去添加一个todo对象this.$emit(addTodo,todoObj,1,2,3)//清空输入this.title }},}
/scriptstyle scoped/*header*/.todo-header input {width: 560px;height: 28px;font-size: 14px;border: 1px solid #ccc;border-radius: 4px;padding: 4px 7px;}.todo-header input:focus {outline: none;border-color: rgba(82, 168, 236, 0.8);box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);}
/stylesrc/components/MyFootertemplatediv classtodo-footer v-showtotallabel!-- input typecheckbox :checkedisAll changecheckAll/ --input typecheckbox v-modelisAll//labelspanspan已完成{{doneTotal}}/span / 全部{{total}}/spanbutton classbtn btn-danger clickclearAll清除已完成任务/button/div
/templatescriptexport default {name:MyFooter,props:[todos],computed: {//总数total(){return this.todos.length},//已完成数doneTotal(){//此处使用reduce方法做条件统计/* const x this.todos.reduce((pre,current){console.log(,pre,current)return pre (current.done ? 1 : 0)},0) *///简写return this.todos.reduce((pre,todo) pre (todo.done ? 1 : 0) ,0)},//控制全选框isAll:{//全选框是否勾选get(){return this.doneTotal this.total this.total 0},//isAll被修改时set被调用set(value){// this.checkAllTodo(value)this.$emit(checkAllTodo,value)}}},methods: {/* checkAll(e){this.checkAllTodo(e.target.checked)} *///清空所有已完成clearAll(){// this.clearAllTodo()this.$emit(clearAllTodo)}},}
/scriptstyle scoped/*footer*/.todo-footer {height: 40px;line-height: 40px;padding-left: 6px;margin-top: 5px;}.todo-footer label {display: inline-block;margin-right: 20px;cursor: pointer;}.todo-footer label input {position: relative;top: -1px;vertical-align: middle;margin-right: 5px;}.todo-footer button {float: right;margin-top: 5px;}
/style
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/915190.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!