開発者は、複数の Lightning Web コンポーネントを含むアプリを作成しています。 子コンポーネントの 1 つはナビゲーションの目的で使用されます。ユーザーが子コンポーネントの「次へ」というボタンをクリックすると、親コンポーネントに通知して次のページに移動できるようにする必要があります。 これをどのように達成すればよいでしょうか?
正解:C
To notify the parent component from a child component when a button is clicked: Option C: Create a custom event Child Component: // ChildComponent.js handleClick() { const nextEvent = new CustomEvent('next'); this.dispatchEvent(nextEvent); } Parent Component: <!-- ParentComponent.html --> <c-child-component onnext={handleNext}></c-child-component> javascript Copy code // ParentComponent.js handleNext() { // Navigate to the next page } Reference: "To communicate up the component containment hierarchy, fire a custom event in the child component." - Lightning Web Components Developer Guide: Communicate with Events Why Other Options Are Incorrect: Option A: Updating a property on the parent directly is not possible from the child. Option B: "Fire a notification" is vague and not a standard mechanism. Option D: Calling a method in the Apex controller does not facilitate child-to-parent communication within components.