Choose the right Vue.js component for optimal performance

I have a primary element within which there is a secondary element with vue.js 2.0.

The issue arises when the secondary element relies on methods from the primary element.

Here's an illustration:

Vue.component('primary-element', {
  template: '<p>This is the main component. <sub-component><button @click="test()">If this button is pressed: "sub-component" should appear. </button></sub-component></p>',
  methods: {
  test() {
       alert('main element');
        }
  }
})

Vue.component('sub-component', {
  template: '<p>This is sub-component <slot></slot> </p>',
  methods: {
       test() {
      alert('sub-component');
      }
  }
})

var app = new Vue({
  el: '#app'
})
<script src="https://vuejs.org/js/vue.min.js"></script>
<div id="app">
  <primary-element></primary-element>
</div>

How can I ensure that the sub-component uses its own methods, and in this scenario display an alert saying 'sub-component' instead of 'main element' when the button is clicked?

Answer №1

Implement a scoped slot for better control.

Create a Vue component called 'main-component' with a scoped slot to interact with its sub-component.
<script src="https://vuejs.org/js/vue.min.js"></script>
<div id="app">
  <main-component></main-component>
</div>

Answer №2

Why not give this a shot:

<sub-element ref="sub"><button @click="$refs.sub.attempt()">...</button></sub-element>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the functionality of this JQuery Popup?

I'm facing an issue with my JQuery Popup. When the "Login" button is clicked, it hides the Login popup but doesn't show the Sign Up popup. How can I make sure that clicking on "Login" hides the Login popup and shows the Sign Up popup accordingly? ...

What is the best way to reset react-id-swiper every time an event handler is triggered in a React application?

I have incorporated the react-id-swiper module into my React project to create a dynamic image slider. By setting onClick event handlers on buttons with different id attributes, I trigger API calls that update the state and populate the ImageSlider compone ...

Underscore - Evaluating the differences between two arrays of objects (positions)

Is it possible to compare arrays based on the changes in their element positions? I have an original array of objects that has one of its elements' values changed, resulting in a new array: origElements = [{id: 1, value: 50}, ...

Is there a problem with the way divs are being displayed when using jQuery to show the first 12 elements with the class "hide-show"?

I am encountering a problem with displaying data using the jQuery slice() and show() methods to reveal dynamically generated divs from a PHP function. The code is as follows: <style> .hide-show { display :none; } </style> <div class="c ...

How Can You Create a Sliding List Animation (Up/Down) Using Angular and Twitter-Bootstrap?

Hey, can you give me a hand with this? :) I'm attempting to create a sleek sliding up and down list in Angular, but I'm struggling to make it work. My CSS skills are not very advanced, so I'm still learning and gave it my best shot: http:// ...

Generating components dynamically at runtime following an HTTP request in VueJS

Is there a way to conditionally render VueJS components based on an Http request immediately upon application launch? I want to display component 1 if the response is successful, otherwise display component 2. Additionally, I would like the rendering of th ...

Unable to Access Browser Page

After printing out the URL in the console, I encountered an issue while trying to retrieve it using browser.get(). The error message displayed is as follows: Failed: Parameter 'url' must be a string, not object. Failed: Parameter 'url&apo ...

increasing the size of the JSON object

I have a function that is being called multiple times utilizing jQuery to fetch different JSON data from an API. My aim is to calculate the total count of a specific portion of the JSON retrieved. Below is an example of what I currently have: getTheData( ...

Tips for successfully importing .eot and .woff documents using Babel?

When attempting to start my project, I encountered the following error message (You may need an appropriate loader to handle this file type.) for .eot, .woff, .ttf, and .svg files: ERROR in ./src/styles/fonts/nm/icons/nm-icons.svg?aaf8685e87a6901c76c52d00 ...

The call to the hook is invalid. In order to use hooks, they must be called within the body of a function component in

So, in my upcoming application, I am incorporating the react-google-one-tap-login library which features the useGoogleOneTapLogin hooks that need to be invoked within a React component. However, when I attempt to use it in this manner: func() { useGoogle ...

HighCharts velocity gauge inquiry

I recently integrated a highcharts speedometer with PHP and MYSQL on my website. Everything seemed to be working smoothly until I added the JavaScript code for the speedometer, causing it not to display. There are no error messages, just a blank screen whe ...

Tips for accessing the value and text of an Angular Material mat-select through the use of *ngFor

When using a dropdown menu, I want to retrieve both the value and text of the selected option. View dropdown image Underneath the dropdown menu, I aim to display values in the format of "options: 'value' - 'selected option'". compone ...

Creating an Image Slideshow on Your Website

I'm looking to create an image slideshow on my website that functions similarly to the one found at . However, I want the images to occupy the entire screen rather than having smaller images like the reference site. Can anyone offer guidance on how t ...

Efficiently Measuring the Visibility Area of DetailsList in Fluent UI

I am currently utilizing the DetalisList component alongside the ScrollablePane to ensure that the header remains fixed while allowing the rows to scroll. However, I have encountered a challenge as I need to manually specify the height of the scrollable co ...

Hiding links in javascript

I currently have a hyperlink <a href="url1">url1</a>. Interestingly, I have noticed that some websites utilize JavaScript to display this as url1 but in reality, it redirects to url2. When you hover over the link, the original url is not visib ...

What sets $emit and $dispatch apart in Vue.js?

Vue 2.0 has deprecated the use of $dispatch and $broadcast. I have noticed that $dispatch is similar to $emit. What are the key differences between them? Can we safely replace $dispatch with $emit during migration? ...

The function toJson() does not exist for the specified stdClass object

After following a tutorial on implementing websockets in Laravel to create a live commenting system, I encountered an error that I cannot figure out. Even though I followed the code exactly as demonstrated in the video, this error persists. Does anyone hav ...

Having trouble loading select fields using AJAX within Wordpress? Update and find a solution!

UPDATE: I am struggling to iterate through the response object and populate a select field using my ajax function. Although I have tried using a for loop instead of each(), the select field gets populated with "undefined". Any suggestions on how to resolve ...

Property location elusive

I've set up VueJS locally in a very basic way and I'm encountering issues while trying to run a simple example that demonstrates outputting raw HTML. I prefer not to provide a link to JS Fiddle as I aim to resolve it on my local setup, since some ...

Exploring the functionality of event.target.name.value

I've been struggling to make event.target.name.value work for an input field in my form. When I submit the form, the values appear as null and I have to click twice to get them. This is the code I've written: const [name, setName] = useState(& ...