Vue this.$emit 自定义事件分发
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <div id="app"> <todo> <todo-title slot="todo-title" :title="title"></todo-title> <todo-items slot="todo-items" v-for="(item,index) in todoItems" :item="item" v-bind:index="index" v-on:remove="removeItems(index)" :key="index"></todo-items> </todo> </div> <!--引入vue.js 包--> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> <script> //slot 插槽 Vue.component("todo",{ template: '<div>\ <slot name="todo-title"></slot>\ <ul>\ <slot name="todo-items"></slot>\ </ul>\ </div>' }); Vue.component("todo-title",{ props:['title'], template:'<div>{{title}}</div>' }); Vue.component("todo-items",{ props:['item','index'], //只能绑定当前组件的方法 template:'<li>{{index}}----{{item}} <button @click="remove">delete</button> </li>', methods:{ remove:function (index) { //this.$emit 自定义事件分发 this.$emit('remove',index); } } }) var vm=new Vue({ el:"#app", data:{ title:"创客未来", todoItems:['创客','未来','中国'] }, methods:{ removeItems:function (index) { this.todoItems.splice(index,1);//一次删除一个数组元素 } } }); </script> </body> </html>
Vue的核心:数据驱动、组件化。
Vue的优点:借鉴了AngulaJS的模块化和React的虚拟DOM,虚拟DOM就是把DOM操作放到内存中执行。
Vue常用属性:
v-if
v-else-if
v-else
v-for
v-on 绑定事件,简写 @
v-model 数据双向绑定
v-bind 给组件绑定参数,简写 :
Vue 组件化
组合插件 slot插槽
组件内部绑定事件需要使用到 this.$emit("事件名",参数);
计算属性的特色,缓存计算数据
遵循SoC关注分离原则,Vue是纯粹的视图框架,并不包含,比如Ajax之类的通信功能,为了解决通信问题,需要使用Axios框架做异步通信;
注意:
Vue的开发都是要基于NodeJS,实际开发采用 vue-cli 脚手架开发,vue-router路由,vuex做状态管理,Vue UI,界面我们一般使用 ElementUI(饿了么出品),或者ICE(阿里巴巴)来快速搭建前端项目。
官网:
https://element.eleme.cn/#/zh-CN
https://ice.work/
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· SQL Server 2025 AI相关能力初探
· AI编程工具终极对决:字节Trae VS Cursor,谁才是开发者新宠?
· 展开说说关于C#中ORM框架的用法!