美文网首页
dva页面获取并循环遍历对象数组

dva页面获取并循环遍历对象数组

作者: BscLiao | 来源:发表于2018-01-10 22:34 被阅读0次

    首先在model层初始化函数里用service层已写好的接口

        subscriptions: {
            setup({ dispatch, history }) {
                history.listen((location) => {
                    if (location.pathname === '/forum') {
                        dispatch({
                            type: 'query',
                            payload: location.query || {},
                        })
                    }
                })
            },
        },
    
     effects: {
            *query({ payload = {} }, { call, put }) {
                let plateList = yield call(plate.query, payload)
                let { success, message, status, data } = plateList
                if (success) {
                    yield put({
                        type: 'getPlate',
                        payload: data.data,
                    })
                } else {
                    throw plateList
                }
    
                const articleList = yield call(article.query, payload)
                success = articleList.success
                data  =articleList.data
                if (success) {
                    yield put({
                        type:'getArticle',
                        payload:data.data
                    })
                } else {
                    throw articleList
                }
            },
    }
    

    然后在view层connect获取并处理数组对象数据

    方法一:
    //使用将元素插入数组的方法
    const Forum = ({ forum }) => {
        let { plateList, articleList } = forum
        var plateNameList = []
        for (let x in plateList) {
         //采用动态增加html元素的方法增加
            plateNameList.push(<div key={x} className={styles.side_list}>
                <br /><p>{plateList[x].plateName}</p>
            </div>)
        }
    
        return (
            <div className={styles.forum}>
                        {plateNameList}
                    </div>
            </div>
        )
    }
    export default connect(({ loading, forum }) => ({ loading, forum }))(Forum)
    
    方法二:
    //直接使用  _.map(dataSource, function (item) { })函数
       <TabPane tab="个人文章" key="1">
                {
                  _.map(dataSource, function (item) {
                    return (
                      <Row>
                        <Col>
                          <Card
                            key={item.id}
                            className={styles.card}
                            type="inner"
                            title="来自板块"
                            extra={[
                              <IconText text="删除" />,
                              <IconText text="只自己可见" />,]}
                          >
                            <div className={styles.articleName}>
                              <h1>{item.articleName}</h1>
                            </div>
                            <div className="text-card">
                              <div className={styles.imageRight}>
                              <Avatar shape="square" size="large" src={image} />
                              </div>
                              <h3>{userName}</h3>
                              <p>{description}</p>
                            </div>
                            <div className={styles.content} dangerouslySetInnerHTML={{ __html: item.content }}></div>
                          </Card>
                        </Col>
                      </Row>
    
                    )
                  })
                }
              </TabPane>
    
    

    页面效果如下(虚拟效果,按数组数量自动增长的表):

    dog bird cat
    yun foo foo
    bar bar bar
    baz baz baz

    相关文章

      网友评论

          本文标题:dva页面获取并循环遍历对象数组

          本文链接:https://www.haomeiwen.com/subject/rwuanxtx.html