首页 > 其他分享 >【Vue2-03】props属性

【Vue2-03】props属性

时间:2022-11-13 19:23:19浏览次数:44  
标签:03 String default Vue2 Student props 默认值 name

props配置

功能:让组件接收外部传过来的数据

  1. 数据传递:
<Demo name="xxx">
  1. 接收数据:
方式一(只接收):
	props: ['name']
	
方式二(限制类型):
	props:{ 
		name: String
	}
方式三(限制类型、限制必要性、指定默认值):
	props:{
		name: {
			type: String, // 类型
			required: true, // 必要性
			default: '张三' // 默认值
		}
	}

备注:props是只读的,Vue底层会监测你对props的修改,如果进行了修改,就会发出警告,若业务需求确实需要修改,那么请复制props的内容到data中一份,然后去修改data中的数据。

例子

  • Student.vue
<template>
  <div>
      <h1>{{msg}}</h1>
      <h3>我的名字是:{{name}}</h3>
      <h3>我的年龄是:{{age}}</h3>
      <br>
  </div>
</template>

<script>
    export default {
        name: 'Student',
        data() {
            return {
                msg: "我是一个学生",
            }
        },
        props: ['name', 'age']
        /*
        props: {
            name: String,
            age: Number
        }
        props: {
            name: {
                type: String, //限制类型
                required: true, // 限制必要性
                default: '张三' // 指定默认值
            }
        }*/
        
    }
</script>

<style>

</style>
  • App.vue
<template>
  <div>
    <!-- 向组件传递数据 -->
    <Student  age="22"></Student>
    <Student name="Michale" age="32"></Student>
  </div>
</template>

<script>
  // 引入组件
  import Student from './components/Student.vue';

  export default {
    name: 'App',
    // 注册组件
    components: {
      Student,

    }
  }
</script>

<style scoped>

</style>

标签:03,String,default,Vue2,Student,props,默认值,name
From: https://www.cnblogs.com/keyongkang/p/16886673.html

相关文章

  • [VP记录]AGC003
    以后不放题目链接了。[AGC003A]Wannagobackhome普及-。charS[1010];intlen;bools,e,n,w;intmain(){scanf("%s",S+1);len=strlen(S+1);for(inti=1......
  • 【Vue2-01】Vue脚手架
    Vue脚手架一、单文件组件1.单文件组件定义单文件组件:*.vue文件,类似HTML格式的文件。Vue的单文件组件会将一个组件的逻辑(JavaScript)、模板(HTML)和样式(CSS)封装在同......
  • 微服务笔记之Eureka03(服务续约分析)
    服务续约接口分析com.netflix.eureka.resources.InstanceResource#renewLeasepublicResponserenewLease(@HeaderParam(PeerEurekaNode.HEADER_REPLICATIO......
  • 微服务笔记之Eureka03
    服务注册接口源码分析:com.netflix.eureka.resources.ApplicationResource#addInstancepublicResponseaddInstance(InstanceInfoinfo,......
  • 解决操作mysql报 1044 - Access denied for user 'root'@'%' to database 'table&#039
    naccat操作数据库时报 1044-Accessdeniedforuser'root'@'%'todatabase'table'说明root用户没有授权,需要授权Mysql8.01.进入mysql容器dockerexec-itmys......
  • Air32F103CBT6
    #DAPLinkFirmware-seehttps://daplink.ioBuildID:v0257-9-g1c60ddb5(armcc,localmods)UniqueID:070000011a5a5bb50000000001d3080fa5a5a5a597969908HICID......
  • 题解 AGC036D【Negative Cycle】
    problem(fromluogu)有一个\(N\)个点的有向图,节点标号为\(0\sim(N-1)\)。这张图初始时只有\(N-1\)条边,每条边从\(i\)指向\(i+1\),边权为\(0\)。对于每一......
  • gyp ERR! stack Error: Can't find Python executable "python", you can set the PYT
    方式一:安装python解决(正确配置系统环境变量),python(v2.7recommended,v3.x.xisnotsupported)-推荐下载:http://www.python.org/ftp/python/2.7.3/python-2.7.3.msi......
  • windows家庭版安装svn失败,提示2503,2502错误
    情况描述:windows家庭版安装svn失败,提示2503,2502错误,按提示关闭一些程序也会提示,安装失败;解决办法:1.首先在组策略启用“始终以提升的权限安装”win+r  然后输入gpedit......
  • CSP 202203-1 未初始化警告 C++
    1#include<iostream>2#include<vector>3intmain(){4intx{},y{};5std::cin>>x>>y;//读入第一行6std::vector<std::vector<int>>k......