Type: Object | Function
Restriction: Only accepts Function
when used in a component definition.
Details:
The data object for the Vue instance. Vue will recursively convert its properties into getter/setters to make it “reactive”. The object must be plain: native objects such as browser API objects and prototype properties are ignored. A rule of thumb is that data should just be data - it is not recommended to observe objects with their own stateful behavior.
Once observed, you can no longer add reactive properties to the root data object. It is therefore recommended to declare all root-level reactive properties upfront, before creating the instance.
After the instance is created, the original data object can be accessed as vm.$data
. The Vue instance also proxies all the properties found on the data object, so vm.a
will be equivalent to vm.$data.a
.
Properties that start with _
or $
will not be proxied on the Vue instance because they may conflict with Vue’s internal properties and API methods. You will have to access them as vm.$data._property
.
When defining a component, data
must be declared as a function that returns the initial data object, because there will be many instances created using the same definition. If we use a plain object for data
, that same object will be shared by reference across all instances created! By providing a data
function, every time a new instance is created we can call it to return a fresh copy of the initial data.
If required, a deep clone of the original object can be obtained by passing vm.$data
through JSON.parse(JSON.stringify(...))
.
var data = { a: 1 } // direct instance creation var vm = new Vue({ data: data }) vm.a // => 1 vm.$data === data // => true // must use function when in Vue.extend() var Component = Vue.extend({ data: function () { return { a: 1 } } })
Note that if you use an arrow function with the data
property, this
won’t be the component’s instance, but you can still access the instance as the function’s first argument:
data: vm => ({ a: vm.myProp })
See also: Reactivity in Depth
Type: Array<string> | Object
Details:
A list/hash of attributes that are exposed to accept data from the parent component. It has an Array-based simple syntax and an alternative Object-based syntax that allows advanced configurations such as type checking, custom validation and default values.
With Object-based syntax, you can use following options:
type
: can be one of the following native constructors: String
, Number
, Boolean
, Array
, Object
, Date
, Function
, Symbol
, any custom constructor function or an array of those. Will check if a prop has a given type, and will throw a warning if it doesn’t. More information on prop types.default
: any
required
: Boolean
validator
: Function
// simple syntax Vue.component(‘props-demo-simple‘, { props: [‘size‘, ‘myMessage‘] }) // object syntax with validation Vue.component(‘props-demo-advanced‘, { props: { // type check height: Number, // type check plus other validations age: { type: Number, default: 0, required: true, validator: function (value) { return value >= 0 } } } })
See also: Props
原文:https://www.cnblogs.com/chucklu/p/14241974.html