角度:两个不同的模块或单个模块 [英] Angular: two different modules or single module
问题描述
我最近开始研究Angular(Angular5
).我的应用程序具有名为Home, School, Office
的选项卡,并且我有两种类型的用户Admin
和non-admin
.单击Office
选项卡后,管理"页面将被重定向到管理"页面,非管理用户将被重定向到非管理"页面.
I recently started working on Angular (Angular5
). My application has tabs named Home, School, Office
and I have two types of users Admin
and non-admin
. As soon as the Office
tab is clicked Admin page will be redirected to Admin page and non-admin user will be redirected to non-admin page.
那么,为此要求我需要制作两个不同的模块吗?还是我应该制作一个模块并根据用户类型进行重定向?请用项目结构指导我.
So, for this requirement do I need to make two different modules? OR shall I make a single module and redirect on the bases of user types? Please guide me with the project structure.
推荐答案
模块不按应用程序的角色进行分隔-模块分隔是指在没有component
的情况下,不基于复杂度分隔代码的过程.需要在多个地方使用的组件,您可以将该组件移至shared
模块,然后将该模块移至所有子模块中import
-如果您要懒惰地装载模块,则可以懒惰地装载模块
Modules are not separated by roles of your application - Separation of modules is the process of separating your code based on no complexity if in case you have a component
that need to be used in multiple places you can move that component to a shared
module and import
that module into all your sub modules - if you want to load a module Lazily you can work on loading a module Lazily
在您的情况下-单词对您来说是route
-您可以在路由时检查用户是否具有admin
或non-admin
,这可以通过使用对您有帮助的Resolver
来实现-选中此选项链接以进行进一步的查询
In your case - the word is route
for you - you can check whether the user has admin
or non-admin
while routing, that can be achieved by using Resolver
which does magic for you - check this link for further queries
尝试类似的方法
路由器
{
path: 'pathName',
component: ComponentName,
resolve: { RolePermission: RolePermissionService },
}
RolePermissionService
@Injectable({
providedIn: 'root'
})
export class RolePermissionService implements Resolve<object | string> {
constructor(private router: Router) {
}
resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable<boolean | string> | Promise<boolean | string> {
return new Promise(resolve => {
return resolve(true);
// Your logic here - Call the service to check whether admin or non-admin
// Bind the data in your route or you can navigate from here
});
}
}
因此返回数据将绑定到路径中的RolePermission
对象,最后您可以在component
So the return data will bind to the RolePermission
object in your route and finally you can read it in your component
组件
在您的组件中,您可以像这样读取数据
In you component you can read that data like this
this._activatedRoute.snapshot.data["RolePermission"].ISSubmitSlotRequest;
因此这将解决您的问题,您可以将此服务映射到所需的所有路由,并且可以获取用户的角色-我并不反对创建模块,这将是在路由中读取角色的更好方法-检查该链接以获取更多想法-快乐编码:)
So this will solve your problem you can map this service to all the routes you want and you can get the role of the user - I'm not against module creation this will be a better way to read the roles on routing - check that link for more idea - Happy coding :)
这篇关于角度:两个不同的模块或单个模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!