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

丹阳企业网站wordpress小程序编辑

丹阳企业网站,wordpress小程序编辑,手机h5制作小程序,如何做网站的悬浮窗口今天产品经理要求做保留某组件全部功能,还要在它的基础上增加东西。如果不嫌麻烦的话就笨办法,但是想一下怎么只用少量代码高效的二次封装组件呢 Vue中的$attrs 在 Vue2 中,attr 是指组件接收的 HTML 特性(attribute),通过 prop…

今天产品经理要求做保留某组件全部功能,还要在它的基础上增加东西。如果不嫌麻烦的话就笨办法,但是想一下怎么只用少量代码高效的二次封装组件呢

Vue中的$attrs

在 Vue2 中,attr 是指组件接收的 HTML 特性(attribute),通过 props 的方式传递给子组件。
而在 Vue3 中,attr 的概念被引入了 Composition API 中,并且被用于管理各种配置项。

Vue2 中使用 attr

1、使用 v-bind指令绑定 HTML 属性
在 Vue2 中,如果想将父组件传递的 HTML 属性传递给子组件进行使用,可以在子组件中通过 props 接收参数,并使用 v-bind 指令将其绑定到子组件的 HTML 元素上。例如:

<template><div class="demo-component" :style="styleObject">{{ message }}</div>
</template><script>
export default {name: "DemoComponent",props: {message: String,styleObject: Object,},
};
</script>

在父组件中使用该组件时,可以通过 v-bind 指令将 HTML 特性传递给子组件:

<template><demo-component message="Hello, world!" :style-object="{ color: 'red' }"></demo-component>
</template>

2、使用 $attrs 对象传递所有未被 props 所接收的特性
在 Vue2 中,可以通过 $attrs 对象获取父组件传递给子组件但未被 props 所接收的特性,从而实现组件复用和扩展的目的。例如:

<template><div class="demo-component" :style="styleObject" v-bind="$attrs">{{ message }}</div>
</template><script>
export default {name: "DemoComponent",props: {message: String,styleObject: Object,},
};
</script>

在父组件使用该组件时,可以像平常传递普通的 HTML 特性一样,同时还可以传递一些自定义的特性:

<template><demo-componentmessage="Hello, world!":style-object="{ color: 'red' }"custom-attribute="something"></demo-component>
</template>

在子组件中,可以通过 this.$attrs 属性获取父组件传递给子组件但未被 props 所接收的特性:

console.log(this.$attrs.customAttribute); // 输出:something

Vue3 中使用 attr

在 Vue3 中,可以通过 setup 函数中的第二个参数 context 来访问该组件的配置选项,其中包括了所有未被 props 所接收的特性:

<template><div class="demo-component" :style="styleObject" v-bind="$attrs">{{ message }}</div>
</template><script>
export default {name: "DemoComponent",props: {message: String,styleObject: Object,},setup(props, context) {console.log(context.attrs.customAttribute); // 输出:something},
};
</script>

在 setup 函数中,通过 context.attrs 获取父组件传递给子组件但未被 props 所接收的特性。
除了 $attrs,Vue3 中还引入了 $props 对象,它是一个由 props 组成的响应式对象,在组件内部通过解构赋值可以快速地访问 props 的属性值:

<template><div class="demo-component" :style="styleObject">{{ message }}</div>
</template><script>
export default {name: "DemoComponent",props: {message: String,styleObject: Object,},setup(props) {const { message, styleObject } = props;console.log(message, styleObject); // 输出:Hello, world! { color: 'red' }},
};
</script>

在 setup 函数中,通过解构赋值可以快速地访问 props 的属性值。

利用 $attrs 和 $listeners 可以在二次封装 element-ui 组件时非常方便地传递组件属性和事件。

示例代码

下面以 el-input 组件为例,演示一下vue2中如何高效地二次封装 element-ui 组件,从而达到只用少量代码在原有组件上升级的效果:

<template><el-input v-bind="$attrs" v-on="$listeners" :class="{ 'is-invalid': isError }"><template v-if="isError" #append><i class="el-input__icon el-icon-circle-close"></i></template></el-input>
</template><script>
export default {name: "MyInput",inheritAttrs: false,props: {isError: Boolean, // 是否显示错误提示},
};
</script>
<style scoped lang="scss">
//这是写自己的样式内容
</style>

讲解:
1、使用 v-bind=“$attrs” 将父级组件所有的未被 props 所接收的特性绑定到 el-input 组件上。

2、使用 v-on=“$listeners” 将父级组件传递给当前组件的所有事件监听器绑定到 el-input 组件上。

3、在模板中可以很方便地使用 isError 属性来扩展组件,并且不需要在父组件中再次定义。

需要注意的是,由于 element-ui 组件本身也包含了一些默认的属性和事件,因此需要在组件中设置 inheritAttrs: false,以避免传递 element-ui 组件自带的属性和事件。

http://www.yayakq.cn/news/177729/

相关文章:

  • 北京高端企业网站建设企业网站建站策划书
  • 做网站常用字体做微商能利用的网站有哪些
  • 西安搭建网站面料 做网站
  • 住房与城乡建设部违法举报网站宁波网站设计方案
  • 创意家装设计公司seo建站公司
  • 建立网站三大基础什么程序做的网站没有index页面
  • 中小学生在线做试卷的网站wordpress资源站模板
  • 福建莆田网站开发wordpress弹窗通知
  • linux 如何做网站视频网站怎么做排名
  • 网站建设组织机构wordpress导入html
  • 用vue做多页面网站广州网站建设哪家技术好
  • 菜鸟学做网站百度地图网站后台更新
  • 韵达快递小网站怎么做wordpress 没有上级目录的写权限
  • 笔记本怎么建设网站网站如何做网页查询
  • 做别墅花园绿化的网站四川电大住房和城乡建设厅网站
  • 《21天网站建设实录最新新闻热点事件时评
  • 专业手机网站建设价格怎样找到免费的黄页网站
  • 提升网站访问量唐山门户网站建设
  • 不备案的网站能打开吗博客推广工具
  • 威海网站制作服务网站建设报价清单明细
  • 哈尔滨建站免费模板wordpress微信登录插件下载
  • 自己弄网站需要多少钱wordpress政企网站
  • 特色的岑溪网站开发承德的网站建设公司
  • 一步一步教你做网站网站禁止访问目录
  • 台州椒江网站制作公司外贸专业网站制作
  • 网站倒计时怎么做的wordpress搬家文章
  • 本地购物平台湖北百度seo厂家
  • 提高网站注册率企业文化理念口号
  • 网站传送门怎么做株洲网红
  • 沧州做网站最好的公司网站建设外包工作