博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
组件用.vue还是.js_Vue.js组件道具
阅读量:2502 次
发布时间:2019-05-11

本文共 2678 字,大约阅读时间需要 8 分钟。

组件用.vue还是.js

在组件内部定义道具 (Define a prop inside the component)

Props are the way components can accept data from components that include them (parent components).

道具是组件可以从包含它们的组件(父组件)接受数据的方式。

When a component expects one or more prop, it must define them in its props property:

当组件需要一个或多个道具时,必须在其props属性中定义它们:

Vue.component('user-name', {  props: ['name'],  template: '

Hi {

{ name }}

'})

or, in a Vue Single File Component:

或者,在Vue单个文件组件中:

接受多个道具 (Accept multiple props)

You can have multiple props by appending them to the array:

您可以通过将多个道具附加到数组来拥有多个道具:

Vue.component('user-name', {  props: ['firstName', 'lastName'],  template: '

Hi {

{ firstName }} {
{ lastName }}

'})

设置道具类型 (Set the prop type)

You can specify the type of a prop by using an object instead of an array, using the name of the property as the key of each property, and the type as the value:

您可以通过使用对象而不是数组,将属性名称用作每个属性的键,并将类型用作值来指定道具的类型:

Vue.component('user-name', {  props: {    firstName: String,    lastName: String  },  template: '

Hi {

{ firstName }} {
{ lastName }}

'})

The valid types you can use are:

您可以使用的有效类型是:

  • String

  • Number

  • Boolean

    布尔型
  • Array

    数组
  • Object

    目的
  • Date

    日期
  • Function

    功能
  • Symbol

    符号

When a type mismatches, Vue alerts (in development mode) in the console with a warning.

当类型不匹配时,Vue在控制台中以警告方式警告(处于开发模式)。

Prop types can be more articulated.

道具类型可以更清晰地表达。

You can allow multiple different value types:

您可以允许多种不同的值类型:

props: {  firstName: [String, Number]}

将道具设置为强制性 (Set a prop to be mandatory)

You can require a prop to be mandatory:

您可以要求道具是强制性的:

props: {  firstName: {    type: String,    required: true  }}

设置道具的默认值 (Set the default value of a prop)

You can specify a default value:

您可以指定一个默认值:

props: {  firstName: {    type: String,    default: 'Unknown person'  }}

For objects:

对于对象:

props: {  name: {    type: Object,    default: {      firstName: 'Unknown',      lastName: ''    }  }}

default can also be a function that returns an appropriate value, rather than being the actual value.

default也可以是返回适当值的函数,而不是实际值。

You can even build a custom validator, which is cool for complex data:

您甚至可以构建一个自定义验证器,该验证器对复杂数据很酷:

props: {  name: {    validator: name => {      return name === 'Flavio' //only allow "Flavios"    }  }}

将道具传递到组件 (Passing props to the component)

You pass a prop to a component using the syntax

您使用语法将prop传递给组件

if what you pass is a static value.

如果您传递的是静态值。

If it’s a data property, you use

如果它是数据属性,则使用

You can use a ternary operator inside the prop value to check a truthy condition and pass a value that depends on it:

您可以在prop值内使用三元运算符来检查真实条件并传递依赖于该条件的值:

翻译自:

组件用.vue还是.js

转载地址:http://utqgb.baihongyu.com/

你可能感兴趣的文章
【2020-3-21】Mac安装Homebrew慢,解决办法
查看>>
influxdb 命令行输出时间为 yyyy-MM-dd HH:mm:ss(年月日时分秒)的方法
查看>>
已知子网掩码,确定ip地址范围
查看>>
判断时间或者数字是否连续
查看>>
docker-daemon.json各配置详解
查看>>
Docker(一)使用阿里云容器镜像服务
查看>>
Docker(三) 构建镜像
查看>>
FFmpeg 新旧版本编码 API 的区别
查看>>
RecyclerView 源码深入解析——绘制流程、缓存机制、动画等
查看>>
Android 面试题整理总结(一)Java 基础
查看>>
Android 面试题整理总结(二)Java 集合
查看>>
学习笔记_vnpy实战培训day02
查看>>
学习笔记_vnpy实战培训day03
查看>>
VNPY- VnTrader基本使用
查看>>
VNPY - CTA策略模块策略开发
查看>>
VNPY - 事件引擎
查看>>
MongoDB基本语法和操作入门
查看>>
学习笔记_vnpy实战培训day04_作业
查看>>
OCO订单(委托)
查看>>
学习笔记_vnpy实战培训day06
查看>>