vue attribute props default type
('my-component', { props: { // Basic type check (`null` matches any type) //Single type propA: Number, // Multiple possible types propB: [String, Number], // Required string propC: { type: String, required: true }, //function propC: { type:Function, required: true }, // Number with default value propD: { type: Number, default: 100 }, //Multiple possible types with default values propE: { type: [String, Number], default: 100 }, // Object with default value propF: { type: Object, // Object or array and will definitely return the default value from a factory function default: function () { return { message: 'hello' } } }, // Custom verification function propG: { validator: function (value) { // This value must match one of the following strings return ['success', 'warning', 'danger'].indexOf(value) !== -1 } } } })
props custom properties
1. This is a custom property. When encapsulating general components, using props reasonably can greatly improve component reusability.
2. Syntax: Can be defined as array type:
export default { props:['init'] }
It can also be defined as object type:
export default { // props is a "custom property", allowing users to specify the initial value for the current component through custom properties // The name of the custom attribute is customized by the encapsulator (as long as the name is legal) // The data in props can be used directly in the template structure // Note: props are read-only, do not modify the value of props directly, otherwise the terminal will report an error! // props: ['init'], props: { // Custom property A: { /* Configuration options */ }, // Custom property B: { /* Configuration options */ }, // Custom property C: { /* Configuration options */ }, init: { // If the init attribute is not passed when the outside world uses the Count component, the default value takes effect default: 0, // The value type of init must be a Number type: Number, // Required verification required: true } }, }
Note: Array types do not have default attributes, and they only exist as object types.
The above is personal experience. I hope you can give you a reference and I hope you can support me more.