Angular 6 变更检测 - 如何设置 child 组件?
Angular 6 Change detection - how to setup the child component?
我的 parent angular 组件有一个嵌套的 child 组件。 child 组件是一个按钮列表。单击一个按钮时,一个值将传递给 child 组件。然后 child 组件相应地更新为一个 id。我已经对 child 实施了一些更改检测。当 child 注册来自 parent 的更新时,它会运行 ngOnChanges 挂钩。在这里我调用我的后端和数据 returns
我的代码运行良好,但似乎有问题。如您在以下代码中所见,我分离了 changeDetection object。在 ngOnChanges - 在订阅部分,我再次重新附加 CD。我不喜欢
你们能给我一些建议吗?
@Component({
selector: 'app-challenge',
templateUrl: './challenge.component.html',
styleUrls: ['./challenge.component.css']
})
export class ChallengeComponent implements OnInit {
@Input() focusedChallenge: string;
eventId = {};
challenge: Challenge;
userSelection;
constructor(
public snackBar: MatSnackBar,
private eventService: EventService,
private activeRoute: ActivatedRoute,
private cd: ChangeDetectorRef
) {
this.cd.detach()
}
ngOnInit() {
this.eventId = this.activeRoute.snapshot.params.id;
}
ngOnChanges() {
this.eventService.getChallenge(this.focusedChallenge)
.subscribe(
res => {
this.cd.reattach();
console.log(res)
this.challenge = res
},
err => { console.log(err) }
)
challengeSelected.currentValue.toUpperCase();
}
响应回答更新
它确实通过 ngOnChanges(changes: SimpleChanges) 给了我想要的结果。但它仍然给我们一个错误。说是undefined.
选项数组是挑战中的嵌套数组
从 db
返回的 object
{_id: "5b86dc5bfb6fc03893e55001", shortEventId: "d2d3", organization: "Brædstrup", name: "1. december", winner: "", …}
born: "1. decemberplus andet"
endDate: "2018-10-06T23:59:00.000Z"
id: "5b86dc5bfb6fc03893e55001"
name: "1. december"
options: Array(4)
0: {name: "Matas", isCorrect: true, description: "Matas er den førende skib", image: "https://cityxpstorage.blob.core.windows.net/images/Matas.png", id: null}
1: {name: "Føtex", isCorrect: false, description: "Føtex er en dejlig butik", image: "https://cityxpstorage.blob.core.windows.net/images/Føtex.png", id: null, …}
2: {name: "Kvickly", isCorrect: false, description: "Kvickly er en dejlig butik", image: "https://cityxpstorage.blob.core.windows.net/images/Matas.png", id: null, …}
3: {name: "MC Jørgensen", isCorrect: false, description: "MC Jørgensen er en dejlig butik", image: "https://cityxpstorage.blob.core.windows.net/images/Matas.png", id: null}
length: 4
__proto__: Array(0)
organization: "Brædstrup"
shortEventId: "d2d3"
startDate: "2018-10-06T00:00:00.000Z"
winner: ""
_id: "5b86dc5bfb6fc03893e55001"
__proto__: Object
由于您使用的是字符串类型输入,因此不需要捕获任何 ChangeDetection。所以最好你删除 ChangeDetectorRef
并让 angular 在 @Input
值发生变化时触发 ngOnChanges
方法。
您的代码应为 -
@Component({
selector: 'app-challenge',
templateUrl: './challenge.component.html',
styleUrls: ['./challenge.component.css']
})
export class ChallengeComponent implements OnInit {
@Input() focusedChallenge: string;
eventId = {};
challenge: Challenge;
userSelection;
constructor(
public snackBar: MatSnackBar,
private eventService: EventService,
private activeRoute: ActivatedRoute
) {
}
ngOnInit() {
this.eventId = this.activeRoute.snapshot.params.id;
}
ngOnChanges(changes: SimpleChanges) {
let newFocusedChallenge = changes["focusedChallenge"].currentValue;
this.eventService.getChallenge(newFocusedChallenge)
.subscribe(
res => {
console.log(res)
this.challenge = res;
//challengeSelected.currentValue.toUpperCase(); //not sure if is required
},
err => { console.log(err) }
)
}
}
我的 parent angular 组件有一个嵌套的 child 组件。 child 组件是一个按钮列表。单击一个按钮时,一个值将传递给 child 组件。然后 child 组件相应地更新为一个 id。我已经对 child 实施了一些更改检测。当 child 注册来自 parent 的更新时,它会运行 ngOnChanges 挂钩。在这里我调用我的后端和数据 returns
我的代码运行良好,但似乎有问题。如您在以下代码中所见,我分离了 changeDetection object。在 ngOnChanges - 在订阅部分,我再次重新附加 CD。我不喜欢
你们能给我一些建议吗?
@Component({
selector: 'app-challenge',
templateUrl: './challenge.component.html',
styleUrls: ['./challenge.component.css']
})
export class ChallengeComponent implements OnInit {
@Input() focusedChallenge: string;
eventId = {};
challenge: Challenge;
userSelection;
constructor(
public snackBar: MatSnackBar,
private eventService: EventService,
private activeRoute: ActivatedRoute,
private cd: ChangeDetectorRef
) {
this.cd.detach()
}
ngOnInit() {
this.eventId = this.activeRoute.snapshot.params.id;
}
ngOnChanges() {
this.eventService.getChallenge(this.focusedChallenge)
.subscribe(
res => {
this.cd.reattach();
console.log(res)
this.challenge = res
},
err => { console.log(err) }
)
challengeSelected.currentValue.toUpperCase();
}
响应回答更新
它确实通过 ngOnChanges(changes: SimpleChanges) 给了我想要的结果。但它仍然给我们一个错误。说是undefined.
选项数组是挑战中的嵌套数组
从 db
返回的 object{_id: "5b86dc5bfb6fc03893e55001", shortEventId: "d2d3", organization: "Brædstrup", name: "1. december", winner: "", …}
born: "1. decemberplus andet"
endDate: "2018-10-06T23:59:00.000Z"
id: "5b86dc5bfb6fc03893e55001"
name: "1. december"
options: Array(4)
0: {name: "Matas", isCorrect: true, description: "Matas er den førende skib", image: "https://cityxpstorage.blob.core.windows.net/images/Matas.png", id: null}
1: {name: "Føtex", isCorrect: false, description: "Føtex er en dejlig butik", image: "https://cityxpstorage.blob.core.windows.net/images/Føtex.png", id: null, …}
2: {name: "Kvickly", isCorrect: false, description: "Kvickly er en dejlig butik", image: "https://cityxpstorage.blob.core.windows.net/images/Matas.png", id: null, …}
3: {name: "MC Jørgensen", isCorrect: false, description: "MC Jørgensen er en dejlig butik", image: "https://cityxpstorage.blob.core.windows.net/images/Matas.png", id: null}
length: 4
__proto__: Array(0)
organization: "Brædstrup"
shortEventId: "d2d3"
startDate: "2018-10-06T00:00:00.000Z"
winner: ""
_id: "5b86dc5bfb6fc03893e55001"
__proto__: Object
由于您使用的是字符串类型输入,因此不需要捕获任何 ChangeDetection。所以最好你删除 ChangeDetectorRef
并让 angular 在 @Input
值发生变化时触发 ngOnChanges
方法。
您的代码应为 -
@Component({
selector: 'app-challenge',
templateUrl: './challenge.component.html',
styleUrls: ['./challenge.component.css']
})
export class ChallengeComponent implements OnInit {
@Input() focusedChallenge: string;
eventId = {};
challenge: Challenge;
userSelection;
constructor(
public snackBar: MatSnackBar,
private eventService: EventService,
private activeRoute: ActivatedRoute
) {
}
ngOnInit() {
this.eventId = this.activeRoute.snapshot.params.id;
}
ngOnChanges(changes: SimpleChanges) {
let newFocusedChallenge = changes["focusedChallenge"].currentValue;
this.eventService.getChallenge(newFocusedChallenge)
.subscribe(
res => {
console.log(res)
this.challenge = res;
//challengeSelected.currentValue.toUpperCase(); //not sure if is required
},
err => { console.log(err) }
)
}
}