VueScriptX is a small, focused Vue 3 plugin that facilitates asynchronous loading and execution of external JavaScript files and inline scripts directly within Vue components using a custom `<scriptx>` component or a global `$scriptx` property. It aims to reintroduce the familiar `<script>` tag behavior in a Vue-compatible way, inspired by `vue-script2`. The current stable version is 0.2.7, with an informal release cadence indicated by recent minor updates. Key differentiators include its declarative `<scriptx>` component for both ordered and asynchronous execution, support for unloading scripts when components unmount, and full TypeScript compatibility for Vue 3 projects, offering a straightforward and robust approach to managing third-party scripts in Single Page Applications (SPAs). It emphasizes a simple, web-standards-familiar approach to script injection.
npm install vue-scriptxVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to install VueScriptX as a Vue plugin, and provides examples for both declarative script loading using the `<scriptx>` component in a template and programmatic script loading via the global `$scriptx` property.
For parallel loading, use `<scriptx src="..." async></scriptx>`. For ordered execution (default), omit the `async` prop.
Ensure your project uses Vue 3 (`vue@^3.0.0`). For Vue 2, consider `vue-script2` (which inspired this package).
Use the `unload` prop for simple cleanup functions like `jQuery.noConflict(true)`. For complex logic, consider wrapping it in a function defined in your component's script or creating a custom directive.
Declare the global variables in a `d.ts` file, e.g., `declare const $: any;` or `declare interface Window { $: JQuery; }`, to inform TypeScript of their existence. Be mindful of potential naming conflicts.Create a `vue-scriptx.d.ts` (or similar) file in your project with `declare module 'vue' { interface ComponentCustomProperties { $scriptx: typeof import('vue-scriptx').ScriptXService; } }` to extend the Vue instance's types.Change your import statement from `import { ScriptX } from 'vue-scriptx';` to `import ScriptX from 'vue-scriptx';`.Add a global declaration in a `.d.ts` file (e.g., `globals.d.ts`): `declare const $: any;` or for better typing: `import type * as JQuery from 'jquery'; declare global { interface Window { $: JQuery; } }`.