I'm having trouble trying to understand the hierarchy using SwiftUI's @EnvironmentObject wrapper. I've got a ViewModel that needs to be accessed in multiple screens within the app so I've put this code inside the main app file:
var chartsModel = ChartsViewModel()
Inside the ContentView file generated by Xcode on a new project I have put this inside the ContentView_Previews
ContentView().environmentObject(ChartsViewModel())
Where I'm now getting confused is I have a view 2 levels deep inside ContentView that needs data from the ViewModel.
ContentView()->HomeView()->ChartView()
ChartView is a child of HomeView()
I can get ChartView to display the data when in preview mode by using the following code.
@EnvironmentObject var viewModel: ChartsViewModel
The problem is when previewing HomeView, ContentView or running the app on a device nothing is outputted.