多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[TOC] >[success] # Non-Props 属性是什么 1. **inheritAttrs** :**不接收父组件传入的属性** 2. **$attrs**: **接收父组件传入的所有属性** 3. **$attrs.传递过来的属性**: **按需来显示,要显示的属性** 4. 在 **生命周期** 中使用 **$attrs** >[success] ## inheritAttrs 如果 **父组件传值,子组件不接收的话,它会把父组件传递过来的内容放在子组件最外层的标签上,变成dom标签的一个属性** ,如下代码: **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Non-Props 属性是什么</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> // Non-prop 属性 const app = Vue.createApp({ template: ` <div> <counter msg="hello"/> </div>` }) // 子组件未用props接收msg app.component('counter', { template: '<div>Counter</div>' }) const vm = app.mount('#root') </script> </html> ~~~ **子组件在浏览器上就会被渲染** 成这样: ~~~ <div id="root" data-v-app=""> <div> <!-- 这里会多个msg属性这是因为子组件没有接收父组件传过来的属性 --> <div msg="hello">Counter</div> </div> </div> ~~~ 如果你 **不希望渲染时标签上有这样的属性** ,可以 **在子组件中添加 inheritAttrs 属性,意思是不继承父组件传过来的props属性** 。 ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Non-Props 属性是什么</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> // Non-prop 属性 const app = Vue.createApp({ template: ` <div> <counter msg="hello"/> </div>` }) // 子组件未用props接收msg app.component('counter', { inheritAttrs: false, // 不继承父组件传过来的props属性 template: '<div>Counter</div>' }) const vm = app.mount('#root') </script> </html> ~~~ 这样写**渲染后子组件上就不会有父组件传递过来的属性** 了,包括 **style,class** 也不会被传递过来。 >[success] ## $attrs **父组件传递给子组件的属性(style、class等等)** 可以用 **指定给子组件的某个元素** ,如下代码: **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Non-Props 属性是什么</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> // Non-prop 属性 const app = Vue.createApp({ data(){ return { num: 1 } }, template: ` <div> <counter style="color:red" class="div-style" msg="hello"/> </div>` }) app.component('counter', { template: ` <div>Counter</div> <div v-bind="$attrs">Counter</div> <div>Counter</div> ` }) const vm = app.mount('#root') </script> </html> ~~~ 渲染后的结果如下: ~~~ <div id="root" data-v-app=""> <div> <div>Counter</div> 因为子组件的v-bind="$attrs"是加到第二个元素身上了,所以渲染时都会添加到该标签身上 <div class="div-style" msg="hello" style="color: red;">Counter</div> <div>Counter</div> </div> </div> ~~~ >[success] ## $attrs按需显示 如果你 **只想在某个标签身上显示某个传递过来的属性** ,你可以这样写: **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Non-Props 属性是什么</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> // Non-prop 属性 const app = Vue.createApp({ template: ` <div> <counter style="color:red" class="div-style" msg="hello"/> </div>` }) app.component('counter', { template: ` <div :style="$attrs.style">Counter</div> <div :class="$attrs.class">Counter</div> <div :msg="$attrs.msg">Counter</div> ` }) const vm = app.mount('#root') </script> </html> ~~~ 放到浏览器上渲染的结果就是这样: ~~~ <div id="root" data-v-app=""> <div> <div style="color: red;">Counter</div> <div class="div-style">Counter</div> <div msg="hello">Counter</div> </div> </div> ~~~ >[success] ## 在生命周期中使用$attrs 我们除了可以 **在子组件内的标签上使用 $attrs ,也可以在 mounted 生命周期中获取到 $attrs** 代码如下: **index.html** ~~~ <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Non-Props 属性是什么</title> <!-- 通过cdn方式引入vue --> <script src="https://unpkg.com/vue@next"></script> </head> <body> <div id="root"></div> </body> <script> // Non-prop 属性 const app = Vue.createApp({ template: ` <div> <counter style="color:red" class="div-style" msg="hello"/> </div>` }) app.component('counter', { mounted(){ console.log(this.$attrs.style) }, template: ` <div :style="$attrs.style">Counter</div> <div :class="$attrs.class">Counter</div> <div :msg="$attrs.msg">Counter</div> ` }) const vm = app.mount('#root') </script> </html> ~~~