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这三样事情。我们都有各自的操作方法和语法内容。很少会有人会使用透传属性这样的操作方式。大家作为了解即可。

相关推荐
hrrrrb13 分钟前
【CSS3】筑基篇
前端·css·css3
boy快快长大15 分钟前
【VUE】day01-vue基本使用、调试工具、指令与过滤器
前端·javascript·vue.js
三原20 分钟前
五年使用vue2、vue3经验,我直接上手react
前端·javascript·react.js
嘉琪coder25 分钟前
React的两种状态哲学:受控与非受控模式
前端·react.js
木胭脂沾染了灰36 分钟前
策略设计模式-下单
java·前端·设计模式
Eric_见嘉40 分钟前
当敦煌壁画遇上 VS Code:我用古风色系开发了编程主题
前端·产品·visual studio code
青红光硫化黑43 分钟前
React基础之项目创建
开发语言·javascript·ecmascript
拉不动的猪1 小时前
刷刷题28(http)
前端·javascript·面试