0

我在桌面应用程序项目中使用 JetBrains Compose 框架,并且对于路由,他们在官方文档arkivanov-Decompose库中建议用于视图之间的路由(可组合)。

就像一个魅力,但你拥有的视图越多,你的路由文件就越长。我想知道我是否可以让它看起来更好一点。

当我们可以在模块内定义路由时,我只熟悉 Angular 中的 Web 路由。在那里,每个模块都可以有一个module-routes.ts文件,如下所示:

const routes: Routes = [
  { path: 'first-component', component: FirstComponent },
  { path: 'second-component', component: SecondComponent },
];

这样我就可以在模块内部管理与模块相关的所有元素,并将路由导入到全局路由器模块中。

在分解中,我试图按照这些思路做一些事情,所以我可以将某些视图封装在它们各自的模块中(一些视图只与同一模块的视图交互,但我很难让我的路由器分布在它们之间模块。有没有人知道怎么做?

我有我的路由器和我的孩子:

private val router =
    router<Configuration, Content>(
        initialConfiguration = Configuration.Auth, // Starting with Login
        childFactory = ::createChild // The Router calls this function, providing the child Configuration and ComponentContext
    )

和我的孩子工厂:

private fun createChild(configuration: Configuration, context: ComponentContext): Content =
    when (configuration) {
        is Configuration.Auth -> auth(configuration)
        is Configuration.UserList -> userList()
        is Configuration.NewUser-> newUser()

我可以从模块中获取这些配置以使其更清洁吗?我可以为不同类型的用户(管理员、普通用户等)设置不同的路由吗?

4

1 回答 1

1

我可以从模块中获取这些配置以使其更清洁吗?

是的,您可以将Configuration密封类及其所有子类移动到一个单独的文件中,并将其设为internal. 您也可以移动该createChild功能。

我可以为不同类型的用户(管理员、普通用户等)设置不同的路由吗?

您可以为每个路由定义单独的配置类。

PS:还请注意,您可以将组件组织成树。因此,在每个组件上,您将只有几条路线和有限的责任。

像这样的东西:

              Root
          /          \
     Auth              Main
    /    \            /    \
SignIn  SignUp  UserList  UserProfile
于 2022-01-27T21:37:36.257 回答