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,它是一个包含nameage属性的对象。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,这违反了子组件的校验规则。

相关推荐
天宇&嘘月2 小时前
web第三次作业
前端·javascript·css
小王不会写code3 小时前
axios
前端·javascript·axios
发呆的薇薇°4 小时前
vue3 配置@根路径
前端·vue.js
luoluoal4 小时前
基于Spring Boot+Vue的宠物服务管理系统(源码+文档)
vue.js·spring boot·宠物
luckyext4 小时前
HBuilderX中,VUE生成随机数字,vue调用随机数函数
前端·javascript·vue.js·微信小程序·小程序
小小码农(找工作版)4 小时前
JavaScript 前端面试 4(作用域链、this)
前端·javascript·面试
前端没钱4 小时前
前端需要学习 Docker 吗?
前端·学习·docker
前端郭德纲4 小时前
前端自动化部署的极简方案
运维·前端·自动化
海绵宝宝_5 小时前
【HarmonyOS NEXT】获取正式应用签名证书的签名信息
android·前端·华为·harmonyos·鸿蒙·鸿蒙应用开发
码农土豆5 小时前
chrome V3插件开发,调用 chrome.action.setIcon,提示路径找不到
前端·chrome