Vue组件声明注册之特别属性Props

1. 声明及使用Props

props接受的标签属性是自读的
如果想要修改,用变量接收,接收之后在DOM上输出这个响应式变量
响应式变量接收与修改可以在计算属性中修改

①使用<script setup>单文件组件

<template>
  <!--直接调用模版时属性名称-->
  <div>{{ 标签属性 }}</div>
  <div>{{ props.标签属性 }}</div>
</template>

<script setup>
//定义一个title标签,用来接收使用此组件接收到的标签
//也可以只写defineProps(['title'])
let props = defineProps(["使用此组件所定义的标签属性1", "使用此组件所定义的标签属性2",.....]);
props的使用方法为props.标签属性
</script>

<style>
</style>

②不使用<script setup>单文件组件

<template>
  <div>{{ 属性标签 }}</div>
</template>

<script>
export default {
  //定义一个title标签,用来接收使用此组件接收到的标签
  props:['使用此组件所定义的标签属性1','使用此组件所定义的标签属性2',.....],
  //注意下方setup内必须传入上面定义的props
  setup(props){
    调用方法props时,需使用props.属性标签
  }
}
</script>

<style>
</style>

③使用方法

<template>
  <classtest title="Props测试数据" name="Props的name值"/>
  <classtest :title="titletxt" :name="nametxt"/>
</template>
<script setup>
import {ref} from 'vue'
import classtest from "./components/classtest.vue";

let titletxt=ref('这里是动态属性title')
let nametxt=ref('这里是动态属性name')
</script>
<style scoped>
</style>

2. Props使用实例

①使用<script setup>单文件组件

<template>
  <!--直接调用模版时属性名称-->
  <div>{{ title }}</div>
  <div>{{ name }}</div>
  <div>{{ props.title }}</div>
  <div>{{ props.name }}</div>
</template>

<script setup>
//定义一个title标签,用来接收使用此组件接收到的标签
//也可以只写defineProps(['title'])
let props = defineProps(["title", "name"]);
console.log("前端接收到的title值", props);
console.log("前端接收到的title值", props.title);
</script>

<style>
</style>

②不使用<script setup>单文件组件

<template>
  <!--直接调用模版时属性名称,
  注意此处不能使用props.title,直接使用props定义的接收标签-->
  <div>{{ title }}</div>
  <div>{{ name }}</div>
</template>

<script>
export default {
  //定义一个title标签,用来接收使用此组件接收到的标签
  props:['title','name'],
  //注意下方setup内必须传入上面定义的props
  setup(props){
    console.log('接收到的props值:',props)
    console.log(props.title)
  }
}
</script>

<style>
</style>

③使用方法

<template>
  <classtest title="Props测试数据" name="Props的name值"/>
  <classtest :title="titletxt" :name="nametxt"/>
</template>
<script setup>
import {ref} from 'vue'
import classtest from "./components/classtest.vue";

let titletxt=ref('这里是动态属性title')
let nametxt=ref('这里是动态属性name')
</script>
<style scoped>
</style>

3. Props类型验证

运行时type选项
String
Number
Boolean
Array
Object
Date
Function
Symbol
Error

defineProps({
  // 基础类型检查
  // (给出 `null` 和 `undefined` 值则会跳过任何类型检查)
  propA: Number,
  // 多种可能的类型
  propB: [String, Number],
  // 必传,且为 String 类型
  propC: {
    type: String,
    required: true
  },
  // Number 类型的默认值
  propD: {
    type: Number,
    default: 100
  },
  // 对象类型的默认值
  propE: {
    type: Object,
    // 对象或数组的默认值
    // 必须从一个工厂函数返回。
    // 该函数接收组件所接收到的原始 prop 作为参数。
    default(rawProps) {
      return { message: 'hello' }
    }
  },
  // 自定义类型校验函数
  // 在 3.4+ 中完整的 props 作为第二个参数传入
  propF: {
    validator(value, props) {
      // The value must match one of these strings
      return ['success', 'warning', 'danger'].includes(value)
    }
  },
  // 函数类型的默认值
  propG: {
    type: Function,
    // 不像对象或数组的默认,这不是一个
    // 工厂函数。这会是一个用来作为默认值的函数
    default() {
      return 'Default function'
    }
  }
})

所有 prop 默认都是可选的,除非声明了 required: true。

除 Boolean 外的未传递的可选 prop 将会有一个默认值 undefined。

Boolean 类型的未传递 prop 将被转换为 false。这可以通过为它设置 default 来更改——例如:设置为 default: undefined 将与非布尔类型的 prop 的行为保持一致。

如果声明了 default 值,那么在 prop 的值被解析为 undefined 时,无论 prop 是未被传递还是显式指明的 undefined,都会改为 default 值。

当 prop 的校验失败后,Vue 会抛出一个控制台警告 (在开发模式下)。

如果使用了基于类型的 prop 声明 ,Vue 会尽最大努力在运行时按照 prop 的类型标注进行编译。举例来说,defineProps<{ msg: string }> 会被编译为 { msg: { type: String, required: true }}。

其他
1.type 也可以是自定义的类或构造函数,Vue 将会通过 instanceof 来检查类型是否匹配

class Person {
  constructor(firstName, lastName) {
    this.firstName = firstName
    this.lastName = lastName
  }
}

defineProps({
  author: Person
})

2.Boolean 类型转换

defineProps({
  disabled: Boolean
})
<!-- 等同于传入 :disabled="true" -->
<MyComponent disabled />

<!-- 等同于传入 :disabled="false" -->
<MyComponent />

3.当一个 prop 被声明为允许多种类型时,Boolean 的转换规则也将被应用。然而,当同时允许 String 和 Boolean 时,有一种边缘情况——只有当 Boolean 出现在 String 之前时,Boolean 转换规则才适用:

// disabled 将被转换为 true
defineProps({
  disabled: [Boolean, Number]
})
  
// disabled 将被转换为 true
defineProps({
  disabled: [Boolean, String]
})
  
// disabled 将被转换为 true
defineProps({
  disabled: [Number, Boolean]
})
  
// disabled 将被解析为空字符串 (disabled="")
defineProps({
  disabled: [String, Boolean]
})
  • 29
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值