ES6将一些函数作为对象导入

时间:2017-08-20 03:20:59

标签: javascript node.js reactjs ecmascript-6 redux

actions.js

export const setX = () => {...}
export const setY = () => {...}
export const setT = () => {...}

somecomponent.js

import {setX, setY, setT} from 'actions'

export class somecomponent extends React.Component
{
   constructor(props,context)
   {
      super(props)
      this.state={
          X,
          Y,
          T
      }
   }


componentWillMount()
{
   let reduxstate = this.store.getState()
    Object.keys(this.state).forEach(n => {
      let fn = n + '-Changed';
      this[fn] = evt => {
        let update = {};
        update[n] = evt.target.value;
        this.setState(update);
        RETRIEVEDFUNCTION = ****//How to retrieve the imported actions setX,setY and setT by name****
        this.store.dispatch(RETRIEVEDFUNCTION(evt.target.value))
      }
      this.state[n] = reduxstate[n]
    });
}

所有导入的函数是否都在全局“窗口”中。我无法找到导入的函数来按名称访问它们

allimportedfunction['set'+n ](evt.target.value)
window['set'+n](evt.target.value)

是否可以将导入的函数添加到对象

import {setX, setY, setT} as actionCreators from 'actions'
actionCreators['set'+n ](evt.target.value)

从'actions'导入*作为actionCreators - >这有效,但我不想导入所有函数

3 个答案:

答案 0 :(得分:2)

你不能这样做。

但你可以将它们放在一个物体中:

import {setX, setY, setT} from 'actions'
const actionCreators = {setX, setY, setT};

答案 1 :(得分:1)

MDN有一个很好的overview of all forms of import statements

  

语法

import defaultMember from "module-name";
import * as name from "module-name";
import { member } from "module-name";
import { member as alias } from "module-name";
import { member1 , member2 } from "module-name";
import { member1 , member2 as alias2 , [...] } from "module-name";
import defaultMember, { member [ , [...] ] } from "module-name";
import defaultMember, * as name from "module-name";
import "module-name";

所以,不,仅凭import无法实现。

答案 2 :(得分:0)

如果您可以启用webpack提供的树形图,请执行以下操作

action.js

var db = require('../controllers/db');
var mongo = require('mongodb').MongoClient();

var url = "mongodb://localhost:27017/blog";
var ObjectId = require('mongodb').ObjectID;


//#1: this function lists all posts in my blog
const list = function(req, res){
    db.find('post', {}, 10, {timeCreated: 1})
    .then(posts => {
        var promises = posts.map(post =>
            getPostAuthor(post.author)
            .then(author => author /* this value doesn't go to promises*/)
        );
        console.log(promises); //printed [Promise {<pending>}, ...]
    })
}

//#2: this function gets authorId and gives authorName
const getPostAuthor = function(authorId){
    return db.findOne('user', {_id: new ObjectId(authorId)})
    .then(author => author.name);
}

//#3: finds from db
const find = function(collection, cond = {}, limit = 0, sort = {}){
        return mongo.connect(url)
        .then(db =>
            db.collection(collection)
            .find(cond)
            .limit(limit)
            .sort(sort)
            .toArray()
        )
}


//#4: finds one from db
const findOne = function(collection, cond = {}){
        return mongo.connect(url)
        .then(db =>
            db.collection(collection).findOne(cond)
        )
}


list();

somecomponent.js

export const setX = () => {...}
export const setY = () => {...}
export const setT = () => {...}
export default { setX, setY, setT }