1@api textValue;2textValueToRender;3changeCounter = 0;45get textValue(){6 return this.textValueToRender;7}89// Due to the nature of the Flow Runtime, this setter method10// is called after the FlowAttributeChangeEvent fired below. It also11// is invoked when reactivity leads to a change to the textValue property.12set textValue(newTextValue){13 this.changeCounter++;14 this.textValueToRender = newTextValue;15}1617handleTextInputChange(event){18 const event = new FlowAttributeChangeEvent('textValue', event.target.value);19 this.dispatchEvent(event);20}
次の例は、ユーザに入力テキストボックスといくつかの色見本を表示するカラーピッカーを示しています。カラーピッカーコンポーネントはフローに color を返すので、color にのみ @api アノテーションが必要です。selectedSwatchId と inputValue メンバーは内部のみで、デコレータは必要ありません。
1//colorPicker.js23@api color;45inputValue;6selectedSwatchId;789ALL_SWATCHES =[{10 'id': 1,11 'color': 'red'12}, {13 'id': 2,14 'color': 'blue'15}, {16 'id': 3,17 'color': 'green'18}, {19 'id': 4,20 'color': 'yellow'21}];2223set color(newColor){24 // Set the content of the input text to the color value:25 this.inputValue = newColor;2627 // If the color matches a swatch, highlight it in the UI:28 const foundSwatch = this.ALL_SWATCHES.find(swatch => swatch.color === newColor);29 this.selectedSwatchId = foundSwatch && foundSwatch.id;30}3132get color(){33 return this.inputValue;34}3536handleSwatchSelected(event){37 // Select get the swatch data for the clicked swatch:38 const selectedSwatch = this.ALL_SWATCHES.find(swatch => swatch.id === event.target.dataset.id);39 const changeEvent = new FlowAttributeChangeEvent('color', selectedSwatch.color);40 this.dispatchEvent(changeEvent);41}4243handleInputValueChange(event){44 this.inputValue = event.target.value;45 const changeEvent = new FlowAttributeChangeEvent('color', this.inputValue);46 this.dispatchEvent(changeEvent);47}
get メソッドを使用して複数の @api プロパティを結合し、ビューの派生変数を作成します。
1//Salutation.js2@api firstName;3@api lastName;4@api ageName;56get salutation(){7 return `Hello ${this.firstName} ${this.lastName}, you are ${this.age} years old.`;8}
1const changeEvent = new FlowAttributeChangeEvent("prop", "value");2changeEvent.bubbles = false; // Don’t do this3changeEvent.composed = false; // Don’t do this
競合状態が発生する可能性があるため、FlowAttributeChangeEvents と FlowNavigationXxx イベントを同時に起動しないでください。ナビゲーションプロセスが開始されるまでに、Lightning Web コンポーネントが更新された値を表示する時間が確保されるわけではありません。
ナビゲーション変更イベントのベストプラクティス
FlowAttributeChangeEvent に加えて、Lightning Web コンポーネントは、画面間の移動、フローの一時停止または終了のために、ナビゲーションイベントを起動できます。ナビゲーションイベントを使用するには、次の関数を lightning/flowSupport からインポートします。