喷气背包组合导航的多个参数
如何声明具有多个导航参数的导航路线?我查了资料,并且所有 的 这些物品(这似乎只是重申文件说什么),我只能找到途径的例子有一个参数。
这是我所拥有的:
composable(
route = "createExercise/{exerciseId}",
arguments = listOf(navArgument("exerciseId") { type = NavType.IntType })
) { backStackEntry ->
CreateExerciseScreen(
exerciseId = backStackEntry.arguments!!.getInt("exerciseId"),
)
}
这是我想要的:
composable(
route = "createExercise/{exerciseId},{workoutId}",
arguments = listOf(
navArgument("exerciseId") { type = NavType.IntType },
navArgument("workoutId") { type = NavType.IntType },
)
) { backStackEntry ->
CreateExerciseScreen(
exerciseId = backStackEntry.arguments!!.getInt("exerciseId"),
workoutId = backStackEntry.arguments!!.getInt("workoutId"),
)
}
我为上面的例子随意选择了一个逗号分隔的语法来代替我正在寻找的真实语法。
所以,我的问题是:在声明导航路线时,多个参数的正确语法是什么?(可选参数呢?)
回答
根据文档:
您可以将其视为通向特定目的地的隐式深层链接。
因此,它遵循与网络上任何其他隐式深层链接和 RESTful URL 约定相同的约定,通常使用 a/来分隔不同的参数以形成 URL 的路径 - 这涵盖了所需的参数:
createExercise/{exerciseId}/{workoutId}
根据可选参数文档,必需参数的路径后面可以跟一个或多个查询参数形式的任意数量的可选参数:
createExercise/{exerciseId}/{workoutId}?setNumber={setNumber}&repNumber={repNumber}
- +1 To be fair, while the use of `&` to separate multiple optional arguments is quite natural, this usage doesn't seem to actually be shown anywhere in the official docs.