Hi. How do I create viewModel scoped to a sub grap...
# koin
g
Hi. How do I create viewModel scoped to a sub graph in KMP project?
I have something like
Copy code
@Composable
fun NavGraphBuilder.singUpFlowScreen(navController: NavHostController) {
    navigation<Screens.SignupFlow>(startDestination = Screens.SignupFlow) {
        val viewModel = koinViewModel<ProfileViewModel>()
        composable<SignupScreens.CredentialsScreen> {
            Surface(color = MaterialTheme.colorScheme.background) {
                SignupScreen(onBackClick = { navController.navigateUp() })
            }
        }
        composable<SignupScreens.OtpScreen> {
            Surface(color = MaterialTheme.colorScheme.background) {
                SignupScreen(onBackClick = { navController.navigateUp() })
            }
        }
        composable<SignupScreens.SuccessScreen> {
            Surface(color = MaterialTheme.colorScheme.background) {
                SignupScreen(onBackClick = { navController.navigateUp() })
            }
        }
    }
}
but shows error at
val viewModel = koinViewModel<ProfileViewModel>()
Copy code
@Composable invocations can only happen from the context of a @Composable function
a
I think that the the viewModel should be called either within
composable {}
either outside of
navigation<>() {}
.
g
umm, I was looking for a way to scope ViewModel to the subgraph and share it between 4 screens...
if I do outside, then it will stay alive forever
a
You can have other NavHost such as: MasterNav { • SignupNav (that'll correspond to the code that you've shared) • Feature1Nav } The viewModel will then be scoped within the SignupNav.