I actually came across this same issue today trying to inject a Service into my Livewire component. This actually resolved my issue but I'd be wondering the same thing. Is this best practice and expected? I couldn't find anything about Service injection in the Livewire docs.
Jan 20, 2025
1
Level 1
Inject service using livewire boot() method
Hello, I'm trying to inject a service into a Livewire component, but when I use constructor injection or the mount method, I encounter the following error:
$apiKeyService must not be accessed before initialization
To resolve this, I switched to using the boot method to inject the service, and it seems to work fine.
My questions are:
Is using the boot method for service injection a good practice in Livewire components?
Will this approach cause any issues like memory leaks, or is it safe in terms of performance and lifecycle management?
protected ApiKeyService $apiKeyService;
public function boot(ApiKeyService $apiKeyService)
{
$this->apiKeyService = $apiKeyService;
}
Please or to participate in this conversation.