Splidejs Integration in Nuxt.js with Vue

Is there anyone out there who has attempted to integrate a Vue solution into Nuxt as a plugin or module?

I've been facing numerous challenges while trying to accomplish this correctly!

My current struggle involves importing the Splide Vue slider from NPM into Nuxt. Even after installing it via NPM, I'm encountering difficulties in importing it into a splide.js file within plugins and then referencing it in nuxt.config.js as a plugin. Various errors keep popping up.

Below are the relevant files:

nuxt.config.js

modules: [
    // https://go.nuxtjs.dev/bootstrap
    '@nuxtjs/style-resources',
    '@nuxtjs/bulma', 
    '@splidejs/splide'
    
  ],

splide.vue (in nuxt components)

<template>
  <splide :options="options">
        <splide-slide>
            <h1>boo</h1>
        </splide-slide>
        <splide-slide>
            <h1>boo2</h1>
        </splide-slide>
  </splide>
</template>

<script>
  export default {
      data() {
          return {
              options: {
                rewind : true,
                  width  : 800,
                  perPage: 1,
                  gap    : '1rem',
              },
          };
      },
  }
</script>

splide.js (in plugins)

import Vue from 'vue';
import VueSplide from 'vue-splide';

Vue.use( VueSplide );
new Vue( {
    el    : '#app',
    render: h => h( App ),
  } );

Here is the link to the Splide site for instructions: splide instructions

The most recent error I encountered is "window is not defined"

Answer №1

Don't forget to change the name of splide.js to splide.client.js to designate it as a client-side plugin.

For more information, check out the Nuxt documentation.

To specify whether a plugin should run on the client or server side, you can use the .client.js or .server.js extension for the plugin file. This will ensure that the file is included only on the specified (client or server) side.

Answer №2

Big thanks to Nick!

I've implemented a few other tweaks as well, and now it's running smoothly. Here's the updated code that works perfectly for me:

slider.vue (located under components)

<template>
    <div>
        <splide :options="options">
        <splide-slide>
            <h1>boo1</h1>
            <!-- <img src="../assets/img/mw-logo.png" alt="">         -->
        </splide-slide>
        <splide-slide>
            <h1>boo2</h1> 
            <!-- <img src="../assets/img/mw-logo.png" alt="">         -->
        </splide-slide>
        <splide-slide>
            <h1>boo2</h1> 
            <!-- <img src="../assets/img/mw-logo.png" alt="">         -->
        </splide-slide>
        </splide>
    </div>
</template>

<script>
  export default {
      data() {
          return {
              options: {
                  rewind : true,
                  width  : 800,
                  perPage: 2,
                  gap    : '1rem',
                  
              },
          };
      },
  }
</script>

nuxt.config.js:

  plugins: [
    {src: '~/plugins/splide.js', mode: 'client'}
  ],

splide.client.js

    import Vue from 'vue';
    import VueSplide from '@splidejs/vue-splide';
    import '@splidejs/splide/dist/css/themes/splide-default.min.css';


    Vue.use( VueSplide );
    new Vue( {
        el    : '#app',
        render: h => h( App ),
    } );

index.vue (found under pages)

<slider />    

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

Extract information from a JSON string and present it on the screen

I am a complete novice when it comes to D3 (with very little experience in JS). Here are the lines of code I am working with: <script type='text/javascript'> data ='{"mpg":21,"cyl":6,"disp":160,"hp":110,"drat":3.9,"wt":2.62,"qsec":16. ...

Setting the z-index for a JavaScript plugin

I need help with embedding the LinkedIn plugin into a website that has stacked images using z-index for layout. I have tried assigning the plugin to a CSS class with a z-index and position properties, but it hasn't worked as expected. Can anyone sugge ...

Setting a default value for an optional url parameter in Vue Router when another optional url parameter is specified

I am currently facing an issue with my route setup. Here is the route configuration I have: export const personRoutes: RouteRecordRaw[] = [ { path: '/person/:id?/:handedSlug?', name: 'Person', component: () => import(&a ...

Directives for Nested Elements in AngularJS

I am currently working on creating two custom elements: <accordion> and <accordion-group-active>. .directive('accordion', function () { return { restrict: 'E', replace: true, transclude: true, ...

Making an AJAX request to a different domain

My understanding is that the AJAX cross-domain policy prevents me from simply making a call to "http://www.google.com" using an AJAX HTTP request and then displaying the results on my website. I attempted to use the dataType "jsonp", which theoretically s ...

Encountering a problem with fetching data in a dropdown using a hidden select in MVC 4 Razor and Selectpicker

I need to show my hidden select data in dropdown-menu inner selectpicker ul with the default "--choose--" option selected. ASP.NET MVC Razor Syntax @Html.DropDownListFor(model => model.BrandName, Model.CompanyItems, "--Choose Brand--", new { @id = "B ...

Do you always need to use $scope when defining a method in Angular?

Is it always necessary to set a method to the $scope in order for it to be visible or accessible in various parts of an application? For example, is it a good practice to use $scope when defining a method that may only be used within one controller? Consid ...

"Interacting with an Asp.net MVC 5 controller from a different assembly is causing issues with serving angular static files,

Currently, I am working on a project that includes both the UI and a class library where I have shared views. I have successfully implemented VirtualPathProvider to serve Razor pages as embedded resources, which is functioning smoothly. However, within t ...

Cross-origin resource sharing policy is preventing a request from the client-side to the server

I am currently working on a Vue.js application and I am facing an issue with CORS policy blocking my backend requests. I am using axios to make the request to the backend for data that I need to display charts on the UI. Here is the code snippet of my char ...

If the item already exists within the array, I aim to replace the existing object with the new one

I am faced with a situation where I have an array of objects, and when a user selects an option, it adds a new object to the array. My goal is to write a code that can check if this new object's key already exists in one of the objects within the arra ...

I successfully created a Chinese writing practice deck in Anki, which is functional on the desktop version but unfortunately not compatible with Ankidroid

While AnkiDesktop is functioning properly, there seems to be an issue with character encoding in Ankidroid. Despite trying numerous solutions, the problem persists. The character length displays correctly in Anki Desktop, but not in Ankidroid. For example ...

I need to confirm the validity of minDate and maxDate in a ReactJS component, ensuring that maxDate is always at least 5 years after min

start from today and select a date end 5 years from the starting date I haven't attempted anything yet. Seeking help for a solution. ...

An exploration of dynamic reactivity at the template level within the Meteor

In my current project, I am facing a challenge where I need to present data in a dashboard both as a chart using the perak:c3 package and as a table using the aslagle:reactive-table package. The issue arises because the data is retrieved from a MongoDB col ...

Jquery deactivates the CSS hover effect

Having a dilemma with linked photos on my website. I have set their opacity to 0.45 by default, but want them to change to full opacity (1) when hovered over or clicked. I've implemented a JQuery function that resets the rest of the photos back to 0.4 ...

React: Incorporating .map for nested arrays. Ensure each child component within the list is assigned a distinct "key" property

I have an array called navMenuItems that contains data for building a navigation menu with links and link names. Each item in the array may also contain child links, similar to the mobile menu on https://www.w3schools.com/. However, I'm encountering a ...

Incorporating Checkbox Value into Textbox with classic ASP and jQuery

Here is a glimpse of the code I've written: response.write "<th align=left><font size=2>" response.write "1. <input type=checkbox class='checkboxes' value='ORG'>Organization" response.write "</font> ...

Enhance the visibility of pop-ups across all pages

I'm currently working on a feature to maximize my popup, specifically an iframe. Here is the JQuery code I am using for this functionality: $(document).ready(function(){ $('#"+btnMaximiza.Id+"').click(function(){ $('#"+btnMaxim ...

Automatically selecting the map center while using the Drawing Manager feature for placing markers

My application utilizes the Google Drawing Library. When a user clicks on the Add New Marker Button, the Drawing Manager is activated allowing the user to generate a marker by clicking anywhere on the map. Subsequently, the following listener is executed: ...

How to assign attributes to multiple menu items in WordPress without using JavaScript

I'm currently working on adding attributes to each item in my WordPress navbar. Here is what I have at the moment: <ul id="menu-nav-bar" class="menu"> <li><a href="#text">text</a></li> <li><a href="#car ...

Script executing single run only

I'm currently working on a side menu that slides open and closed from the left with the press of a button. I have successfully implemented the CSS and HTML code, but I am facing issues with the JavaScript. The functionality works flawlessly at first: ...