2017-05-04 70 views
0

这看起来微不足道,但我找不到任何文档。如何将我的应用程序分为多个模块?

我想将我的(非常基本的)应用分成UI和非UI模块。所以我创造了这个:

/* datahandler.js */ 

export class DataHandler {                   

    getSuppliers(search,limit,offset) {                 
    let params = { method: 'GET',                  
        headers: { 'Accept': 'application/json' },           
        body: null };                  

    let url = 'http://url.for.data.service.goes.here';         

    return fetch(`${url}?params=${search},${limit},${offset}`, params)        
     .then((response) => response.json())               
     .catch((error) => { console.error("fetch caught:" + error); });        

    }                         

}  

但我不知道在哪里把模块,以便它被识别。如果我把它放在顶层,并与import {DataHandler} from 'datahandler';调用它,然后我得到:

error: bundling: UnableToResolveError: Unable to resolve module `datahandler` from `/home/andy/andy/try_react/try1/index.android.js`: Module does not exist in the module map or in these directories: 
    /home/andy/andy/try_react/try1/node_modules 

This might be related to https://github.com/facebook/react-native/issues/4968 
To resolve try the following: 
    1. Clear watchman watches: `watchman watch-del-all`. 
    2. Delete the `node_modules` folder: `rm -rf node_modules && npm install`. 
    3. Reset packager cache: `rm -fr $TMPDIR/react-*` or `npm start --reset-cache`. 
    at UnableToResolveError (/home/andy/andy/try_react/try1/node_modules/react-native/packager/src/node-haste/DependencyGraph/ResolutionRequest.js:488:5) 
    at p.catch.error (/home/andy/andy/try_react/try1/node_modules/react-native/packager/src/node-haste/DependencyGraph/ResolutionRequest.js:366:19) 
    at process._tickCallback (internal/process/next_tick.js:109:7) 
Bundling `index.android.js` 0.0% (0/1), failed. 

好像我可以放下文件到node_modules目录,但我不想这样做。是否有某种模块路径或文件清单可供包含,但我没有在文档中看到?


顺便我也尝试react-native new-library --name="datahandler",但它所作的只是建立一个图书馆的目录,而不是承诺的模板(一整个图书馆吗?我敢肯定,我不想说,反正)。

回答

0

这只是

import (DataHandler} from './datandler'; 

我错过了./

但是:我仍然无法找到任何地方这是记录。

0

通常情况下,您只需使用文件的路径来抓取自己的类。

rootDir 
.. pages 
... page.jsx 
.. modules 
... datahandler.js 

你的模块将看起来像

export default class DataHandler { 
... 
} 

,并在页面

import DataHandler from '../modules/datahandler' 
相关问题