createComponentLoader function returns a component loader. When a component loader is specified in components, the component will be loaded asynchronously: the target component will not be rendered during attaching, instead it’ll be rendered after. The features of asynchronous components include:
Each loader returned by createComponentLoader will be loaded only once. In other words, the load method will be called only once.
The target component is ensured to be rendered asynchronously. Even if the loading has already completed, the target component rendering will still be queued as a macro task after the current component finished rendering.
createComponentLoader accepts an asynchronous function as its argument, in which case the returned Promise should be resolved as a component class.
// simulate the loading process, will be resolved in 1 second var inputLoader = san.createComponentLoader(function () { returnnewPromise(function (resolve) { setTimeout(function () { resolve(InputComponent); }, 1000); }); });
Typically, asynchronous components all have a certain complexity. We can specify a placeholder to be shown while the component is loading. In this case, pass an object with a load property specifying the loader function and a placeholder property specifying the placeholder component to the createComponentLoader function.
// simulate the loading process, will be resolved in 1 second var inputLoader = san.createComponentLoader({ load: function () { returnnewPromise(function (resolve) { setTimeout(function () { resolve(InputComponent); }, 1000); }); },
// simulate the loading process, will be resolved in 1 second var inputLoader = san.createComponentLoader({ load: function () { returnnewPromise(function (resolve, reject) { setTimeout(function () { reject(); }, 1000); }); },
// simulate the loading process, will be resolved in 1 second var inputLoader = san.createComponentLoader(function () { returnnewPromise(function (resolve, reject) { setTimeout(function () { reject(LabelComponent); }, 1000); }); });