vue3+ts深入组件Props

在Vue 3和TypeScript中,深入了解组件的Props是非常重要的。Props是组件之间进行数据传递的一种方式,可以将数据从父组件传递给子组件。

首先,在Vue 3中定义Props的方式有所改变。在组件的选项中,我们可以使用props属性来定义Props的类型和验证规则。例如:

typescript 复制代码
import { defineComponent, PropType } from 'vue';

export default defineComponent({
  props: {
    // 基本类型的Props
    name: {
      type: String,
      required: true
    },
    age: {
      type: Number,
      default: 18
    },
    // 自定义类型的Props
    person: {
      type: Object as PropType<{ name: string, age: number }>,
      required: true
    },
    // 数组类型的Props
    hobbies: {
      type: Array as PropType<string[]>,
      default: () => []
    }
  },
  // ...
});

在上面的例子中,我们定义了几个不同类型的Props。name是一个必需的字符串类型的Props,age是一个可选的数字类型的Props,默认值为18。person是一个必需的自定义类型的Props,它是一个包含name和age属性的对象。hobbies是一个可选的数组类型的Props,默认值为空数组。

在使用Props时,我们可以在子组件中通过props选项来访问它们。例如:

typescript 复制代码
import { defineComponent } from 'vue';

export default defineComponent({
  props: ['name', 'age', 'person', 'hobbies'],
  // ...
  created() {
    console.log(this.name); // 访问字符串类型的Props
    console.log(this.age); // 访问数字类型的Props
    console.log(this.person); // 访问自定义类型的Props
    console.log(this.hobbies); // 访问数组类型的Props
  }
});

在上面的例子中,我们通过props选项将Props声明为组件的属性。然后,在组件的created生命周期钩子中,我们可以通过this关键字来访问这些Props。

此外,还可以使用TypeScript的类型注解来提供Props的类型检查。例如:

typescript 复制代码
import { defineComponent } from 'vue';

interface Person {
  name: string;
  age: number;
}

export default defineComponent({
  props: {
    person: {
      type: Object as () => Person,
      required: true
    }
  },
  // ...
});

在上面的例子中,我们使用了TypeScript的接口来定义Person类型,并在props选项中使用了类型注解来指定person的类型为Person。

总结一下,在Vue 3和TypeScript中,我们可以使用props选项来定义和验证组件的Props。可以使用不同类型的Props,包括基本类型、自定义类型和数组类型。在子组件中,可以通过props选项来访问这些Props,并使用TypeScript的类型注解来提供类型检查。这样可以更安全和可靠地进行组件间的数据传递。

传递静态prop

在Vue 3和TypeScript中,如果要传递静态的Props,可以在父组件中直接在子组件的标签上使用Props的语法来传递静态值。

例如,假设我们有一个子组件ChildComponent,它有一个接受字符串类型的Props message。

typescript 复制代码
import { defineComponent, PropType } from 'vue';

export default defineComponent({
  props: {
    message: {
      type: String,
      required: true
    }
  },
  // ...
});

在父组件中,可以在子组件的标签上使用Props的语法来传递静态值。

html 复制代码
<template>
  <div>
    <child-component message="Hello, World!"></child-component>
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  components: {
    ChildComponent
  }
};
</script>

在上面的例子中,我们在ChildComponent的标签上使用message属性,并传递了静态的字符串值"Hello, World!"。

在子组件中,可以通过props选项来接收传递的静态Props。

typescript 复制代码
import { defineComponent } from 'vue';

export default defineComponent({
  props: ['message'],
  // ...
  created() {
    console.log(this.message); // 输出:Hello, World!
  }
});

在上面的例子中,我们在子组件的created生命周期钩子中,通过this.message来访问传递的静态Props。

传递非字符串类型,使用v-bind

如果要传递非字符串类型的Props,并且希望使用动态的值,可以使用v-bind指令来绑定Props。

例如,假设我们有一个子组件ChildComponent,它有一个接受数字类型的Props count。

typescript 复制代码
import { defineComponent, PropType } from 'vue';

export default defineComponent({
  props: {
    count: {
      type: Number,
      required: true
    }
  },
  // ...
});

在父组件中,可以使用v-bind指令来绑定Props的值。

html 复制代码
<template>
  <div>
    <child-component :count="totalCount"></child-component>
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  components: {
    ChildComponent
  },
  data() {
    return {
      totalCount: 10
    };
  }
};
</script>

在上面的例子中,我们使用v-bind指令来绑定子组件的count属性,并将其值绑定到父组件的totalCount变量上。

在子组件中,可以通过props选项来接收传递的动态Props。

typescript 复制代码
import { defineComponent } from 'vue';

export default defineComponent({
  props: ['count'],
  // ...
  created() {
    console.log(this.count); // 输出:10
  }
});

在上面的例子中,我们在子组件的created生命周期钩子中,通过this.count来访问传递的动态Props。

prop校验,单向数据流

在Vue中,可以通过使用props选项来对Props进行校验,以确保传递给组件的数据满足特定的要求。

例如,假设我们有一个子组件ChildComponent,它有一个接受数字类型的Props count,并且要求传递的值必须大于0。

typescript 复制代码
import { defineComponent, PropType } from 'vue';

export default defineComponent({
  props: {
    count: {
      type: Number,
      required: true,
      validator: (value: number) => value > 0
    }
  },
  // ...
});

在上面的例子中,我们在props选项中定义了count属性,并指定了它的类型为Number,并且设置了required: true,表示这个Props是必需的。同时,我们还使用了一个自定义的校验函数validator,该函数接受传递的值作为参数,返回一个布尔值,用于校验传递的值是否满足要求。

在父组件中,如果传递的Props不满足校验要求,Vue会在控制台中输出警告信息。

html 复制代码
<template>
  <div>
    <child-component :count="totalCount"></child-component>
  </div>
</template>

<script>
import ChildComponent from './ChildComponent.vue';

export default {
  components: {
    ChildComponent
  },
  data() {
    return {
      totalCount: -5
    };
  }
};
</script>

在上面的例子中,我们在父组件中将totalCount设置为-5,这违反了子组件的校验规则。

相关推荐
计算机魔术师5 分钟前
半年翻倍!OpenAI再融300亿,估值冲到1.4万亿
前端
研☆香27 分钟前
jQuery + Swiper.js 实现动态渲染轮播图
前端·javascript·jquery
子兮曰1 小时前
Node.js 50个优势场景盘点:一个人单干,为啥我多数时候只用它
前端·后端
IT_陈寒1 小时前
我花3小时debug,就因为JavaScript这个隐式转换坑
前端·人工智能·后端
excel2 小时前
Nuxt 中 useSeoMeta 与 useHead 的区别
前端
竹林8182 小时前
OmniGame 技术白皮书:从零依赖到 WebRTC P2P,重新定义网页小游戏的工程上限
前端·浏览器
ss2732 小时前
AI全栈实战 | 2.1-02 CSS 布局:垂直居中为什么是经典面试题?所有布局在解决什么矛盾
前端·css
律宏阔2 小时前
Flutter Riverpod 3:统一打印 Provider 错误与自动重试日志
前端·flutter
周洲08302 小时前
STM32片内Flash读写深度详解|掉电参数保存、底层原理
linux·前端·stm32
data analyse 4562 小时前
埋点工具的私有化部署成本高吗?
前端·数据分析·github