Creating a "Container" component in Vue.js step by step

As a newcomer to Vue, I am facing a challenge in implementing a wrapper component similar to React's 'Wrapper' component. Specifically, I want to create a reusable datagrid component using a 3rd-party some-table component and a pagination component. The goal is for the datagrid component to provide the necessary data/props/events for both child components and manage communication between them. In React, achieving this would be as simple as:

// inside <Datagrid />
<Table {...someProps} />
<Pagination {...otherProps} />

However, in Vue, it seems that passing down props to children components is not as straightforward:

// inside Datagrid.vue
<some-table v-bind="$props"></some-table>

I have considered using slots, but I'm unsure if they can fully address my requirements. The wrapper component I envision should gather all the necessary props/events/slots required by its children (potentially 3rd-party components) and pass them down effectively. Additionally, it should handle tasks like data exchange between its children components. While datagrid could serve as a slots wrapper, the issue arises when both table and pagination need access to the same data prop, which I believe should be managed by datagrid. How can I efficiently share this data with the slots?

// Datagrid.vue
<template>
  <div>
    <slot name="table"></slot>
    <slot name="pagination"></slot>
  </div>
</template>

<script>
export default {
  name: 'Datagrid',
  data() {
    return {
      data: 'How to share it with table and pagination',
    }
  },
}
</script> 

I have explored some possible solutions:

  1. Using Render Functions, though I believe this may overcomplicate the situation
  2. Instead of creating a 'Container' component, utilizing mixins. However, does this mean I have to include
    <pagination :total="total" :other-props="are-same-for-all-datagrids"></pagination>
    every time I need a datagrid?

Are there any examples or best practices that address such scenarios in Vue? Your insights are greatly appreciated!

Answer №1

Utilizing slots:

Vue.component('wrapper', { template: '#wrapper' })

new Vue({ el: '#app' })
<!-- wrapper template -->
<script type="text/x-template" id="wrapper">
  <div class="wrapper" style="background: beige; padding: 5px;">
    This is being wrapped: 
    <slot></slot>
  </div>
</script>

<div id="app">
  <wrapper>
    <div style="background: aliceblue;">I'm being wrapped!</div>
  </wrapper>
</div>

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.3.3/vue.js"></script>

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

Guide on Validating and Updating an embedded item within a mongoDB Collection Document

How do I go about updating the values of an embedded object within a mongoDB document? The values displayed for {{service.id}} and {{service.username}} in the table template are correct. However, I am unsure of the correct way to access them within the sa ...

The query attribute of the http_incomingmessage in Node.js is not empty, causing Express to have trouble parsing the query parameter

While utilizing node version 6.9.4 and express version 4, there seems to be an issue with the creation of the IncomingMessage object in _http_common.js. Specifically, on line number 60, the parser.incoming.query function is being set to a value (usually it ...

Tips for preventing the caret (^) symbol from being added to a package when installing with yarn

Is there a way to prevent Yarn from adding the caret prefix to version numbers when installing packages like npm has for changing version prefix (https://docs.npmjs.com/misc/config#save-prefix)? I would like to apply this configuration only for the current ...

The process of inserting data using NextJS Mysql works seamlessly when executed through phpMyAdmin, however, it encounters issues when

My SQL query works perfectly in phpmyadmin, but I'm encountering an issue when making a request from the API. The API uses the MySQL package which was installed using: npm i mysql This is the SQL code that is causing the problem: BEGIN; INSERT INTO A ...

JQuery AJAX not retrieving data after $.post request

I am currently facing a challenge with a specific JQuery $.post request to a PHP-based processor. To troubleshoot, I set up a test page containing the following code: It's important to note that this is hosted on a subdomain, but there are no cross-d ...

When the browser is resized, the fadeIn() function restarts from the

My plan was to create a dynamic effect where a browser-sized div would rotate through 3 different backgrounds using jQuery's fading effect. However, I encountered an issue - whenever I resize the browser window, the effect restarts from the beginning ...

The jQuery .val() function does not function properly when using 'this'

This is an example with a textarea: <textarea id='ta' onkeydown='down(this)'></textarea> and here is the accompanying JavaScript code: <script> function down(input) { alert(input.val()); // not functioning ...

JavaScript: Extending a class with an invalid or null value is not permitted

Trying my hand at constructing a page object for login testing with WebdriverIO. Encountering the error ERROR: Class extends value #<Page> is not a function or null on line 3 of login.page.js. No clue what mistake I'm making... Is there a wron ...

What is the best way to add a filter to a nested array?

I am currently facing a challenge in creating a multiple filter for multiple arrays without duplicating the nested array. I am working with vuejs and some plugins that are relying on the array, so my only option is to filter it without modifying it. Using ...

What might be causing the issue with my ajax request to the PHP file within the data parameter?

Although I am successfully getting my php value into a JavaScript variable, it seems like my script.js file is unable to find it. For the sake of brevity, some files have been omitted in this question. The main issue is that the script.js file retrieves th ...

When the mouse leaves, the background image will revert back to its original setting

https://i.stack.imgur.com/Ojd0Q.pngI need assistance in reverting a div's background image back to its default state using the onmouseleave method. Below is the HTML and JS code I have been working on: <script type="text/javascript"> functi ...

I am attempting to add a new row (div) for the invoice, but when I do so, the

Looking for a solution to repeat a row when clicking a button in order to utilize PHP for data manipulation? Check out the code snippet below: <!-- Table row --> <br><br> <div class="row"> <!--Product Table--> <div ...

Submitting HTML forms in SilverStripe using Ajax

I need to transfer data from a basic HTML form to a controller using Ajax, then handle the data and send a response back. Currently, my setup looks like this: HomePage.ss <form method="POST" class="form-horizontal submit-form" onsubmit="return checkf ...

Interactive input field designed for modifying, adding, and removing data in MySQL

I am working on a project where I have a simple form. However, I need to dynamically change the form action from insert to update within the same page. Additionally, I also want to display the values on the same page. Within my code, I have set up the up ...

Unable to alter the vertex color in three.js PointCloud

I'm currently facing an issue with changing the color of a vertex on a mouse click using three.js and Angular. After doing some research, I believe that setting up vertex colors is the way to go. My approach involves setting up vertex colors and then ...

Create a new array of objects by extracting specific properties from an existing array of objects

Consider the input array below: const initialArray = [{name: 'john', age: 12, height: 178, likes: 'music'}, {name: 'mike', age: 22, height: 181, likes: 'sport'}, {name: &ap ...

The wrapAll() method can be used to wrap list items within two columns

I am looking to group multiple li elements within two div containers by using jQuery's wrapAll method. The challenge lies in the fact that these items are rendered within a single <ul> element via a CMS. Here is the current setup: <ul> ...

Ensure that you decode the URL before making any changes to the location in a

Hi everyone, I'm facing an issue and need your help. Let's say we have a URL like site.com/post?comments=1,2,3,4. When I paste it into the browser address bar, my app opens and decodes the URL to site.com/post?comments=1%2C2%2C3%2C4. How can I re ...

Challenges encountered while working with OpenWeather API

I created a weather prediction web application using p5.js. It functions perfectly on my local server. However, I keep encountering this issue on the GitHub page: Mixed Content: The page at '' was loaded over HTTPS, but requested an insecure ...

Incorporate text into the URL of the image

Got a URL of an image like this: https://lipsum.mobi/catalog/product/SE0229E/YG/AAA/4/1/SE0229E-YG-AAA-4.jpg', and looking to add 240x240 within the URL. Current Url: https://lipsum.mobi/catalog/product/SE0229E/YG/AAA/4/1/SE0229E-YG-AAA-4.jpg Desire ...