How to integrate inertiaJS with quasar framework?

Viewed 1547

I would like to integrate intertiaJS into my Quasar app so that I can communicate with my Laravel backend. My problem now is that the general stuff is taken over by the Quasar CLI, which is good in principle, but in this case it takes away my entry point as described at https://inertiajs.com/client-side-setup:

import { createApp, h } from 'vue'
import { App, plugin } from '@inertiajs/inertia-vue3'

const el = document.getElementById('app')

createApp({
  render: () => h(App, {
    initialPage: JSON.parse(el.dataset.page),
    resolveComponent: name => require(`./Pages/${name}`).default,
  })
}).use(plugin).mount(el)

My thought is that I could use a boot file like the offered in Quasar (https://quasar.dev/quasar-cli/boot-files), but I have to admit that I don't have the right approach. When I look at the app.js that is automatically generated, I see that nothing special happens in the rendering:

/**
 * THIS FILE IS GENERATED AUTOMATICALLY.
 * DO NOT EDIT.
 *
 * You are probably looking on adding startup/initialization code.
 * Use "quasar new boot <name>" and add it there.
 * One boot file per concern. Then reference the file(s) in quasar.conf.js > boot:
 * boot: ['file', ...] // do not add ".js" extension to it.
 *
 * Boot files are your "main.js"
 **/
import Vue from 'vue'
import './import-quasar.js'

import App from 'app/src/App.vue'

import createStore from 'app/src/store/index'

import createRouter from 'app/src/router/index'

export default async function () {
  // create store and router instances
  
  const store = typeof createStore === 'function'
    ? await createStore({Vue})
    : createStore
  
  const router = typeof createRouter === 'function'
    ? await createRouter({Vue, store})
    : createRouter
  
  // make router instance available in store
  store.$router = router
  

  // Create the app instantiation Object.
  // Here we inject the router, store to all child components,
  // making them available everywhere as `this.$router` and `this.$store`.
  const app = {
    router,
    store,
    render: h => h(App)
  }

  app.el = '#q-app'
  
  // expose the app, the router and the store.
  // note we are not mounting the app here, since bootstrapping will be
  // different depending on whether we are in a browser or on the server.
  return {
    app,
    store,
    router
  }
}

I.e. in principle I should be able to link in without it causing any conflict situations. The question is, how would that look?

I have to link into the rendering afterwards and overwrite it as described in the code example. I would like to stay with the Quasar Cli, because it is very useful and the situation described here is the only exception.

p7

1 Answers

the boot files is the right place to inject and initialize your own dependencies or just configure some startup code for your application.

I have not had the opportunity to use the library you mention, but I detail a little how you could implement

create your boot file

import { plugin } from '@inertiajs/inertia-vue';
    
export default async({ app, Vue }) => {
  Vue.use(plugin);
}

until there you have 50%. On the other hand, you cannot do a mixin to the main instance but you could do it for each page, however I recommend that you make a component part to which you add the data you need and make a mixin of the library you need

<template>
  <div />
</template>
    
<script>
import { App } from '@inertiajs/inertia-vue';

export default {
  mixins: [App],
  props: ['initialPage', 'resolveComponent'],
}
</script>

In order to do this, modify according to how the library you use works.

Related