Documentation

On this page

Download full book (text)

# **Chapter 26: Vue and Vite Client — Same Gates as React** > **Prerequisites:** Chapter 25 (shared `sphp-client.js` + Gate `exreactajax`), > Chapter 13 (AJAX / jsServer) > **Try it:** Same Gate as Chapter 25 — `exreactajax.html` > (`exreactajax-ping.html`, `-echo`, `-panel`, `-form`, `-save`). > Vue helpers: `comp/vueJs/sphp-vue.js` (imports `comp/reactJs/sphp-client.js`). > `baseUrl`: your SartajPhp website origin (any app with Gates). > **Run:** [exreactajax.html](exreactajax.html) --- ## **26.1 Same Client, Vue UI** Chapter 25 introduced the ESM bridge. Vue does **not** need a different AJAX protocol — only a thin helper for reactive HTML slots. | Piece | Path | |-------|------| | Shared client | `comp/reactJs/sphp-client.js` | | Vue helpers | `comp/vueJs/sphp-vue.js` | | Demo Gate | `exreactajax` (unchanged) | API names stay identical: `sphp.sendAjax`, `sphp.getAjaxReturn`, `sphp.callNativeGate`, `sphp.ensureFormLibs`. **Ownership** - **Vue** — templates, refs, composition API - **SartajPhp Gate** — `page_event_*`, jsServer, FrontFiles, fui validation ## **26.2 Install Aliases** Load the shared client (docs site or your app’s `comp/reactJs/`) and the Vue helper. `baseUrl` is configured in app code (§26.3), not here. ```js import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' const SPHP_CLIENT_ORIGIN = 'https://sartajphp.com' // Or your app if it ships comp/reactJs + comp/vueJs: // const SPHP_CLIENT_ORIGIN = 'https://myapp.example' export default defineConfig({ plugins: [vue()], resolve: { alias: { 'sphp-client': SPHP_CLIENT_ORIGIN + '/comp/reactJs/sphp-client.js', 'sphp-vue': SPHP_CLIENT_ORIGIN + '/comp/vueJs/sphp-vue.js', }, }, }) ``` ## **26.3 Configure and Call (Same Endpoints as Chapter 25)** ```js import { ref } from 'vue' import sphp from 'sphp-client' import { useSphpHtml } from 'sphp-vue' sphp.configure({ // YOUR SartajPhp app (Gates). Any site with start.php — not only the docs host. baseUrl: 'https://myapp.example', credentials: 'include', debug: true, }) window.onAjaxPing = function (msg) { console.log('onAjaxPing', msg) } // Shared with React demo — form Front calls this after validation.js passes window.onSphpSpaFormSave = function () { const form = document.getElementById('formReact') if (!form) { return false } const data = Object.fromEntries(new FormData(form).entries()) sphp.getAjaxReturn('exreactajax-save.html', data, true, (retobj) => { console.log('save', retobj) }) return false } const name = ref('Ada') const echoText = ref('hello from Vue') const statusHtml = useSphpHtml('sphpStatus', 'idle') const panelHtml = useSphpHtml('sphpPanel', '') function runPing() { sphp.sendAjax('exreactajax-ping.html', { txtname: name.value }) } function runEcho() { sphp.getAjaxReturn( 'exreactajax-echo.html', { txt: echoText.value }, true, (retobj) => console.log(retobj) ) } function runPanel() { sphp.getAjaxReturn('exreactajax-panel.html', { who: name.value }, true, () => {}) } async function runForm() { await sphp.ensureFormLibs() sphp.getAjaxReturn('exreactajax-form.html', {}, true, () => {}) } ``` Template sketch: ```html <div id="sphpStatus" class="status" v-html="statusHtml"></div> <button type="button" @click="runPing">sendAjax ping</button> <button type="button" @click="runEcho">getAjaxReturn echo</button> <button type="button" @click="runPanel">Load panel Front</button> <div id="sphpPanel" v-html="panelHtml"></div> <button type="button" @click="runForm">Load form Front</button> <!-- Plain div: setHtml owns DOM so validation.js handlers survive --> <div id="sphpForm"></div> ``` ## **26.4 Vue Helper (`comp/vueJs/sphp-vue.js`)** ```js import { onMounted, onUnmounted, ref } from 'vue' import sphp from '../reactJs/sphp-client.js' export function useSphpHtml(id, initial = '') { const html = ref( sphp.getHtml(id) !== undefined ? sphp.getHtml(id) : initial ) let unsubscribe = null onMounted(() => { unsubscribe = sphp.subscribeHtml(id, (next) => { html.value = next == null ? '' : String(next) }) }) onUnmounted(() => { if (typeof unsubscribe === 'function') { unsubscribe() } }) return html } export { sphp } export default sphp ``` Use `v-html` for status/panel slots. For **forms** from `addJsonFront`, keep a plain `<div id="sphpForm">` without `v-html` binding — Vue updates would wipe jQuery submit handlers (same rule as React `SphpSlot`). ## **26.5 Full `App.vue` Pattern** ```vue <script setup> import { ref } from 'vue' import sphp from 'sphp-client' import { useSphpHtml } from 'sphp-vue' sphp.configure({ baseUrl: 'https://myapp.example', credentials: 'include', }) window.onAjaxPing = (msg) => console.log('onAjaxPing', msg) window.onSphpSpaFormSave = () => { const form = document.getElementById('formReact') if (!form) return false const data = Object.fromEntries(new FormData(form).entries()) sphp.getAjaxReturn('exreactajax-save.html', data, true, () => {}) return false } const name = ref('Ada') const echoText = ref('hello from Vue') const statusHtml = useSphpHtml('sphpStatus', 'idle') const panelHtml = useSphpHtml('sphpPanel', '') const busy = ref(false) function runPing() { busy.value = true sphp.sendAjax('exreactajax-ping.html', { txtname: name.value }) .finally(() => { busy.value = false }) } function runEcho() { busy.value = true sphp.getAjaxReturn('exreactajax-echo.html', { txt: echoText.value }, true, () => { busy.value = false }) } function runPanel() { busy.value = true sphp.getAjaxReturn('exreactajax-panel.html', { who: name.value }, true, () => { busy.value = false }) } function runForm() { busy.value = true sphp.ensureFormLibs() .then(() => sphp.getAjaxReturn('exreactajax-form.html', {}, true, () => { busy.value = false })) } </script> <template> <main> <div id="sphpStatus" v-html="statusHtml"></div> <input v-model="name" /> <button type="button" :disabled="busy" @click="runPing">ping</button> <input v-model="echoText" /> <button type="button" :disabled="busy" @click="runEcho">echo</button> <button type="button" :disabled="busy" @click="runPanel">panel</button> <div id="sphpPanel" v-html="panelHtml"></div> <button type="button" :disabled="busy" @click="runForm">form</button> <div id="sphpForm"></div> </main> </template> ``` Gate PHP and FrontFiles are **exactly** Chapter 25 / `exreactajax` — no Vue-specific Gate code. ## **26.6 Checklist** 1. Reuse `comp/reactJs/sphp-client.js`; add `comp/vueJs/sphp-vue.js` for `useSphpHtml`. 2. Point Vite aliases at those public (or your-app) paths. 3. Set `baseUrl` to **your** SartajPhp website. 4. Call the same relative Gate URLs as React (`exreactajax-*.html` or your gates). 5. Forms: `ensureFormLibs()` + plain `#sphpForm` + `window.onSphpSpaFormSave`. ## **26.7 Next Steps** - Chapter 25 for Gate / Front / form validation detail - Chapter 13 for jsServer block types - Chapter 22 if you need `callNativeGate` / WebSocket - Chapters 27–28 for MobileGate / Onsen hybrid Android apps --- **Summary:** Vue apps import the same `sphp-client.js` as React, add optional `sphp-vue.js` for reactive HTML, and call the same Gates. `baseUrl` is any SartajPhp site; update the shared client once to distribute fixes.