injectViewModel
inline fun <VM : ViewModel> injectViewModel(qualifier: Any? = null, viewModelStoreOwner: ViewModelStoreOwner = checkNotNull(LocalViewModelStoreOwner.current) {
"No ViewModelStoreOwner was provided via LocalViewModelStoreOwner"
}, key: String? = null, extras: CreationExtras = if (viewModelStoreOwner is HasDefaultViewModelProviderFactory) {
viewModelStoreOwner.defaultViewModelCreationExtras
} else {
CreationExtras.Empty
}): VM
Returns an existing ViewModel or creates a new one by Koject
@Composable
fun TopPage(
viewModel: TopViewModel = injectViewModel()
) {
/* ... */
}
Content copied to clipboard
Parameters
qualifier
Qualifier for identification.
viewModelStoreOwner
The owner of the ViewModel that controls the scope and lifetime of the returned ViewModel. Defaults to using LocalViewModelStoreOwner.
key
The key to use to identify the ViewModel.
extras
The default extras used to create the ViewModel.