DoneJS StealJS jQuery ++ FuncUnit DocumentJS
3.0.0
2.3.27

 

  • Github
  • Twitter
  • Chat
  • Forum
  • Guides
  • Core
  • Ecosystem
    • can-construct-super
    • can-define-stream
    • can-fixture
    • can-fixture-socket
    • can-jquery
    • can-stache-converters
    • can-stream
    • can-vdom
    • can-view-import
      • Pages
        • Dynamic Imports
        • Static Imports
      • Attributes
        • can-tag
        • from
        • {^value}
    • can-zone
    • steal-stache
  • Infrastructure
  • Legacy
  • Bitovi
    • Bitovi.com
    • Blog
    • Consulting
    • Training
    • Open Source
  • Chat
  • Forum
  • Star
  • Follow @canjs
  • CanJS
  • /
  • Ecosystem
  • /
  • can-view-import
  • /
  • Dynamic Imports
  • / On this page
    • Dynamic Imports

      page

      Dynamic imports are used in conditional situations such as within a {{#if expression}}, to prevent unnecessarily fetching resources that might not be needed in all cases.

      • source

      To make your import be loaded dynamically it cannot but self closing like /> but rather must have a closing tag </can-import>.

      Example

      <can-import from="components/foobar">
        {{#if isResolved}}
        <foobar/>
        {{/if}}
      </can-import>
      

      which is equivalent to a Steal import like:

      steal.import('components/foobar').then(function(foobar) {
       // access to the module you loaded.
       // e.g. access to a component's ViewModel 
       // foobar.ViewModel
      });
      

      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 before injecting something like a can-component.

      Use the {{#if isResolved}} helper for that.

      CanJS is part of DoneJS. Created and maintained by the core DoneJS team and Bitovi. Currently 3.0.0.