我在我的插件中使用了这个插件,一切都很好:
edit: withAPIData( props => {
return {
roles: '/matt-watson/secure-blocks/v1/user-roles/'
};
} )( props => {……
然而,这里的指南表明,这将不再被支持:https://github.com/WordPress/gutenberg/issues/7390#issuecomment-398667802
因此,我设法将其应用到解决方案中:
const DEFAULT_STATE = {
userRoles: {},
};
const actions = {
setUserRoles( userRoles ) {
return {
type: 'SET_USER_ROLES',
userRoles,
};
},
fetchFromAPI( path ) {
return {
type: 'FETCH_FROM_API',
path,
};
},
};
registerStore( 'matt-watson/secure-block', {
reducer( state = DEFAULT_STATE, action ) {
switch ( action.type ) {
case 'SET_USER_ROLES':
return {
...state,
userRoles: action.userRoles,
};
}
return state;
},
actions,
selectors: {
getUserRoles( state ) {
const { userRoles } = state;
return userRoles;
},
},
controls: {
FETCH_FROM_API( action ) {
return apiFetch( { path: action.path } );
},
},
resolvers: {
* getUserRoles( state ) {
const path = '/matt-watson/secure-blocks/v1/user-roles/';
const userRoles = yield actions.fetchFromAPI( path );
return actions.setUserRoles( userRoles );
},
}
,
} );……
edit: withSelect( ( select ) => {
return {
roles: select('matt-watson/secure-block').getUserRoles(),
};
} )( props => {……
现在的问题是,“控件”部分永远不会触发。在文档中,它说这是一个可选择的组件,我需要通过use命令:https://github.com/WordPress/gutenberg/tree/master/packages/data#controls来启用它。
当我尝试这样做时,use是未定义的。
所以我的问题是这个。当withAPIData以一种非常快速和简单的方式完成我所需要的一切时,我是否可以使用另一种“插入”代码,或者我是否需要使用我正在注册的商店?如果是的话,我做错了什么?
任何帮助都很感激。谢谢。
发布于 2018-12-10 14:32:44
值得注意的是,上面的答案已经过时了。数据存储现在应该如下所示:
const actions = {
setUserRoles( userRoles ) {
return {
type: 'SET_USER_ROLES',
userRoles,
};
},
receiveUserRoles( path ) {
return {
type: 'RECEIVE_USER_ROLES',
path,
};
},
};
const store = registerStore( 'matt-watson/secure-block', {
reducer( state = { userRoles: {} }, action ) {
switch ( action.type ) {
case 'SET_USER_ROLES':
return {
...state,
userRoles: action.userRoles,
};
}
return state;
},
actions,
selectors: {
receiveUserRoles( state ) {
const { userRoles } = state;
return userRoles;
},
},
controls: {
RECEIVE_USER_ROLES( action ) {
return apiFetch( { path: action.path } );
},
},
resolvers: {
* receiveUserRoles( state ) {
const userRoles = yield actions.receiveUserRoles( '/matt-watson/secure-blocks/v1/user-roles/' );
return actions.setUserRoles( userRoles );
},
},
} );我更新了我的教程:https://mwatson.co.uk/working-with-gutenberg-and-the-wordpress-rest-api/
发布于 2018-08-15 19:29:56
我已经破解了!
const actions = {
setUserRoles( userRoles ) {
return {
type: 'SET_USER_ROLES',
userRoles,
};
},
receiveUserRoles( path ) {
return {
type: 'RECEIVE_USER_ROLES',
path,
};
},
};
const store = registerStore( 'matt-watson/secure-block', {
reducer( state = { userRoles: {} }, action ) {
switch ( action.type ) {
case 'SET_USER_ROLES':
return {
...state,
userRoles: action.userRoles,
};
case 'RECEIVE_USER_ROLES':
return action.userRoles;
}
return state;
},
actions,
selectors: {
receiveUserRoles( state ) {
const { userRoles } = state;
return userRoles;
},
},
resolvers: {
* receiveUserRoles( state ) {
const userRoles = apiFetch( { path: '/matt-watson/secure-blocks/v1/user-roles/' } )
.then( userRoles => {
return actions.setUserRoles( userRoles );
} )
yield userRoles;
},
},
} );这将设置userRoles的状态,并在解析后返回userRoles。
然后,您可以这样访问:
……
edit: withSelect( ( select ) => {
return {
roles: select('matt-watson/secure-block').receiveUserRoles(),
};
} )( props => {……
https://wordpress.stackexchange.com/questions/311431
复制相似问题