<can-import>
can/view/stache/system.import
<can-import from="MODULE_NAME"/>
Parameters
-
MODULE_NAME
{moduleName}
OptionalA module that this template depends on.
{moduleName}
OptionalA module that this template depends on.
Use
A template might depend on component or helper modules.
<can-import>
allows you to specify these dependencies.Example:
Currently this only works with can.autorender or the system plugin.
Progressive Loading
A template may load or conditionally load a module after the initial page load.
<can-import>
allows progressive loading by using an end tag.The first example below shows a component being loaded ad hoc. The second illustrates conditionally loading modules based on some application state.
Example:
static & dynamic import
static
These are imports that are direct dependencies of a template.
Example:
which is equivalent to a ES6 import like:
dynamic
These are conditional imports, things you only want to import in certain situations. Like described before in section "Progressive Loading".
which is equivalent to a stealJS import like:
Please notice that when dynamically importing modules in a stache file, the scope inside <can-import> is a Promise, so you have to wait until it is resolved. Use the
{{#if isResolved}}
helper for that.The recommended way to progressively load modules with StealJS is to use the
@loader
module provided by steal. For more information see http://stealjs.com/docs/@loader.htmlworking with modules
With the new CanJS binding syntax introduced in 2.3 you can use
can-import
to work with modules like in ES6 or CJS. For example we have a ES6 module: mymodule.jsOne way is to import the module within the reference scope: app.js
The other way is to import the module into the template's data. app.js
As you may notice, you have to define a
can.Map
with predefined attributes. The best way to do that is to use the define plugin. To load a function from a module you have to use the@
operator. For more information see the documentationcan-tag
can-tag
allows for injecting a component, using the imported promise as the injected component's view model.The example below shows a loading graphic until the cart component has been loaded. Once the cart promise is resolved,
<shopping-cart></shopping-cart>
is injected into the page.Loading Indicator Component
Main Template