Assuming the UserService has correctly designed using dependency injection, it can be used the following approach with any explicit changes in production codestruct WelcomeView_Previews: PreviewProvider { static var previews: some View { let userService = UserService() userService.user = User(username: "Alex", email: "[email protected]") // << any predefined set up here // any more parameters set up here return WelcomeView().environmentObject(userService) }} 这篇关于SwiftUI - 在预览中模拟 @EnvironmentObject 数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 10-14 02:47