版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- GB/T 46373-2025压缩空气储能电站接入电网技术规定
- GB/Z 18978.810-2025人-系统交互工效学第810部分:机器人、智能和自主系统
- GB/Z 145-2025标准化教育课程建设指南物流标准化
- 2026年广西水利电力职业技术学院单招职业倾向性测试题库及完整答案详解1套
- 2026年山西国际商务职业学院单招职业倾向性考试题库及答案详解1套
- 2026年柳州城市职业学院单招职业适应性测试题库及参考答案详解
- 2026年河北软件职业技术学院单招职业倾向性测试题库及参考答案详解1套
- 2026年湖北职业技术学院单招职业技能考试题库及参考答案详解1套
- 2026年巴中职业技术学院单招职业倾向性考试题库带答案详解
- 2026年郑州亚欧交通职业学院中单招职业适应性考试题库含答案详解
- 《CRTAS-2024-06 互联网租赁自行车停放区设置指南》
- 2025年北京工程造价定额与计价规范解析
- 林业和草原局护林员招聘考试《森林资源管护》题库(答案+解析)
- 中华人民共和国职业分类大典是(专业职业分类明细)
- 电子票据管理办法医院
- 云南省曲靖市麒麟区2023年小升初数学试卷
- 电子承兑支付管理办法
- 安徽旧锅炉拆除合同范本
- 学堂在线 雨课堂 学堂云 知识产权法 章节测试答案
- 全检员考试试题及答案
- 医院搬迁整体方案
评论
0/150
提交评论