我正在尝试修改以下代码,以便从 http 接收一组用户并显示所有用户。此代码仅接收和显示单个用户。
import Cycle from '@cycle/core';
import {div, button, h1, h4, a, makeDOMDriver} from '@cycle/dom';
import {makeHTTPDriver} from '@cycle/http';
function main(sources) {
const USERS_URL = 'http://jsonplaceholder.typicode.com/users/';
const getAllUsers$ = sources.DOM.select('.get-all').events('click')
.map(() => {
return {
url: USERS_URL + 1,
method: 'GET'
};
});
const user$ = sources.HTTP
.filter(res$ => res$.request.url.indexOf(USERS_URL) === 0)
.mergeAll()
.map(res => res.body)
.startWith(null);
const vtree$ = user$.map(user =>
div('.users', [
button('.get-all', 'Get all users'),
user === null ? null : div('.user-details', [
h1('.user-name', user.name),
h4('.user-email', user.email),
a('.user-website', {href: user.website}, user.website)
])
])
);
return {
DOM: vtree$,
HTTP: getAllUsers$
};
}
const drivers = {
DOM: makeDOMDriver('#app-main'),
HTTP: makeHTTPDriver(),
};
Cycle.run(main, drivers);