Search code examples
javascriptpolymerweb-componentpolymer-3.x

Passing a function as a parameter to child component polymer 3


I'm new to Polymer and stared learning it using a course called Polymer 3 - Code like a Google developer.

I would like to call the function: addContactClick() from the child component: side-menu.

To do that I have passed the function as an argument. I have defined it in the side-menu and called it using on-click

start-polymer3.js:

import {
  PolymerElement,
  html
} from '@polymer/polymer';
import SideMenu from './side-menu.js'
import ContentArea from './content-area.js'

class StartPolymer3 extends PolymerElement {
  static get properties() {
    return {
      addContactClick: {
        type: Function,
      }
    };
  }

  constructor() {
    super();
    this.addContactClick = this.addContactClick.bind(this);
  }

  static get template() {
    return html `
      <style>
        .main-page {
          display: grid;
          grid-template-columns: 250px 1fr;
        }
      </style>
      <div class="main-page">
        <side-menu addContactClick="addContactClick"></side-menu> 
        <content-area></content-area>
      </div>
    `;
  }
  addContactClick() {
    console.log('Button Clicked');
  }
}

customElements.define('start-polymer3', StartPolymer3);

side-menu.js:

import {
  PolymerElement,
  html
} from '@polymer/polymer';

export default class SideMenu extends PolymerElement {


  constructor() {
    super();
  }

  static get properties() {
    return {
      addContactClick: {
        type: Function
      },
    };
  }

  static get template() {
    return html `
      <style>
        #side-menu {
            background: #323759;
            height: 100vh;
            padding: 50px 25px;
        }
        .logo {
            text-align: center;
        }
        .logo img {
            width: 50px;
        }
        .title {
            color: #ccced7;
            font-weight: 700;
            margin: 1rem 0;
        }
        #side-menu nav a {
            color: #ccced7;
            text-decoration: none;
            text-transform: capitalize;
            display: block;
            padding: 10px 10px 10px 0;
        }
        #side-menu nav a span.icon{
            padding: 10px 10px 10px 0;
        }
        
        
      </style>
        <section id="side-menu"> 
          <div class="logo">
            <img src="https://www.dexterousgroup.com.au/wp-content/uploads/2018/03/google-logo-icon-PNG-Transparent-Background.png">
          </div>
          <div class="menu">
            <div class="title">Contacts</div>
            <nav>
              <a href="#" on-click="addContactClick"><span class="icon"> + </span>Add Contacts</a>
            </nav>
          </div>
        </section>
    `;
  }

}

customElements.define('side-menu', SideMenu);

When I execute it, I get the following error:listener method addContactClick not defined handler @ template-stamp.js:92


Solution

  • Here are changes you should do and try.

    start-polymer3.js

    1. Removed property addContactClick.
    2. Changed definition of addContactClick() function
    3. Added Binding instead of just name of function, while passing our own function in side-menu element.

    import {
      PolymerElement,
      html
    } from '@polymer/polymer';
    import SideMenu from './side-menu.js'
    import ContentArea from './content-area.js'
    
    class StartPolymer3 extends PolymerElement {
      static get properties() {
        return {
          
        };
      }
    
      constructor() {
        super();
        // this.addContactClick = this.addContactClick.bind(this);
      }
    
      static get template() {
        return html `
          <style>
            .main-page {
              display: grid;
              grid-template-columns: 250px 1fr;
            }
          </style>
          <div class="main-page">
            <side-menu add-contact-click="[[addContactClick()]]"></side-menu> 
            <content-area></content-area>
          </div>
        `;
      }
      addContactClick() {
        return function() {console.log('Button Clicked')};
      }
    }
    
    customElements.define('start-polymer3', StartPolymer3);

    side-menu.js

    1. Changed type of addContactClick from Function to String.

    import {
      PolymerElement,
      html
    } from '@polymer/polymer';
    
    export default class SideMenu extends PolymerElement {
    
    
      constructor() {
        super();
      }
    
      static get properties() {
        return {
          addContactClick: {
            type: String
          },
        };
      }
    
      static get template() {
        return html `
          <style>
            #side-menu {
                background: #323759;
                height: 100vh;
                padding: 50px 25px;
            }
            .logo {
                text-align: center;
            }
            .logo img {
                width: 50px;
            }
            .title {
                color: #ccced7;
                font-weight: 700;
                margin: 1rem 0;
            }
            #side-menu nav a {
                color: #ccced7;
                text-decoration: none;
                text-transform: capitalize;
                display: block;
                padding: 10px 10px 10px 0;
            }
            #side-menu nav a span.icon{
                padding: 10px 10px 10px 0;
            }
            
            
          </style>
            <section id="side-menu"> 
              <div class="logo">
                <img src="https://www.dexterousgroup.com.au/wp-content/uploads/2018/03/google-logo-icon-PNG-Transparent-Background.png">
              </div>
              <div class="menu">
                <div class="title">Contacts</div>
                <nav>
                  <a href="#" on-click="addContactClick"><span class="icon"> + </span>Add Contacts</a>
                </nav>
              </div>
            </section>
        `;
      }
    
    }
    
    customElements.define('side-menu', SideMenu);

    If still, you have an issue let me know.

    Here is another link you can visit which is for similar question:

    Passing a function as an attribute value