host
Using the host attribute, you can obtain the host component instance of an element. This is very useful for accessing the data and methods of its host component inside the component.
The following is an example that demonstrates how to use the host attribute to obtain an instance of the host component:
Username: {{username}}
Response: {{response}}
In this example, we created a custom component user-card, and inside the component, we accessed the host component (page)'s method sayHi via this.host, achieving interaction between the component and the host.
If the element is not within a component or page module, the value of host will be null. For example:
-
I am target
-
In this example, the #target element is under the body, not within any component or page, so the value of $("#target").host is null.