如果您还想通过不同的列表菜单将 CRUD 功能添加到同一路线,上述答案并不是那么有用。如果您有 2 个带有 CRUD 组件的列表视图 List1 和 List2。从 List2 输入编辑(例如)并点击保存,您将被重定向到 List1
更广泛的解决方案是为您的 REST 客户端创建自定义包装器。灵感来自下方。
https://marmelab.com/admin-on-rest/RestClients.html#decorating-your-rest-client-example-of-file-upload
就我而言,它看起来像这样。
我在 App.js 中创建了一个虚拟资源“trackTale”。restWrapper.js 中的
const RESTWrapper = requestHandler => (type, resource, params) => {
if (type === 'GET_LIST' && resource === 'trackTale') {
const page = params.pagination.page
const perPage = params.pagination.perPage
const {field, order} = params.sort
const query = {}
query['where'] = {...params.filter}
if (field) {query['order'] = [field + ' ' + order]}
if (perPage > 0) {
query['limit'] = perPage;
if (page >= 0) {
query['skip'] = (page - 1) * perPage
}
}
//Key part here hardcoding the tales route to trackTale
const url = config.host + '/' + 'tales?' + queryParameters({filter: JSON.stringify(query)})
const options = {};
options.user = {authenticated: true}
options.user.token = localStorage.getItem('token');
options.method = 'GET';
return fetchUtils.fetchJson(url, options)
.then((response) => {
const {headers, json} = response;
//admin on rest needs the {data} key
return {data: json,
total: parseInt(headers.get('x-total-count').split('/').pop(), 10)}
})
}
}
//below function is very very specific to how loopback.js expects to recieve REST queries. Please customize it for your API needs
const queryParameters = data => Object.keys(data)
.map(key => [key, data[key]].map(encodeURIComponent).join('='))
.join('&');
这适用于所有情况。如果您的不同路线没有 CRUD,仍然可以创建自定义菜单。