Search code examples
javascriptpropertiespolymerpolymer-2.xpolymer-elements

change property value in polymer


I have declared a property like this:

static get properties() {
        return {
            auth1: {
                type: Boolean,
                readonly: false,
                value: false,
                notify: true
            }
        };
    }

in my Polymer element. Now I have the function:

connect(){
        this.auth1.value = true;
        console.log("Authenticated" + this.authenticated);

    }

which should change the property value to true. Everytime I call the function I am having the error "TypeError: Attempted to assign to readonly property.". But I have set readonly to false in my property. My function is called with a button like this: <button id="loginbutton" on-tap="connect">Click me!</button>

Can anybody help me?


Solution

  • The problem was in the changing of the property value.

    Instead:

    connect(){
            this.auth1.value = true;
            console.log("Authenticated" + this.authenticated);
    
        }
    

    The change can be like this:

    connect() {
        this.auth1 = true;
        console.log("Authenticated" + this.auth1.value);
    }
    

    readonly: false is the default, and can be removed.

    static get properties() {
        return {
            auth1: {
                type: Boolean,
                value: false,
                notify: true
            }
        };
    }