Vue3【十七】props的作用和组件之间的传值限定类型和默认值

Vue3【十七】props的作用和组件之间的传值限定类型和默认值

Vue3【十七】props的作用和组件之间的传值限定类型和默认值
父组件传值给子组件 多个值传递
传值限定类型和
默认值

实例截图

组件之间的传值,多个值

组件之间的传值限定类型和默认值

目录结构

在这里插入图片描述

代码

person.vue

<template>
    <div class="person">
        <p>Props的使用</p> 
        <!-- <p>父组件传值给子组件</p> -->
        <p>父组件传值给子组件:a:{{ a }} b: {{ b }}</p>
        <h3>list: {{ list }}</h3>
        <h4>
            <ul>
                <li v-for="w in list" :key="w.id" >
                    {{ w.name }} -- {{ w.age }}
                </li>
            </ul>

        </h4>
        <!-- <h3 a ="1+1" :b="1+1" :d='a' >  测试标签计算代码:a ="1+1" :b="1+1" :d=a  </h3> -->

    </div>
</template>

<script lang="ts" setup namwe="Person">
import { type PersonInter,type Persons } from '@/types';
import { reactive } from 'vue';

// 等同于 Array<PersonInter>
// let personList3 = reactive<Persons>([
//     {id: 1,name: '白娘子',age: 10008,},
//     {id: 2,name: '法海',age: 10900,},
//     {id: 3,name: '文曲星',age: 20,x: 111},
// ])

// difine开头的函数 可以直接使用 接受 a,b,list同时将props保存到x 中
let x = defineProps(['a','b','list'])
console.log(x)

// 只接收list
// defineProps(['list'])

// 接受 list 并限制类型
// defineProps<{ list: Persons }>()

// 接受list 限制类型 限制必要性 指定默认值
// withDefaults(
//     defineProps<{ list?: Persons}>(),
//     {
//         list:()=>[
//             {id:1,name:'小青',age:11000}
//         ]
//     }

// ) 
</script>

<style scoped>
.person {
    background-color: #ff9e4f;
    box-shadow: 0 0 10px;
    border-radius: 30px;
    padding: 30px;
}

button {
    margin: 0 10px;
    padding: 0 5px;
    box-shadow: 0 0 5px;
    ;
}
</style>

index.ts

//  定义一个接口,用于限制person对象的具体属性
export interface PersonInter {
    id: number,
    name: string,
    age: number,
    x?: number, //? 表示可选属性
}

// 一个自定义类型
// export type Persons = Array<PersonInter>
// 等同于
export type Persons = PersonInter[]

app.vue

<template>
    <div class="app">
        <h1>你好世界! 我是App根组件</h1>
        <Person a="经验值+200" b="魔法药水+100" :list="personList" />
        <!-- <Person  /> -->
    </div>
</template>

<script lang="ts" setup name="App"">
import Person from './components/Person.vue'
import {reactive} from 'vue'
import {Persons} from './types'

let personList = reactive<Persons>([
    {id: 1,name: '白娘子',age: 10008,},
    {id: 2,name: '法海',age: 10900,},
    {id: 3,name: '文曲星',age: 20,x: 111},
])
</script>

<style scoped>
.app {
    background-color: #4fffbb;
    box-shadow: 0 0 10px;
    border-radius: 10px;
    padding: 20px;
}
</style>

相关推荐

  1. vue3+vite+ts父子组件之间

    2024-06-12 19:12:06       43 阅读
  2. vue3父子组件之间方式

    2024-06-12 19:12:06       21 阅读
  3. vue3组件之间通讯

    2024-06-12 19:12:06       12 阅读
  4. vue组件

    2024-06-12 19:12:06       46 阅读
  5. vue3使用mitt用于组件之间

    2024-06-12 19:12:06       31 阅读
  6. htmlashx之间以及jsaspx

    2024-06-12 19:12:06       15 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-06-12 19:12:06       14 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-06-12 19:12:06       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-06-12 19:12:06       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-06-12 19:12:06       18 阅读

热门阅读

  1. 日期input框能写占位符吗

    2024-06-12 19:12:06       6 阅读
  2. web前端号:探索、挑战与未来的无限可能

    2024-06-12 19:12:06       7 阅读
  3. jQuery如何验证复选框是否选中

    2024-06-12 19:12:06       5 阅读
  4. js基本数据类型

    2024-06-12 19:12:06       5 阅读
  5. streamlit和grado的使用

    2024-06-12 19:12:06       5 阅读
  6. 中证指数绿色金融

    2024-06-12 19:12:06       6 阅读
  7. 【Linux学习之路 | vim编译器】vim编译器的使用

    2024-06-12 19:12:06       6 阅读
  8. 互动技巧( Interaction Skills 业务分析能力)

    2024-06-12 19:12:06       5 阅读
  9. Excel分组做散点图

    2024-06-12 19:12:06       7 阅读
  10. 最大N个数与最小N个数之和

    2024-06-12 19:12:06       5 阅读
  11. 【MeshLib & VTK】MeshLib PK VTK

    2024-06-12 19:12:06       7 阅读