您现在的位置是:首页 > 编程 > 

【Vue 从入门到实战 进阶式掌握完整知识体系】01

2025-07-28 15:45:07
【Vue 从入门到实战 进阶式掌握完整知识体系】01 4、on-Props属性概述当父组件向子组件传的值,子组件没有接收的时候,vue 会把传的值作为子组件最外层 div 的属性;代码代码语言:javascript代码运行次数:0运行复制<!DOCTYPE html> <html lang="en"> <head> <met

【Vue 从入门到实战 进阶式掌握完整知识体系】01

4、on-Props属性

概述

当父组件向子组件传的值,子组件没有接收的时候,vue 会把传的值作为子组件最外层 div 的属性;

代码
代码语言:javascript代码运行次数:0运行复制
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="@next"></script>
</head>

<body>
  <div id="root"></div>
</body>

<script>
  ct app = ({
    template: `
        <div>
          <test message="Good Bye!" />
        </div>
    `
  });

  
  // on-props
  appponent('test',{
    template: `
        <div>Hello World!</div>
    `
  })

  ct vm = ('#root');
</script>

</html>
运行结果
image-202106114262491.png
阻止这种情况的发生
代码语言:javascript代码运行次数:0运行复制
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="@next"></script>
</head>

<body>
  <div id="root"></div>
</body>

<script>
  ct app = ({
    template: `
        <div>
          <test message="Good Bye!" />
        </div>
    `
  });

  
  // on-props
  appponent('test',{
    // 阻止将父组件传递过来的属性和值附加到最外层的 div 上
    inheritAttrs: false,
    template: `
        <div>Hello World!</div>
    `
  })

  ct vm = ('#root');
</script>

</html>
运行结果
image-202106114295416.png
子组件最外层多个div实现接收数据

指定一个 div 来接收父组件传过来的数据,否则会报警告,且此时多个 div 都不接收父组件传的数据

代码语言:javascript代码运行次数:0运行复制
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="@next"></script>
</head>

<body>
  <div id="root"></div>
</body>

<script>
  ct app = ({
    template: `
        <div>
          <test message="Good Bye!" />
        </div>
    `
  });

  
  appponent('test',{
    template: `
        <div>Hello World!</div>
        <div v-bind="$attrs">Hello World!</div>
        <div>Hello World!</div>
    `
  })

  ct vm = ('#root');
</script>

</html>
运行结果

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-5sT0T46I-1624975241626)(.image)]

子组件最外层多个div实现接收到父组件所传递他多个属性和值的其中一个
代码语言:javascript代码运行次数:0运行复制
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="@next"></script>
</head>

<body>
  <div id="root"></div>
</body>

<script>
  ct app = ({
    template: `
        <div>
          <test message="Good Bye!" content="大哥刘备" />
        </div>
    `
  });

  // v-bind 也可以使用简写成 :
  appponent('test',{
    template: `
        <div v-bind:message="$">Hello World!</div>
        <div v-bind:content="$">Hello World!</div>
        <div v-bind="$attrs">Hello World!</div>
    `
  })

  ct vm = ('#root');
</script>

</html>
运行结果
image-2021061148882.png
获取父组件传过来的属性和值
代码语言:javascript代码运行次数:0运行复制
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>hello vue</title>
  <!-- 引入Vue库 -->
  <script src="@next"></script>
</head>

<body>
  <div id="root"></div>
</body>

<script>
  ct app = ({
    template: `
        <div>
          <test message="Good Bye!" content="大哥刘备" />
        </div>
    `
  });

  
  appponent('test',{
    mounted(){
      cole.log(this.$attrs);
    },
    template: `
        <div v-bind:message="$">Hello World!</div>
        <div v-bind:content="$">Hello World!</div>
        <div v-bind="$attrs">Hello World!</div>
    `
  })

  ct vm = ('#root');
</script>

</html>
运行结果
image-2021061144201860.png
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。 原始发表:2021-06-29,如有侵权请联系 cloudcommunity@tencent 删除数据htmlscriptvue入门

#感谢您对电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格的认可,转载请说明来源于"电脑配置推荐网 - 最新i3 i5 i7组装电脑配置单推荐报价格

本文地址:http://www.dnpztj.cn/biancheng/1200208.html

相关标签:无
上传时间: 2025-07-23 18:13:45
留言与评论(共有 16 条评论)
本站网友 深圳平安银行信用卡
4分钟前 发表
【Vue 从入门到实战 进阶式掌握完整知识体系】01 4
本站网友 胎心仪
20分钟前 发表
原始发表:2021-06-29
本站网友 产检项目
14分钟前 发表
false
本站网友 宁波天宫庄园
16分钟前 发表
` <div>Hello World!</div> ` }) ct vm = ('#root'); </script> </html>运行结果image-202106114295416.png子组件最外层多个div实现接收数据 指定一个 div 来接收父组件传过来的数据
本站网友 隔壁班的男生
25分钟前 发表
false
本站网友 中国移动广西
1分钟前 发表
initial-scale=1.0"> <title>hello vue</title> <!-- 引入Vue库 --> <script src="@next"></script> </head> <body> <div id="root"></div> </body> <script> ct app = ({ template
本站网友 汕头论坛
23分钟前 发表
initial-scale=1.0"> <title>hello vue</title> <!-- 引入Vue库 --> <script src="@next"></script> </head> <body> <div id="root"></div> </body> <script> ct app = ({ template
本站网友 潮白河孔雀城
6分钟前 发表
` <div v-bind
本站网友 春江花园二手房
3分钟前 发表
initial-scale=1.0"> <title>hello vue</title> <!-- 引入Vue库 --> <script src="@next"></script> </head> <body> <div id="root"></div> </body> <script> ct app = ({ template
本站网友 爬叉
23分钟前 发表
{ template
本站网友 图图
5分钟前 发表
on-Props属性概述当父组件向子组件传的值
本站网友 生男生女预测表
13分钟前 发表
message="$">Hello World!</div> <div v-bind
本站网友 newwise
8分钟前 发表
initial-scale=1.0"> <title>hello vue</title> <!-- 引入Vue库 --> <script src="@next"></script> </head> <body> <div id="root"></div> </body> <script> ct app = ({ template
本站网友 团购买房
3分钟前 发表
` <div>Hello World!</div> <div v-bind="$attrs">Hello World!</div> <div>Hello World!</div> ` }) ct vm = ('#root'); </script> </html>运行结果[外链图片转存失败
本站网友 高盛在中国
23分钟前 发表
message="$">Hello World!</div> <div v-bind