All component data related operations are provided by the data property of the component instance.
Retrieving Data
Retrieve data through the data.get method.
1 | san.defineComponent({ |
The data.get method accepts a string representing the property accessor
, so the above example can also be written like this:
1 | san.defineComponent({ |
Manipulating Data
data provides some methods of data manipulation. Refer to the data manipulation document for more.
Initializing Data
When the component is instantiated, you can pass the data option to specify the component’s initial data.
1 | var MyApp = san.defineComponent({ |
Passing in the initial data when new
a component is not a common parttern. In general, If you want to set initial data for each instance when you define a component, you can specify it in the initData method. The initData method returns initial data for the component instance.
1 | var MyApp = san.defineComponent({ |
Computed Data
Sometimes, the value of a data item may be computed from other data items, and we can define computed to compute data. computed is an object, the key is the name of the computed data item, and value is a function that returns the value of the data item.
1 | san.defineComponent({ |
In this case, item name
is a computed data, whose value computed from firstName
and lastName
data item.
tips
: In functions of computing data, you can only use the this.data.get method to get the values of data items. You cannot call a component method with this.method or set the component data with this.data.set.
1 | san.defineComponent({ |
The computed data item can depend on another computed data item. In the above example, the name
item that the info
item depends on is a computed data item. However, be careful when using it, do not form a circular dependency between the computed data items.