Noa*_*oah 4 android android-jetpack-navigation android-jetpack-compose
如何声明具有多个导航参数的导航路线?我查了资料,并且所有 的 这些物品(这似乎只是重申文件说什么),我只能找到途径的例子有一个参数。
这是我所拥有的:
composable(
route = "createExercise/{exerciseId}",
arguments = listOf(navArgument("exerciseId") { type = NavType.IntType })
) { backStackEntry ->
CreateExerciseScreen(
exerciseId = backStackEntry.arguments!!.getInt("exerciseId"),
)
}
Run Code Online (Sandbox Code Playgroud)
这是我想要的:
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"),
)
}
Run Code Online (Sandbox Code Playgroud)
我为上面的例子随意选择了一个逗号分隔的语法来代替我正在寻找的真实语法。
所以,我的问题是:在声明导航路线时,多个参数的正确语法是什么?(可选参数呢?)
ian*_*ake 14
根据文档:
您可以将其视为通向特定目的地的隐式深层链接。
因此,它遵循与网络上任何其他隐式深层链接和 RESTful URL 约定相同的约定,通常使用 a/来分隔不同的参数以形成 URL 的路径 - 这涵盖了所需的参数:
createExercise/{exerciseId}/{workoutId}
Run Code Online (Sandbox Code Playgroud)
根据可选参数文档,必需参数的路径后面可以跟一个或多个查询参数形式的任意数量的可选参数:
createExercise/{exerciseId}/{workoutId}?setNumber={setNumber}&repNumber={repNumber}
Run Code Online (Sandbox Code Playgroud)
这是传递多个参数的详细代码
// Declaring multiple arguments placeholders
NavHost(navController, startDestination = "profile/{userId}/{username}/{address}") { // or startDestination = "home"
composable(
"profile/{userId}/{username}/{address}", // declaring placeholder in String route
arguments = listOf( // declaring argument type
navArgument("userId") { type = NavType.IntType },
navArgument("username") { type = NavType.StringType },
navArgument("address") { type = NavType.StringType }
)
) { backStackEntry ->
// Extracting exact values and passing it to Profile() screen
val userId = backStackEntry.arguments?.getInt("userId")
val username = backStackEntry.arguments?.getString("username")
val address = backStackEntry.arguments?.getString("address")
Profile(navController, userId, username, address)
}
}
Run Code Online (Sandbox Code Playgroud)
要在执行导航时传递精确值,请使用此代码
navController.navigate("profile/24/sriyanksid/India")
// userId = 24, username = "sriyanksid", address = "India"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
907 次 |
| 最近记录: |