标签:DvaJS users userId dispatch 使用 path history match Subscription
简介:
subscriptions是订阅,用于订阅一个数据源,然后根据需要dispatch相应的action。数据源可以是当前的时间、服务器的websocket连接、keyboard输入、geolocation变化、history路由变化等等。格式为({ dispatch, history }) => unsubscribe。
subscripition的用法:
异步数据初始化:
比如:当用户进入 /users 页面时,触发action users/fetch 加载用户数据。
app.model({
subscriptions: {
setup({ dispatch, history }) {
history.listen(({ pathname }) => {
if (pathname === '/users') {
dispatch({
type: 'users/fetch',
});
}
})
}
}
})
拓展:
path-to-regexp Package |
如果url规则比较复杂,比如: /users/:userId/search ,那么匹配和userId的获取都会比较麻烦。这时推荐使用path-to-regexp简化这部分逻辑。
import pathToRegexp from 'path-to-regexp';
// in subscription
const match = pathToRegexp('/users/:userId/search').exec(pathname);
if (match) {
const userId = match[1];
// dispatch action with userId
}
标签:DvaJS,users,userId,dispatch,使用,path,history,match,Subscription 来源: https://blog.csdn.net/weixin_43606158/article/details/98475142
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。