0

We're having issues calling a public (@api) method in child component from the parent. We are doing everything as in the guides - the child has an annotated method:

@api publicMethod(param) {
    console.log('--------public method in child called with param:', param);
}

The parent calls the method as follows (button click handler):

let child = this.template.querySelector('c-lwc-asset-group-list');
child.publicMethod({a:'param'});

The error we get is

Uncaught TypeError: this.template.querySelector(...).publicMethod is not a function

I couldn't find a similar issue when I was looking for a solution.

Edit: The parent component is created inside a Visulaforce page, not sure if this is relevant.

Edit 2: The error message does not display in the dev console, but rather at the bottom of the page in a div#auraErrorMessge.

Edit 3: The problem seems to be in this.template.querySelector, because when called on a regular class inside the template's DOM, e.g. like this:

this.template.querySelector('.top-container').scrollIntoView(true);,

we get the same error:

Uncaught TypeError: this.template.querySelector(...).scrollIntoView is not a function.

  • Have you tried using Pubsub? github.com/developerforce/pubsub – d_k Sep 3 at 8:26
  • 1
    What happens when you attempt this without the VF page? Does it work? It would help to see a bit more of the details of lwcAssetGroupList. One possible issue could be the camel-case-to-kebab-case name conversion goofing up the querySelector call. – pchittum Sep 3 at 8:51
  • @pchittum I probably should have expressed myself better. Both parent and child are LWCs. The parent is initialized from a VF page. We are not running in Lightning Experience. I could try switching to it and adding the parent to a tab... But I don't think it would matter. EDIT: But it won't be a very easy task, the parent has some api parameters as well, so I will first need to make a wrapper component. – Vít Říha Sep 3 at 9:04
  • So when you say the parent is initialized from a VF page, how are you doing that? Are you using lightning for Visualforce? developer.salesforce.com/docs/atlas.en-us.lightning.meta/… – pchittum Sep 3 at 9:18
  • Yes, that's pretty much it, the way it is in your link. – Vít Říha Sep 3 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.