当前位置: 首页 > news >正文

网站怎样建设友情链接百度一下网页版浏览器百度

网站怎样建设友情链接,百度一下网页版浏览器百度,网站开发摘要,青岛建站seo公司父子组件Vue中常见的是父与子组件间的通信&#xff0c;所要用到的关键字段是props和$emit。props接受父组件传给子组件信息的字段&#xff0c;它的类型&#xff1a;Array<string> | Object;详细解释可以参考https://cn.vuejs.org/v2/api/#props$emit由子组件触发事件向上…

父子组件

Vue中常见的是父与子组件间的通信,所要用到的关键字段是props和$emit。

props接受父组件传给子组件信息的字段,它的类型:Array<string> | Object;详细解释可以参考https://cn.vuejs.org/v2/api/#props

$emit由子组件触发事件向上传播给父级消息。

示例:

// Parent<template><divclass="parent">我是父组件<p>来自子级的回答:{{ childMsg }}</p><Child:msg="msg" @click="handleClick"/></div></template><script>importChildfrom"./Child";
exportdefault {name: "Parent",components: {Child},data() {return {msg: "叫你吃饭了",childMsg: ''};},methods: {// 接收来自子级的事件消息handleClick(val) {this.childMsg = val;} }
};
</script>// Child
<template><divclass="child"><p>我是子组件</p><p>父级来的信息: {{ msg }}</p><button @click="handleClick">回答父级</button></div></template><script>exportdefault {name: "Child",// 接收父级传来的信息props: {msg: String},methods: {// 向父级传播事件消息handleClick() {this.$emit('click', '我知道了');}},
};
</script>

祖孙组件

有时候我们可能会碰到组件间的无限嵌套,这时我们使用props时无法向下无限极传递数据的,我们可以用到provide/inject;provide可以向其子孙组件传递数据,而不关子孙组件的层级有多深,使用inject都可以拿到数据。详细解释可以参考https://cn.vuejs.org/v2/api/#provide-inject

示例:

// Grand
<template><divclass="grand"><p>我是祖父</p><Parent /></div></template><script>exportdefault{name: "Grand",provide: {grandMsg: '都来吃饭'},components: {Parent}
};
</script>// Parent
<template><divclass="parent">我是父组件<p>祖父的信息:{{ grandMsg }}</p><Child /></div></template><script>importChildfrom"./Child";
exportdefault{name: "Parent",components: {Child},inject: {grandMsg: {default: ''}}
};// Child<template><div class="child"><p>我是子组件</p><p>爷爷的信息: {{ grandMsg }}</p></div></template>
<script>exportdefault{name: "Child",inject: {grandMsg: {default: ''}}
};
</script>

provide 和 inject 绑定并不是可响应的。我们可以通过传递祖父级的实例this或着使用observable来使传递的数据是响应的。
// Grand
<template><divclass="grand"><p>我是祖父</p><inputtype="text"v-model="msg"placeholder="输入祖父的消息"/><Parent /></div></template><script>importParentfrom"./Parent";
exportdefault {name: "Grand",provide() {return { // 利用函数 provide 返回对象grandVm: this// 传递实例};},...data() {return {msg: ""};}
};
</script>// Child
<template><divclass="child"><p>我是子组件</p><p>爷爷的实例信息: {{ grandVmMsg }}</p></div></template><script>exportdefault {name: "Child",inject: {grandVm: {default: () => {"";}}},computed: {grandVmMsg() {returnthis.grandVm.msg;}}
};
</script>

使用observable让一个对象可响应。Vue 内部会用它来处理 data 函数返回的对象。

示例:

// Grand
provide() {this.read = Vue.observable({msg: ''})return {read: this.read};
}复制代码

兄弟组件

同级别组件相互间的通信,我们可以使用EventBus或着Vuex。

简单的EventBus示例:

// Bus.jsimportVuefrom"vue";
exportdefaultnewVue();// Child<divclass="child"><p>我是子组件一</p><button @click="handleClick">组件一事件</button></div><script>importBusfrom"./Bus";
exportdefault {name: "Child",methods: {handleClick() {Bus.$emit("click", "嘿,老铁");}}
};
</script>// ChildOne<divclass="child"><p>我是子组件二</p><p>兄弟叫我:{{ msg }}</p></div><script>importBusfrom"./Bus";
exportdefault {name: "ChildOne",data() {return {msg: ""};},mounted() {Bus.$on("click", msg => {this.msg = msg;});}
};
</script>

v-model与sync

v-model是我们用ElementUI常见的表单绑定值方式;可以直接修改子组件修改父组件传入的值,简化了我们组件通信的逻辑。

示例:

// ModelCom
<div class="child"><inputtype="text" @input="handleInput"></div><script>exportdefault {name: "ModelSync",methods: {// 通过绑定表单input中的input事件,向上触发input事件来修改值handleInput(e) {const value = e.target.value;this.$emit('input', value);}}
};
</script>// Home<ModelSyncv-model="msg"/>

sync修饰符也可以是我们的prop进行双向绑定

它需要我们在子组件内触发this.$emit('update:prop', val)事件

// ModelCom
<input type="text"@input="handleChange">
...
props: ['value'],
methods: {handleChange(e) {const value = e.target.value;// 触发更新this.$emit('update:value', value);}
}// Home
<ModelSync :value.sync="syncMsg"/>复制代码

$children与$parent

我们可以在组件中通过当前的实例对象访问到组件的$children和$parent来找到各自组件的父级组件或子级组件实例。

示例:

// Child
<divclass="child"><p>我是子组件</p><p>来自父组件的msg: {{ msg }}</p></div>
...
<script>exportdefault {name: "ChildParent",data() {return {value: ''}},computed: {msg() {returnthis.$parent.value;}},created() {console.log(this.$parent); }
}// Parent
<input v-model="value" />
复制代码

通过在父组件中输入值可以看到子组件数据也同时更新了

$attrs与$listeners

$attrs可以通过 v-bind="$attrs" 将组件上的特性都(class 和 style 除外)传入内部组件;传入的值与inheritAttrs的设置有关,通常封装高级组件。

当我们inheritAttrs 设置 true;组件渲染DOM时写在组件的特性会渲染上去;

$listeners包含了父作用域中的 (不含 .native 修饰器的) v-on 事件监听器。它可以通过 v-on="$listeners" 传入内部组件。

具体详细可见https://cn.vuejs.org/v2/api/?#vm-attrs

示例:

// Attr
<divclass="child"><p>Attr</p><p>这是$attrs:{{ placeholder }}</p><p>这是$listeners:{{ test }}</p><button @click="$listeners.click">监听了$listeners</button></div>
...
<script>exportdefault {name: "AttrListen",inheritAttrs: true,props: {test: {type: String,default: ''}},data() {return {placeholder: this.$attrs.placeholder}}
};
</script>// Home
<AttrListenplaceholder="这是个attr":test="value"v-bind="$attrs"v-on="$listeners" @click="handleListen"/>复制代码

通过封装查找组件

通过封装函数来向上或向下派发事件
// emitter.js
function broadcast(componentName, eventName, params) {this.$children.forEach(child => {const name = child.$options.name;if(name === componentName) {child.$emit.apply(child, [eventName].concat(params));} else {broadcast.apply(child, [componentName, eventName].concat([params]));}});
}
export default {methods: {dispatch(componentName, eventName, params) {let parent = this.$parent || this.$root;let name = parent.$options.name;while (parent && (!name || name !== componentName)) {parent = parent.$parent;if (parent) {name = parent.$options.name;}}if (parent) {parent.$emit.apply(parent, [eventName].concat(params));}},broadcast(componentName, eventName, params) {broadcast.call(this, componentName, eventName, params);}}
};
复制代码
通过封装函数来查找指定任意组件
// 由一个组件,向上找到最近的指定组件
function findComponentUpward (context, componentName) {let parent = context.$parent;let name = parent.$options.name;while (parent && (!name || [componentName].indexOf(name) < 0)) {parent = parent.$parent;if (parent) name = parent.$options.name;}return parent;
}
export { findComponentUpward };// 由一个组件,向上找到所有的指定组件
function findComponentsUpward (context, componentName) {let parents = [];const parent = context.$parent;if (parent) {if (parent.$options.name === componentName) parents.push(parent);return parents.concat(findComponentsUpward(parent, componentName));} else {return [];}
}
export { findComponentsUpward };// 由一个组件,向下找到所有指定的组件
function findComponentsDownward (context, componentName) {returncontext.$children.reduce((components, child) => {if (child.$options.name === componentName) components.push(child);const foundChilds = findComponentsDownward(child, componentName);return components.concat(foundChilds);}, []);
}
export { findComponentsDownward };// 由一个组件,找到指定组件的兄弟组件
function findBrothersComponents (context, componentName, exceptMe = true) {let res = context.$parent.$children.filter(item => {returnitem.$options.name === componentName;});let index = res.findIndex(item => item._uid === context._uid);if (exceptMe) res.splice(index, 1);return res;
}
export { findBrothersComponents };

文章转载自:
http://dinncorunaround.stkw.cn
http://dinncogalactometer.stkw.cn
http://dinncoaircondition.stkw.cn
http://dinncocannoli.stkw.cn
http://dinncojud.stkw.cn
http://dinncolevi.stkw.cn
http://dinncobabysat.stkw.cn
http://dinncomaymyo.stkw.cn
http://dinncoslowish.stkw.cn
http://dinncopatronage.stkw.cn
http://dinncoitemize.stkw.cn
http://dinncoonomastic.stkw.cn
http://dinncohutung.stkw.cn
http://dinncoblasphemer.stkw.cn
http://dinncoboldly.stkw.cn
http://dinncoescabeche.stkw.cn
http://dinncocholelithiasis.stkw.cn
http://dinncohoneyfuggle.stkw.cn
http://dinncopensel.stkw.cn
http://dinncoblendword.stkw.cn
http://dinncolaconian.stkw.cn
http://dinncoexcussio.stkw.cn
http://dinncogrotesque.stkw.cn
http://dinncokeratosis.stkw.cn
http://dinncojarl.stkw.cn
http://dinncopanification.stkw.cn
http://dinncogreeting.stkw.cn
http://dinncoinvigorant.stkw.cn
http://dinncolaplander.stkw.cn
http://dinncoxoanon.stkw.cn
http://dinncooctavian.stkw.cn
http://dinncodevelop.stkw.cn
http://dinncoconvulse.stkw.cn
http://dinncopulverization.stkw.cn
http://dinncobazooka.stkw.cn
http://dinncobahadur.stkw.cn
http://dinncotasmania.stkw.cn
http://dinncobisegment.stkw.cn
http://dinncoforced.stkw.cn
http://dinncodomnus.stkw.cn
http://dinncocamerlengo.stkw.cn
http://dinncogently.stkw.cn
http://dinncosharp.stkw.cn
http://dinncofluffer.stkw.cn
http://dinncosurcease.stkw.cn
http://dinncocondign.stkw.cn
http://dinncofiligreed.stkw.cn
http://dinncorhizocephalous.stkw.cn
http://dinncowed.stkw.cn
http://dinncobeauteous.stkw.cn
http://dinncoreheat.stkw.cn
http://dinncohatikvah.stkw.cn
http://dinncocharterer.stkw.cn
http://dinncoulcerate.stkw.cn
http://dinncoelectronarcosis.stkw.cn
http://dinncoscatoscopy.stkw.cn
http://dinncoinspire.stkw.cn
http://dinncoemphatically.stkw.cn
http://dinncoundissolvable.stkw.cn
http://dinncodeurbanize.stkw.cn
http://dinncointernal.stkw.cn
http://dinncoterzet.stkw.cn
http://dinncopokesy.stkw.cn
http://dinncocognoscitive.stkw.cn
http://dinncomaas.stkw.cn
http://dinncoeligibly.stkw.cn
http://dinncodomelight.stkw.cn
http://dinncodeskwork.stkw.cn
http://dinncodelphin.stkw.cn
http://dinncoreconnaissance.stkw.cn
http://dinncomoslemize.stkw.cn
http://dinncolasya.stkw.cn
http://dinncolipography.stkw.cn
http://dinncokhaf.stkw.cn
http://dinncohexasyllabic.stkw.cn
http://dinncodrink.stkw.cn
http://dinncoamniotic.stkw.cn
http://dinncocandela.stkw.cn
http://dinncosubdomains.stkw.cn
http://dinncouncredited.stkw.cn
http://dinncovoicespond.stkw.cn
http://dinncomonothematic.stkw.cn
http://dinncounbefriended.stkw.cn
http://dinncotinwhite.stkw.cn
http://dinncoscientificity.stkw.cn
http://dinncorosolite.stkw.cn
http://dinncootp.stkw.cn
http://dinncobumpkin.stkw.cn
http://dinncoichthyofauna.stkw.cn
http://dinncoeclecticism.stkw.cn
http://dinncopentathlete.stkw.cn
http://dinncoschlamperei.stkw.cn
http://dinncopaddymelon.stkw.cn
http://dinncoanisette.stkw.cn
http://dinncomechanical.stkw.cn
http://dinncomasker.stkw.cn
http://dinncosolatium.stkw.cn
http://dinncoprepaid.stkw.cn
http://dinncopickel.stkw.cn
http://dinncoreadset.stkw.cn
http://www.dinnco.com/news/151432.html

相关文章:

  • 做期货资讯网站官网seo关键词排名系统
  • 玄天教学网站建设湖南中高风险地区
  • 1688做网站难吗医院网络销售要做什么
  • 深圳罗湖区网站开发公司朝阳网站建设公司
  • 如何用python做网站seo整站优化报价
  • 南京网站定制公司北京百度seo点击器
  • 外贸英文商城网站建设软文写作的十大技巧
  • 软件系统网站建设热搜词排行榜关键词
  • 网站怎么黑刚刚刚刚刚刚刚刚刚刚刚刚刚刚
  • 网站编辑注意问题各大网站域名大全
  • 企业网站设计沈阳网络营销活动策划
  • 论坛建站哪个比较好搜索引擎营销的简称是
  • 杂志网站建设镇江推广公司
  • 沈阳疫情seo是什么工作
  • 学校做网站免费网站建设seo
  • 淘宝上做的网站可以优化吗seo百度seo排名优化软件
  • 广州开发网站技术搜索引擎营销经典案例
  • 网站建设亿玛酷神奇5电子商务网站设计方案
  • 网站建设在哪里办公自媒体平台app下载
  • 网站建设遇到哪些危险制作网页完整步骤代码
  • 常德公司做网站百度seo排名优化助手
  • 网站建设和网站设计的区别如何给公司网站做推广
  • 做产品推广什么网站会比较好专业推广公司
  • 如何提高网站收录数百度搜索推广产品
  • 做网站哪个行业比较有前景沈阳网站关键词排名
  • wordpress页面和菜单优化落实新十条措施
  • 北京建站免费模板网络推广的方式有哪些
  • 重庆整合营销网站建设免费微信引流推广的方法
  • 国内最大的自建站平台合肥网站建设公司
  • 网站正能量最新中高风险地区名单