2016-11-05 52 views
0

我已經得到了很多的代碼如下所示:爲JavaScript動態進口陣營流星

import {Meteor} from 'meteor/meteor'; import {createContainer} from 'meteor/react-meteor-data'; 

import FoodItemList from '../components/FoodItemList.jsx'; 

import {FoodItems} from '../../api/FoodItems/FoodItems.js'; 

const FoodItemListContainer = createContainer(({imageIDFilter}) => { 
    const user = Meteor.user() 
     ? Meteor.user().username 
     : ''; 
    const query = { 
     username: { 
      $not: { 
       $eq: user 
      } 
     } 
    }; 
    const foodItems = Meteor.subscribe('foodItems'); 
    const foodItemList = FoodItems.find(query).fetch() 
    const loading = !foodItems.ready(); 

    return {loading, foodItemList, imageIDFilter, user}; 

}, FoodItemList); 

export default FoodItemListContainer 

顯然,當我重構我想刪除一些這種鍋爐板,但我不知道怎麼樣我可以這樣做,因爲我沒有看到如何動態導入我需要的文件。 JS React Meteor有可能嗎?

+2

您不能動態地導入文件的一部分。您可以收集普通代碼產生它(工廠的功能,例如,接收收藏,訂閱的名稱等,並返回容器它可能會或可能不會救你打字或防止重複的功能。另一種選擇是生成代碼。 – MasterAM

回答

0

可以使用流星進行動態導入,如using require所述,但注意不建議這樣做,因爲它可能會在您的應用中導致錯誤。

我已經做了這種進口的幾次,我的代碼仍然正常工作。但我不建議你這樣做,只有當你真的需要它,它可能值得嘗試

0

我剛寫了一篇如何做到這一點,更重要的是,何時以及爲什麼這樣做的文章。

https://code.zeroasterisk.com/2017/05/meteor-1-5-bundle-optimization/

TL; DR:import('./my_component')返回一個承諾,當客戶端具有它這樣就解決了。

以前:客戶方捆

import PickDates from './PickDates'; 

的正常進口部分之後:動態導入不再是客戶方捆

import Loader from 'react-loader'; 

// generic loading component to show while transfering section of code 
const LoadingComponent =() => <span className="text-muted"><i className="fa fa-refresh" /></span>; 
// new version of the component, now: loading --> rendered 
const PickDates = Loader({ 
    // this does the dynamic import, and returns a promise 
    loader:() => import('./PickDates'), 
    // this is our generic loading display (optional) 
    LoadingComponent, 
    // this is a delay before we decide to show our LoadingComponent (optional) 
    delay: 200, 
});