Swift + Xcode + macOS:非功能性预览。错误与每个 Xcode 的 .environmentObject 有关

Swift + Xcode + macOS: Non functional preview. Error is related to .environmentObject per Xcode

我正在编写一个 Swift 程序,并试图让其中一个预览工作,但没有成功。我收到的错误消息是:程序因缺少 NavigationViewModel 类型的环境而崩溃。要解决添加 .envionmentObject(NavigationViewModel(...)) 到适当的预览。我尝试了几件事但没有运气。如果有人能指出我的错误,将不胜感激。下面是我的代码。预览部分位于底部。 问候 克里斯

import SwiftUI
struct ListView: View {
    @EnvironmentObject var navVM: NavigationViewModel
    var body: some View {
        Spacer().frame(height: 30)
        VStack {
            ForEach (navVM.options, id: \.self) { option in
                switch option.id {
                case 0:
                    HomeItem(imageName: option.imageName, title: option.title, id: option.id)
                case 1, 4, 7:
                    MainItem(imageName: option.imageName, title: option.title, id: option.id)
                case 2, 3, 5, 6, 8, 9:
                    SubItem(title: option.title, id: option.id)
                default:
                    Text("Something went wrong")
                } // end switch
            } // end for each
        } // end vstack
        Spacer()
            .frame(width: 180, alignment: .leading)
    } // end var body
} // end list view
struct HomeItem: View {
    let imageName : String
    let title : String
    let id: Int
//    let currentViewId: Int
    @EnvironmentObject var navVM: NavigationViewModel
    var body: some View {
        HStack{
            Image(systemName: imageName)
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 30, height: 30, alignment: .leading)
                .foregroundColor(navVM.highLightedImage == id ? Color.blue : Color.black)
                .padding(.leading, 20 )
                .padding(.top, 10)
            Text(title)
                .font(.system(size: 15))
                .underline()
                .frame(width: 125, alignment: .leading)
                .foregroundColor(Color.blue)
                .padding(.top, 15)
                .onTapGesture {
                    navVM.updateCurrentViewId(value: id)
                    navVM.updateSelectedItem(value: 0)
                    navVM.updateHighLightedImage(value: 0)
                }
        } // end hstack
    }
}
struct MainItem: View {
    let imageName : String
    let title : String
    let id: Int
    @EnvironmentObject var navVM: NavigationViewModel
    var body: some View {
        HStack{
            Image(systemName: imageName)
                .resizable()
                .aspectRatio(contentMode: .fit)
                .frame(width: 30, height: 30, alignment: .leading)
                .foregroundColor(navVM.highLightedImage == id ? Color.blue : Color.black)
                .padding(.leading, 20 )
                .padding(.top, 10)
            Text(title)
                .font(.system(size: 15))
                .frame(width: 125, alignment: .leading)
                .padding(.top, 10)
        } // end hstack
    }
}
struct SubItem: View {
    let title: String
    //    let currentViewId : Int
    let id: Int
    @EnvironmentObject var navVM: NavigationViewModel
    var body: some View {
        HStack{
            Text(title)
                .font(.system(size: 13))
                .underline()
                .frame(width: 35, alignment: .leading)
                .foregroundColor(Color.blue)
                .padding(.bottom, 2)
                .padding(.leading, 15)
                .onTapGesture {
                    navVM.updateCurrentViewId(value: id)
                    navVM.updateSelectedItem(value: id)
                    switch navVM.selecteditem {
                    case 2, 3:
                        navVM.updateHighLightedImage(value: 1)
                    case 5, 6:
                        navVM.updateHighLightedImage(value: 4)
                    case 8, 9:
                        navVM.updateHighLightedImage(value: 7)
                    default:
                        navVM.updateHighLightedImage(value: 0)
                    }
                }
            switch navVM.currentViewId {
            case id:
                Image(systemName: "checkmark").padding(.bottom, 5).foregroundColor(Color.blue).opacity(100)
            default:
                Image(systemName: "checkmark").padding(.bottom, 5).foregroundColor(Color.blue).opacity(0)
            }
        } // end hstack
    }
}
struct ListView_Previews: PreviewProvider {
    @EnvironmentObject var navVM: NavigationViewModel
    static var previews: some View {
        ListView()
    }
//        .environmentObject(navVM: NavigationViewModel())
}

将其添加到 ListView() 而不是 previews 变量

ListView().environmentObject(navVM: NavigationViewModel())