简体   繁体   中英

Meteor 1.5 : Dynamic Import for Blaze

I am having two questions:

1) I want to use Meteor 1.5 Dynamic Import for Blaze but all the examples and tutorials are given for React. So I am confused how exactly it can be used . Can anyone give examples of it.

2) I am using packages from atmospherejs.com like amcharts which I only need at Admin Dashboard side. How to dynamically import them?

Thanks in Advance!

UPDATE(Solution):

Below is homepage.html (parent template)

<template name="homepage">
  Homepage Content
 {{> Template.dynamic template=content}}    
</template>

login.html (child template)

<template name="login">
  You're logged in!
</template>

login.js

import '../homepage/homepage.js';
import './login.html';
API = function () {
  BlazeLayout.render("homepage",{content: 'login'});
}

export { API }

main.js

LoadLogin = function () {
  import('/imports/modules/common/login/login.js').then(function (api) {
    api.API();
  })
}

/lib/route.js

import { FlowRouter } from 'meteor/ostrio:flow-router-extra';
FlowRouter.route('/', {
  name: 'homepage',
  action() {
    LoadLogin();
  }
});

I am developing my own admin panel, Meteor Candy , to be driven by dynamic imports, so I am happy to share how I got it working.

First, we have the view.html:

<template name="admin">
    Admin
</template>

Second, we have our JS logic:

import { Template } from 'meteor/templating';
import { Meteor } from 'meteor/meteor';
import { Blaze } from 'meteor/blaze';

import './view.html';

API = {}

API.render = function () {
     Blaze.render(Template.admin, document.body);
}

export { API }

Finally, we just need to import that code and trigger our Template to be rendered into the page:

openAdmin = function () {
    import('./imports/admins').then(function (api) {
        api.render()
    })
}

Once something runs the openAdmin() function, the templates will be imported from the server and the render function will be called.

The basic technique for dynamically importing modules in Meteor 1.5 using Blaze is as follows:

Template.theTemplate.events({
  'click button'(event, instance) {
    import("foo").then(Foo => {
      console.log(Foo);
    });
  }
});

Make sure you take a close look at how your module is then imported, because apparently some refactoring may be needed when calling it in your code. For example, using "zxcvbn":

WAS

const result = zxcvbn(pwd);

IS

const result = zxcvbn.default(pwd);

It is pretty straight forward using example link https://github.com/thesaucecode/meteor-amcharts-example/blob/master/client/example2.js , you just have to write the code inside Template.MYTEMPLATE.onRendered(function(){});

On top of that you can use var chart as reactive-var.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM