在 ObservableObject 中引用 EnvironmentObject
Reference EnvironmentObject in ObservableObject
我有一个 LoginView,如果用户未登录则显示 RegisterView,如果用户已登录则显示 ContentView:
struct LoginView: View {
@EnvironmentObject var userManager: UserManager
var body: some View {
Group {
if userManager.isRegistered {
ContentView()
} else {
RegisterView()
}
}
}
}
ContentView
有三个 ObservedObject
属性,使用 combine 从服务器获取内容,其余 api。
struct ContentView: View {
@EnvironmentObject var userManager: UserManager
@ObservedObject var usersStore = UsersStore()
@ObservedObject var rolesStore = RolesStore()
@ObservedObject var elementsStore = ElementsStore()
var body: some View {
NavigationView {
ZStack {
Image("stell")
.resizable()
.aspectRatio(contentMode: .fit)
.opacity(0.1)
VStack(alignment: .leading, spacing: 5) {
NavigationLink(destination: UsersView(usersStore: usersStore) ) {
Text("Users")
}
NavigationLink(destination: RolesView(rolesStore: rolesStore)) {
Text("Roles")
}
NavigationLink(destination: ElementsView(elements: $elementsStore.elements)) {
Text("Elements")
}
}.font(.title).padding(20)
}.navigationBarTitle(Text("STELL"))
}
}
}
我遇到的问题是我想从任何观察到的对象中引用 userManager
,例如当其余 api 的 returns 401 Unauthorized
会话令牌已过期时。然后我希望 ObservedObject 将 userManager
中的 isRegistered
标志设置为 false
,以便自动显示 RegisterView
。但是我该怎么做呢?我无法在任何 ObservedObject 属性 初始化器中设置对 userManager
的引用,因为编译器抱怨 属性 初始化器在 self
可用之前是 运行 .
在这种情况下,我会通过构造函数使用依赖注入...下面显示了 UsersStore
示例的可能方法,对于其他示例,它是相同的
UsersStore
的变化
class UsersStore: ObservableObject {
var manager: UserManager
init(manager: UserManager) { // << inject UserManager via constructor
self.manager = manager
}
...
}
ContentView
的变化
struct ContentView: View {
@EnvironmentObject var userManager: UserManager
@ObservedObject var usersStore: UsersStore
init(usersStore: UsersStore) {
self.usersStore = usersStore // << inject UsersStore via contructor
}
用法变化
struct LoginView: View {
@EnvironmentObject var userManager: UserManager
var body: some View {
Group {
if userManager.isRegistered {
// userManager is valid here some UsersStore can be created
ContentView(usersStore: UsersStore(manager: self.userManager))
我有一个 LoginView,如果用户未登录则显示 RegisterView,如果用户已登录则显示 ContentView:
struct LoginView: View {
@EnvironmentObject var userManager: UserManager
var body: some View {
Group {
if userManager.isRegistered {
ContentView()
} else {
RegisterView()
}
}
}
}
ContentView
有三个 ObservedObject
属性,使用 combine 从服务器获取内容,其余 api。
struct ContentView: View {
@EnvironmentObject var userManager: UserManager
@ObservedObject var usersStore = UsersStore()
@ObservedObject var rolesStore = RolesStore()
@ObservedObject var elementsStore = ElementsStore()
var body: some View {
NavigationView {
ZStack {
Image("stell")
.resizable()
.aspectRatio(contentMode: .fit)
.opacity(0.1)
VStack(alignment: .leading, spacing: 5) {
NavigationLink(destination: UsersView(usersStore: usersStore) ) {
Text("Users")
}
NavigationLink(destination: RolesView(rolesStore: rolesStore)) {
Text("Roles")
}
NavigationLink(destination: ElementsView(elements: $elementsStore.elements)) {
Text("Elements")
}
}.font(.title).padding(20)
}.navigationBarTitle(Text("STELL"))
}
}
}
我遇到的问题是我想从任何观察到的对象中引用 userManager
,例如当其余 api 的 returns 401 Unauthorized
会话令牌已过期时。然后我希望 ObservedObject 将 userManager
中的 isRegistered
标志设置为 false
,以便自动显示 RegisterView
。但是我该怎么做呢?我无法在任何 ObservedObject 属性 初始化器中设置对 userManager
的引用,因为编译器抱怨 属性 初始化器在 self
可用之前是 运行 .
在这种情况下,我会通过构造函数使用依赖注入...下面显示了 UsersStore
示例的可能方法,对于其他示例,它是相同的
UsersStore
class UsersStore: ObservableObject {
var manager: UserManager
init(manager: UserManager) { // << inject UserManager via constructor
self.manager = manager
}
...
}
ContentView
struct ContentView: View {
@EnvironmentObject var userManager: UserManager
@ObservedObject var usersStore: UsersStore
init(usersStore: UsersStore) {
self.usersStore = usersStore // << inject UsersStore via contructor
}
用法变化
struct LoginView: View {
@EnvironmentObject var userManager: UserManager
var body: some View {
Group {
if userManager.isRegistered {
// userManager is valid here some UsersStore can be created
ContentView(usersStore: UsersStore(manager: self.userManager))