vue前端开发自学,透传属性的练习demo

复制代码
<template>
  <!-- <Main /> -->
  <!-- <ComA /> -->
  <AttrComponent class="attr-container"/>
</template>
<script>
  // import ComponentEvent from "./components/ComponentEvent.vue"
  // import Main from "./components/Main.vue"
  // import ComA from "./components/ComA.vue"
  import AttrComponent from "./components/AttrComponent.vue"
  export default{
    components:{
      // ComponentEvent,
      // Main,
      // ComA,
      AttrComponent
    }
  }
</script>

vue前端开发自学,透传属性的练习demo!以上代码是,父组件的情况。也是App.vue的入口文件内容。


复制代码
<template>
    <!--必须是有且仅有一个根元素,否则该透传属性不会生效的-->
    <h3>透传属性demo</h3>
</template>
<script>
    export default{
        inheritAttrs:true
    }
</script>
<style>
    .attr-container{
        color:red;
    }
</style>

这是子组件内容,AttrComponent.vue的代码内容。可以看出来,它里面是有且仅有一个根元素,H3标签,如果多一个就不行了。透传属性就会失效了。必须是有且仅有一个根元素才行。

我们还特意做了继承的设定,目前是true。允许继承。所以可以看见,的确是透传属性class成功了。

如果改成false.不允许继承的话,就无法透传那个class属性了。

实际上,这种情况我们很少使用到。因为class,id,attribute这三样事情。我们都有各自的操作方法和语法内容。很少会有人会使用透传属性这样的操作方式。大家作为了解即可。

相关推荐
想你依然心痛4 小时前
AtomCode 在前端开发中的实战体验:React + TypeScript 项目开发实录
前端·react.js·typescript
疯狂的魔鬼4 小时前
精确计算容器剩余视口高度:useAutoContainerFullHeight 的工程实践
前端·css·typescript
Esaka_Forever4 小时前
Python 与 JS (V8) 垃圾回收核心区别 + 底层根源分析
开发语言·javascript·jvm
用户059540174464 小时前
用了 3 个月 ChatGPT,才发现它一直在遗忘——用 Playwright 自动化验证记忆存储一致性
前端·css
玄玄子4 小时前
xss前端解决方案
前端·浏览器·xss
林希_Rachel_傻希希5 小时前
web性能优化之——AI总结视频
前端·javascript·面试
前端炒粉5 小时前
个人简历面经总结二
前端·网络·vue.js·react.js·面试
binbin_525 小时前
UIAbility 与 WindowStage:窗口创建、加载、销毁的完整链路
开发语言·javascript·深度学习·华为·harmonyos
用户059540174465 小时前
用了半年 LangChain Memory,才发现回滚测试压根没测对
前端·css
木木的木云5 小时前
从零构建微前端框架:PavilionMfe 设计揭秘
前端·架构·vite