0

https://developer.salesforce.com/docs/component-library/tools/playground/GcVjmML4/2/edit 为什么不在视图上更新@track 值?以及如何强制更新值?

Parent.cmp


   import { LightningElement } from 'lwc';
    var OBG ={ data:
                {name: "Valera",
                lastName : "Pypkin",
                lastNameTWO : "lastNameTWO1111",
                tt: {
                    gg:{name:"aaaaa"}
                }}
            }
    export default class App extends LightningElement {
        OBG = OBG; 
    }

儿童.cmp

import { LightningElement, api, track } from 'lwc';

export default class Child extends LightningElement {
    @api data;
    @track _objCopy;
    get item() {
        return this.data.data;
    }

    handleClick(){
      const Other = { name: "Valera",
            lastName : "Pypkin",
            lastNameTWO : "lastNameTWO1111",
            tt: {
                gg:{name:"gggg"}
            }}
      this.myobj = Other;      //change track value;
   }

    get myobj() {
        return this.item;
    }

    set myobj(value) {
        this._objCopy = value;
    }
}

为什么不更新视图中的@track 值?以及如何强制更新值?

4

1 回答 1

0

我相信你想要这样的东西:

import { LightningElement, api, track } from 'lwc';

        export default class Child extends LightningElement {
            @api
            get data() {
                return this._objCopy;
            }
            set data(value) {
                this._objCopy = value;
            }

            @track _objCopy;

            handleClick(){
                const Other = { name: "Valera",
                    lastName : "Pypkin",
                    lastNameTWO : "lastNameTWO1111",
                    tt: {
                        gg:{name:"gggg"}
                    }}
                this._objCopy = Other;      //change track value;
            }
        }

在组件的视图上只需添加 {_objCopy} 即可将值绑定到视图。一旦handleClick 被触发,它将更新视图。希望能帮助到你

于 2020-02-04T14:24:28.427 回答