



版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、组件化常用技术组件传值、通信父组件 => 子组件: 属性props/ childprops: msg: String / parent<HelloWorld msg="Welcome to Your Vue.js App"/>refs/ parent<HelloWorld ref="hw"/>this.$refs.hw.xx子组件 => 父组件:自定义/ child this.$emit('add', good)/ parent<Cart add="cartAdd($event)&quo
2、t;></Cart>兄弟组件:通过共同祖辈组件通过共同的祖辈组件搭桥,$parent或$root。/ brother1 this.$parent.$on('foo', handle)/ brother2 this.$parent.$emit('foo')祖先和后代之间provide/inject:能够实现祖先给后代传值开课吧web全栈架构师/ ancestor provide() return foo: 'foo'/ descendant inject: 'foo'dispatch:后代给祖先传值/ 定义一个di
3、spatch方法,指定要派发 名称和数据function dispatch(eventName, data) let parent = this.$parent/ 只要还存在父元素就继续往上查找while (parent) / 父元素用$emit触发parent.$emit(eventName,data)/ 递归查找父元素parent = parent.$parent/ 使用,HelloWorld.vue<h1 click="dispatch('hello', 'hello,world')"> msg </h1>/ A
4、pp.vuethis.$on('hello', this.sayHello)任意两个组件之间: 总线 或 vuex总线:创建一个Bus类负责 派发、 和回调管理/ Bus: 派发、 和回调管理class Bus constructor()/ / eventName1:fn1,fn2,/ eventName2:fn3,fn4,/ this.callbacks = $on(name, fn)this.callbacksname = this.callbacksname | this.callbacksname.push(fn)$emit(name, args) if(this.ca
5、llbacksname)this.callbacksname.forEach(cb => cb(args)开课吧web全栈架构师/ main.jsVtotype.$bus = new Bus()/ child1 this.$bus.$on('foo', handle)/ child2 this.$bus.$emit('foo')vuex:创建唯一的全局数据管理者store,通过它管理数据并通知组件状态变更插槽Vue 2.6.0之后采用全新v-slot语法取代之前的slot、slot-scope插槽/ comp1<div><sl
6、ot></slot></div>/ parent<comp>hello</comp>具名插槽/ comp2<div><slot></slot><slot name="content"></slot></div>/ parent<Comp2><!- 默认插槽用default做参数 -><template v-slot:default>具名插槽</template><!- 具名插槽用插槽名做参数 -&
7、gt;<template v-slot:content>内容.</template></Comp2>作用域插槽开课吧web全栈架构师/ comp3<div><slot :foo="foo"></slot></div>/ parent<Comp3><!- 把v-slot的值指定为作用域上下文对象 -><template v-slot:default="ctx">来自子组件数据:ctx.foo</template></Co
8、mp3>#表单组件实现Input双向绑定:input、:value 派发校验<template><div><input :value="value" input="onInput" v-bind="$attrs"></div></template><script>export default inheritAttrs: false, props: value: type: String, default: "",methods: onIn
9、put(e) this.$emit("input", e.target.value);this.$parent.$emit('validate');</script>FormItem给Input预留插槽 - slot能够展示label和校验信息开课吧web全栈架构师能够进行校验<template><div><label v-if="label">label</label><slot></slot><p v-if="errorMessage&
10、quot;>errorMessage</p></div></template><script>import Schema from 'async-validator' export default inject: "form", props: label: type: String, default: "",prop: type: String,data() return errorMessage: "",mounted() this.$on('valida
11、te', this.validate),methods: validate() / 做校验const value = this.form.m p const rules = p/ npm i async-validator -Sconst desc = p: rules; const schema = new Schema(desc);/ return的是校验结果的Promisereturn schema.validate(p: value, errors => if (errors)
12、this.errorMessage = errors0.message;else this.errorMessage = ''),;</script>开课吧web全栈架构师Form给FormItem留插槽设置数据和校验规则全局校验<template><div><slot></slot></div></template><script>export default provide() return form: this;,props: m : type: Object, require
13、d: true,rules: type: Object,methods: validate(cb) const tasks = this.$children.filter(item => p).map(item => item.validate();/ 所有任务都通过才算校验通过Promise.all(tasks).then() => cb(true).catch() => cb(false);</script>Notice组件实现组件实例创建函数:create函数import Vue from 'vue'export def
14、ault function create(Component, props) / 先创建实例开课吧web全栈架构师Notice组件插槽预留标题、内容等属性自动关闭开课吧web全栈架构师<template><div class="box" v-if="isShow"><h3>title</h3><p class="box-content">message</p></div></template><script>export def
15、ault props: title: type: String, default: "",message: type: String, default: "",duration: type: Number, default: 1000,data() return isShow: false;const vm = new Vue( render(h) / h就是createElement,它返回VNodereturn h(Component, props).$mount();/ 手动挂载document.body.appendChild(vm.$el);/
16、 销毁方法const comp = vm.$children0; comp.remove = function() document.body.removeChild(vm.$el); vm.$destroy();return comp;使用<script>import Notice from "/components/notice/KNotice"export default methods: submitForm(form) this.$refsform.validate(valid => const notice = this.$create(Not
17、ice, title: " 你杨哥喊你来搬砖",message: valid ? "请求登录!" : "校验失败!",duration: 1000);notice.show(););</script>Tree组件实现递归组件Item创建开课吧web全栈架构师<template><li><div click="toggle">m .title<span v-if="isFolder">open ? '-' : '+
18、'</span></div><ul v-show="open" v-if="isFolder"><item class="item" v-for="m in m .children" :m ="m ":key="m .title"></item></ul>,methods: show() this.isShow = true; setTimeout(this.hide, this.duration
19、);,hide() this.isShow = false; this.remove();</script>数据和使用开课吧web全栈架构师<template><div><ul><item class="item" :m ="treeData"></item></ul></div></template><script>import Item from "./Item" export default name: "app",data() return treeData: title: "Web全栈架构师", children: </li></template><script>export default name: "Item", p
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中学2025年寒假工作计划
- 国家健康教育特色学校计划
- 放化疗护理常规
- 编译打包部署培训
- 左侧腹股沟肿物鉴别诊断
- 小脑肿瘤的临床诊疗策略
- 肾脏内科业务查房
- 公交安全生产月
- 【酒泉】2025年甘肃酒泉市瓜州县事业单位招聘21人笔试历年典型考题及考点剖析附带答案详解
- 幼儿教学多媒体课件
- 弱电工程项目经理职责
- 双碳知识培训
- 金融科技风险管理
- 大部分分校:地域文化形考任务一-国开(CQ)-国开期末复习资料
- 2025版国家开放大学法律事务专科《民法学(1)》期末考试总题库
- 2024年度软件开发合同功能需求规格说明书2篇
- GB/T 2982-2024工业车辆充气轮胎规格、尺寸、气压与负荷
- 医疗保险基金使用监督管理条例
- 三家比价合同范例
- 《义务教育语文课程标准》(2022年版)
- 项目驻地安全防火培训
评论
0/150
提交评论