在 intanciate 上传递嵌套对象 class
Pass nested object on intanciate class
当我尝试在新 Class() 上传递嵌套对象时,它 returns 为空 class。
我想是因为我传递了错误的论点。
class myClass {
public selected: {
category: {
code: string
}
} = {}
}
const initialState = {
selected: {
category: {
code: ''
}
}
}
private bs: BehaviorSubject<any> = new BehaviorSubject(initialState)
这很好,我得到了我的Class值。
但是当我想在之后传递值时:
setCategory (myCategory) {
let value = this.bs.getValue();
let newValue = Object.assign({}, value, {
selected: {
category:{
code: myCategory
}
}
});
new myClass(newValue) // return { view: {}, selected: { category: { code: '' } } }
}
我得到{ view: {}, selected: { category: { code: '' } } }
更新
实时代码:https://stackblitz.com/edit/angular-bjlhus?file=app%2Fapp.component.ts
您需要将构造函数添加到您的 class 以将输入参数分配给 selected
属性:
constructor(state) {
this.selected = state.selected;
}
这是带有修复的 stackblitz:https://stackblitz.com/edit/angular-hokh2a
当我尝试在新 Class() 上传递嵌套对象时,它 returns 为空 class。
我想是因为我传递了错误的论点。
class myClass {
public selected: {
category: {
code: string
}
} = {}
}
const initialState = {
selected: {
category: {
code: ''
}
}
}
private bs: BehaviorSubject<any> = new BehaviorSubject(initialState)
这很好,我得到了我的Class值。
但是当我想在之后传递值时:
setCategory (myCategory) {
let value = this.bs.getValue();
let newValue = Object.assign({}, value, {
selected: {
category:{
code: myCategory
}
}
});
new myClass(newValue) // return { view: {}, selected: { category: { code: '' } } }
}
我得到{ view: {}, selected: { category: { code: '' } } }
更新
实时代码:https://stackblitz.com/edit/angular-bjlhus?file=app%2Fapp.component.ts
您需要将构造函数添加到您的 class 以将输入参数分配给 selected
属性:
constructor(state) {
this.selected = state.selected;
}
这是带有修复的 stackblitz:https://stackblitz.com/edit/angular-hokh2a