### Navigate, Install Dependencies, and Run Dev Server Source: https://github.com/vuejs/docs/blob/main/src/guide/quick-start.md After scaffolding a new Vue project, navigate into the project directory, install the necessary dependencies, and then start the local development server. This allows you to view and test your application during development. Instructions are provided for npm, pnpm, Yarn, and Bun. ```npm $ cd {{''}} $ npm install $ npm run dev ``` ```pnpm $ cd {{''}} $ pnpm install $ pnpm run dev ``` ```yarn $ cd {{''}} $ yarn $ yarn dev ``` ```bun $ cd {{''}} $ bun install $ bun run dev ``` -------------------------------- ### Create Vue 3 App with Global Build and Options API Source: https://github.com/vuejs/docs/blob/main/src/guide/quick-start.md This example illustrates how to set up a basic Vue 3 application using the global build from a CDN, leveraging the Options API. It initializes a Vue app, defines reactive data, and mounts it to an HTML element, showcasing a simple 'Hello Vue!' message. ```html
{{ message }}
``` -------------------------------- ### Returning Template Ref from `setup()` in Vue Composition API (without ` ``` ```vue ``` -------------------------------- ### Create Vue 3 App with Global Build and Composition API Source: https://github.com/vuejs/docs/blob/main/src/guide/quick-start.md This snippet demonstrates building a Vue 3 application using the global build from a CDN, employing the Composition API with the setup() option. It shows how to declare reactive state using ref and expose it to the template, mounting the application to an HTML element. ```html
{{ message }}
``` -------------------------------- ### Configure Vue Import Map and Initialize App (Composition API) Source: https://github.com/vuejs/docs/blob/main/src/guide/quick-start.md This HTML snippet demonstrates configuring an import map for the 'vue' module from a CDN. It then initializes a basic Vue application using the Composition API with a `setup()` function, mounting it to a div with id 'app' and displaying a reactive message. ```html
{{ message }}
``` -------------------------------- ### Create Vue 3 App with ES Module Build and Composition API Source: https://github.com/vuejs/docs/blob/main/src/guide/quick-start.md This snippet demonstrates building a Vue 3 application using the ES module build from a CDN, implementing the Composition API with setup(). It uses ``` -------------------------------- ### Vue Composition API: useCssModule in Script Setup Example Source: https://github.com/vuejs/docs/blob/main/src/api/sfc-css-features.md Provides a complete example of using `useCssModule` within a ` ``` -------------------------------- ### Vue Script Setup with Async Component Import Source: https://github.com/vuejs/docs/blob/main/src/examples/index.md Demonstrates the complete script section of a Vue component that registers an async component with lazy loading. Uses defineAsyncComponent to defer loading of ExampleRepl.vue and provides visual feedback during the loading phase with a ReplLoading component. ```vue ``` -------------------------------- ### Create Vue 3 App with ES Module Build and Options API Source: https://github.com/vuejs/docs/blob/main/src/guide/quick-start.md This example shows how to create a Vue 3 application using the ES module build from a CDN, utilizing the Options API. It employs ``` -------------------------------- ### Define Vue Component with TypeScript and Composition API `setup` Source: https://github.com/vuejs/docs/blob/main/src/guide/typescript/overview.md This example illustrates how `defineComponent()` facilitates type inference for props when using the Composition API with a `setup` function, specifically without ` ``` -------------------------------- ### Call Composables in Options API setup() Method Source: https://github.com/vuejs/docs/blob/main/src/guide/reusability/composables.md Demonstrates how to use composables within the Options API by calling them inside the setup() function and returning their bindings. The returned properties are exposed to the component instance via 'this' and are accessible in the template. This example imports two composables (useMouse and useFetch) and exposes their returned values. ```javascript import { useMouse } from './mouse.js' import { useFetch } from './fetch.js' export default { setup() { const { x, y } = useMouse() const { data, error } = useFetch('...') return { x, y, data, error } }, mounted() { // setup() exposed properties can be accessed on `this` console.log(this.x) } // ...other options } ``` -------------------------------- ### Scaffold Vue Application using create-vue with npm, pnpm, or yarn Source: https://github.com/vuejs/docs/blob/main/src/guide/quick-start.md This command-line snippet demonstrates how to initialize a new Vue.js Single Page Application (SPA) on your local machine. It utilizes `create-vue` to set up a project with a build configuration based on Vite, enabling the use of Vue Single-File Components (SFCs). Ensure Node.js version `^20.19.0 || >=22.12.0` is installed before running. ```sh $ npm create vue@latest ``` ```sh $ pnpm create vue@latest ``` ```sh # For Yarn (v1+) $ yarn create vue ``` ```sh # For Yarn Modern (v2+) $ yarn create vue@latest ``` -------------------------------- ### Basic setup() with Reactive State in Vue Source: https://github.com/vuejs/docs/blob/main/src/api/composition-api-setup.md Demonstrates declaring reactive state using ref() and exposing it to the template by returning an object from setup(). The returned properties are available in the template and other Options API hooks. Refs are automatically unwrapped in templates. ```vue ``` -------------------------------- ### Vue.js Script Setup: Declaring Basic Generic Type Parameters Source: https://github.com/vuejs/docs/blob/main/src/api/sfc-script-setup.md This example illustrates how to declare generic type parameters using the `generic` attribute on the ` ``` -------------------------------- ### Composition API Component with Lifecycle Hooks Source: https://github.com/vuejs/docs/blob/main/src/guide/introduction.md A comprehensive Composition API example using ` ``` -------------------------------- ### Basic ``` -------------------------------- ### Destructure Setup Context Parameters Source: https://github.com/vuejs/docs/blob/main/src/api/composition-api-setup.md Demonstrates destructuring the setup context object to directly access attrs, slots, emit, and expose. Since the context is non-reactive, destructuring is safe and recommended for cleaner code. ```javascript export default { setup(props, { attrs, slots, emit, expose }) { ... } } ``` -------------------------------- ### Declare Type-Only Props and Emits in Vue 3 Script Setup (TypeScript) Source: https://github.com/vuejs/docs/blob/main/src/api/sfc-script-setup.md This example illustrates how to declare props and emits using a pure-type syntax with `defineProps` and `defineEmits` in Vue 3's ` ``` ### Response #### Success Response (N/A) The variable will hold the resolved value of the awaited expression. #### Response Example ```javascript const post = { id: 1, title: "My fetched post" } ``` ``` -------------------------------- ### Access Setup Context Object in setup() Function Source: https://github.com/vuejs/docs/blob/main/src/api/composition-api-setup.md Shows how to access the setup context object passed as the second argument to setup(). The context provides attrs, slots, emit, and expose properties. The context object is non-reactive and can be safely destructured. ```javascript export default { setup(props, context) { // Attributes (Non-reactive object, equivalent to $attrs) console.log(context.attrs) // Slots (Non-reactive object, equivalent to $slots) console.log(context.slots) // Emit events (Function, equivalent to $emit) console.log(context.emit) // Expose public properties (Function) console.log(context.expose) } } ``` -------------------------------- ### Extend Component with Composition API Setup Source: https://github.com/vuejs/docs/blob/main/src/api/options-composition.md Shows how to extend a component using Composition API by manually calling the base component's setup() function within the extending component's setup(). This workaround merges logic from both components while using Composition API. ```javascript import Base from './Base.js' export default { extends: Base, setup(props, ctx) { return { ...Base.setup(props, ctx), // local bindings } } } ``` -------------------------------- ### Expose Methods with Render Function in Vue 3 setup Source: https://github.com/vuejs/docs/blob/main/src/api/composition-api-setup.md This snippet shows how to expose methods to a parent component when returning a render function from Vue 3's `setup`. By utilizing the `expose` function provided in the setup context, methods like `increment` can be made available via template refs, even while `setup` returns a render function for component rendering. ```js import { h, ref } from 'vue' export default { setup(props, { expose }) { const count = ref(0) const increment = () => ++count.value expose({ increment }) return () => h('div', count.value) } } ``` -------------------------------- ### Install Vue.js Plugin with app.use() Source: https://github.com/vuejs/docs/blob/main/src/guide/reusability/plugins.md Demonstrates the basic syntax for installing a plugin into a Vue application. The app.use() method accepts a plugin and optional configuration options that are passed to the plugin's install function. ```javascript import { createApp } from 'vue' const app = createApp({}) app.use(myPlugin, { /* optional options */ }) ``` -------------------------------- ### Import and Use Components in Vue ``` -------------------------------- ### Inject data in Composition API setup function Source: https://github.com/vuejs/docs/blob/main/src/guide/components/provide-inject.md Use the inject() function inside the setup() function to receive provided data. This approach is used when not using script setup syntax. The inject() call must be made synchronously within setup(). ```javascript import { inject } from 'vue' export default { setup() { const message = inject('message') return { message } } } ``` -------------------------------- ### Return Render Function from Vue 3 setup Source: https://github.com/vuejs/docs/blob/main/src/api/composition-api-setup.md This snippet demonstrates how to return a render function directly from Vue 3's `setup` function. It uses the `h` function to create a `div` element that displays a reactive `count` value. This approach is useful for highly dynamic rendering but prevents returning other properties or methods directly from `setup`. ```js import { h, ref } from 'vue' export default { setup() { const count = ref(0) return () => h('div', count.value) } } ``` -------------------------------- ### Import Helper Functions in Vue ``` -------------------------------- ### Expose Refs in Component setup() Function Source: https://github.com/vuejs/docs/blob/main/src/guide/essentials/reactivity-fundamentals.md Demonstrates how to declare refs in a component's setup() function and return them to make them accessible in the component's template. The setup() hook is the entry point for Composition API in Vue components. ```javascript import { ref } from 'vue' export default { setup() { const count = ref(0) return { count } } } ``` -------------------------------- ### Declare Props with Array Syntax in non-script setup Composition API Source: https://github.com/vuejs/docs/blob/main/src/guide/components/props.md In non-` ``` -------------------------------- ### Define Vue.js Plugin with Install Method Source: https://github.com/vuejs/docs/blob/main/src/guide/reusability/plugins.md Shows the standard plugin structure as an object with an install() method. The install function receives the app instance and any additional options passed during plugin registration. ```javascript const myPlugin = { install(app, options) { // configure the app } } ``` -------------------------------- ### Provide Data in Vue Composition API setup function Source: https://github.com/vuejs/docs/blob/main/src/guide/components/provide-inject.md Illustrates how to use the `provide()` function when not utilizing ` ``` -------------------------------- ### Create Reactive State with ref in Vue ``` -------------------------------- ### Import Demo Component for Vue KeepAlive Examples Source: https://github.com/vuejs/docs/blob/main/src/guide/built-ins/keep-alive.md This snippet imports `SwitchComponent.vue` using ` ``` -------------------------------- ### Execute Server-Side Rendering Script Source: https://github.com/vuejs/docs/blob/main/src/guide/scaling-up/ssr.md Run the `example.js` file to execute the server-side rendering process and print the resulting HTML to the console. ```sh > node example.js ``` -------------------------------- ### Configure isCustomElement in Vue CLI Source: https://github.com/vuejs/docs/blob/main/src/guide/extras/web-components.md Use this vue.config.js setup with Vue CLI to specify that tags starting with 'ion-' should be recognized as custom elements, preventing Vue from attempting to resolve them as Vue components. ```js module.exports = { chainWebpack: (config) => { config.module .rule('vue') .use('vue-loader') .tap((options) => ({ ...options, compilerOptions: { // treat any tag that starts with ion- as custom elements isCustomElement: (tag) => tag.startsWith('ion-') } })) } } ``` -------------------------------- ### Define Event Handler in Vue Composition API Source: https://github.com/vuejs/docs/blob/main/src/tutorial/src/step-4/description.md This example demonstrates how to define an event handler function (`increment`) using Vue's Composition API. It shows two common patterns: directly within ` ``` ```js setup() { const count = ref(0) function increment(e) { // update component state count.value++ } return { count, increment } } ``` -------------------------------- ### Initialize todos data (Options API) Source: https://github.com/vuejs/docs/blob/main/src/style-guide/rules-essential.md Example data structure for demonstrating v-for and key usage with a list of todo items. ```js data() { return { todos: [ { id: 1, text: 'Learn to use v-for' }, { id: 2, text: 'Learn to use key' } ] } } ``` -------------------------------- ### Implement read-only and writable computed properties in Vue.js Source: https://github.com/vuejs/docs/blob/main/src/api/options-state.md Practical example demonstrating both read-only and writable computed properties in a Vue.js component. Shows how to define a simple computed getter (aDouble) and a writable computed property with both get and set methods (aPlus). The example includes lifecycle hook usage to demonstrate computed property behavior. ```javascript export default { data() { return { a: 1 } }, computed: { // readonly aDouble() { return this.a * 2 }, // writable aPlus: { get() { return this.a + 1 }, set(v) { this.a = v - 1 } } }, created() { console.log(this.aDouble) // => 2 console.log(this.aPlus) // => 2 this.aPlus = 3 console.log(this.a) // => 2 console.log(this.aDouble) // => 4 } } ``` -------------------------------- ### Build Vue Application for Production Deployment Source: https://github.com/vuejs/docs/blob/main/src/guide/quick-start.md Generate a production-ready build of your Vue application. This command compiles and optimizes your project's assets, placing the output in the `./dist` directory, ready for deployment to a web server. This process is supported across npm, pnpm, Yarn, and Bun. ```npm $ npm run build ``` ```pnpm $ pnpm run build ``` ```yarn $ yarn build ``` ```bun $ bun run build ``` -------------------------------- ### Implement Computed Property for Conditional Logic in Vue Composition API Source: https://github.com/vuejs/docs/blob/main/src/guide/essentials/computed.md This example demonstrates creating a reactive computed property using Vue's Composition API with ' ``` -------------------------------- ### Simplify Composition API with script setup Syntax Source: https://github.com/vuejs/docs/blob/main/src/guide/essentials/reactivity-fundamentals.md Shows the modern ``` -------------------------------- ### Define Custom Directive with Composition API Source: https://github.com/vuejs/docs/blob/main/src/guide/reusability/custom-directives.md Creates a custom directive using the Composition API with ``` -------------------------------- ### Setup Composition API - Conditional Rendering State Source: https://github.com/vuejs/docs/blob/main/src/guide/essentials/conditional.md Example of initializing reactive state using Vue's Composition API with the ref function. This state can be used with conditional rendering directives like v-if and v-show to control element visibility. ```javascript import { ref } from 'vue' const awesome = ref(true) ``` -------------------------------- ### Access v-model Modifiers with defineModel() Source: https://github.com/vuejs/docs/blob/main/src/api/sfc-script-setup.md Destructure the return value of defineModel() to access modifiers used with the v-model directive. Use get and set transformer options to transform values when reading or syncing back to the parent, such as applying the trim modifier. ```javascript const [modelValue, modelModifiers] = defineModel() // corresponds to v-model.trim if (modelModifiers.trim) { // ... } const [modelValue, modelModifiers] = defineModel({ // get() omitted as it is not needed here set(value) { // if the .trim modifier is used, return trimmed value if (modelModifiers.trim) { return value.trim() } // otherwise, return the value as-is return value } }) ``` -------------------------------- ### Access Previous Value in Computed Property - Composition API Source: https://github.com/vuejs/docs/blob/main/src/guide/essentials/computed.md Shows how to access the previous value of a computed property using Vue's Composition API with the ` ``` -------------------------------- ### Scaffold a New Vue Project with create-vue Source: https://github.com/vuejs/docs/blob/main/src/guide/quick-start.md Use the official `create-vue` tool to initialize a new Vue.js project. This command executes the scaffolding tool, which then prompts the user for various optional features like TypeScript, JSX, routing, state management, testing, ESLint, and Prettier. This snippet demonstrates usage with Yarn and Bun package managers. ```yarn $ yarn dlx create-vue@latest ``` ```bun $ bun create vue@latest ``` -------------------------------- ### Create a Minimal Vue.js Application (Options API, Composition API, and Template) Source: https://github.com/vuejs/docs/blob/main/src/guide/introduction.md This example demonstrates how to set up a basic Vue.js application using both the Options API and the Composition API, along with the corresponding HTML template. It showcases declarative rendering and reactivity by displaying a clickable button that increments a counter. The application mounts to an HTML element with the ID 'app'. ```js import { createApp } from 'vue' createApp({ data() { return { count: 0 } } }).mount('#app') ``` ```js import { createApp, ref } from 'vue' createApp({ setup() { return { count: ref(0) } } }).mount('#app') ``` ```vue-html
``` -------------------------------- ### Configure Vite Alias for Vue in HTML-mode Source: https://github.com/vuejs/docs/blob/main/src/tutorial/src/step-1/description.md This JavaScript configuration snippet for `vite.config.js` shows how to set up a resolve alias for Vue. It directs the 'vue' import to the `vue.esm-bundler.js` file, which is crucial for ensuring proper Vue resolution when working in HTML-mode without a build step in a Vite project. ```javascript export default { resolve: { alias: { vue: 'vue/dist/vue.esm-bundler.js' } } } ``` -------------------------------- ### Client Entry Point (client.js) Source: https://github.com/vuejs/docs/blob/main/src/guide/scaling-up/ssr.md Imports the universal app creation logic and mounts the Vue application to the DOM on the client side. ```js import { createApp } from './app.js' createApp().mount('#app') ``` -------------------------------- ### Vue Single-File Component with Composition API Source: https://github.com/vuejs/docs/blob/main/src/guide/introduction.md A complete Single-File Component using the Composition API style with ` ``` -------------------------------- ### Configure Vue Import Map and Initialize App (Options API) Source: https://github.com/vuejs/docs/blob/main/src/guide/quick-start.md This HTML snippet shows how to define an import map within a script tag to resolve the 'vue' module from a CDN URL. It then initializes a basic Vue application using the Options API, mounting it to a div with id 'app' and displaying a reactive message. ```html
{{ message }}
``` -------------------------------- ### Handle Event Listeners with v-on in Render Functions Source: https://github.com/vuejs/docs/blob/main/src/guide/extras/render-function.md Props starting with 'on' followed by an uppercase letter are treated as event listeners in render functions. The onClick prop is equivalent to @click in templates. Demonstrates basic event listener setup in both h() function and JSX syntax. ```javascript h( 'button', { onClick(event) { /* ... */ } }, 'Click Me' ) ``` ```jsx ``` -------------------------------- ### Using $ref for Reactive Variables in Vue Composition API Source: https://github.com/vuejs/docs/blob/main/src/guide/extras/reactivity-transform.md This example demonstrates the use of the `$ref` compile-time macro within a Vue ` ``` -------------------------------- ### Include Vue 3 Global Build from CDN Source: https://github.com/vuejs/docs/blob/main/src/guide/quick-start.md This snippet demonstrates how to include the Vue 3 global build directly into an HTML page using a ``` -------------------------------- ### Dynamic Components with :is Binding in Vue Source: https://github.com/vuejs/docs/blob/main/src/api/sfc-script-setup.md Shows how to use dynamic components with the :is binding in ``` -------------------------------- ### Expose Public Properties from Component Instance Source: https://github.com/vuejs/docs/blob/main/src/api/composition-api-setup.md Shows how to use the expose() function to explicitly control which properties are accessible to parent components via template refs. This allows limiting the public API of a component by selectively exposing only certain properties. ```javascript export default { setup(props, { expose }) { // make the instance "closed" - // i.e. do not expose anything to the parent expose() const publicCount = ref(0) const privateCount = ref(0) // selectively expose local state expose({ count: publicCount }) } } ``` -------------------------------- ### Render dynamic components by definition in Vue Composition API Source: https://github.com/vuejs/docs/blob/main/src/api/built-in-special-elements.md This Vue Composition API example, utilizing ` ``` -------------------------------- ### Import Vue ES Module for HTML-mode without Build Step Source: https://github.com/vuejs/docs/blob/main/src/tutorial/src/step-1/description.md This JavaScript import statement demonstrates how to directly import Vue's ES module bundler distribution. This approach is necessary when using Vue in HTML-mode without a build step, ensuring the browser can correctly resolve the Vue library. ```javascript import { ... } from 'vue/dist/vue.esm-bundler.js' ``` -------------------------------- ### Vue Component Definition (Composition API) for Module Splitting Source: https://github.com/vuejs/docs/blob/main/src/guide/quick-start.md This JavaScript file defines a reusable Vue component using the Composition API. It exports a default object with a `setup()` function that uses `ref` for reactive state and a `template` string for its structure. This component is designed to be imported as a module into a main application file. ```js import { ref } from 'vue' export default { setup() { const count = ref(0) return { count } }, template: `
Count is: {{ count }}
` } ``` -------------------------------- ### Initialize todos data (Composition API) Source: https://github.com/vuejs/docs/blob/main/src/style-guide/rules-essential.md Example reactive data structure for demonstrating v-for and key usage with a list of todo items. ```js const todos = ref([ { id: 1, text: 'Learn to use v-for' }, { id: 2, text: 'Learn to use key' } ]) ``` -------------------------------- ### Define and Consume Shared Reactive Store in Vue Source: https://github.com/vuejs/docs/blob/main/src/guide/scaling-up/state-management.md This snippet demonstrates how to create a globally shared reactive store using Vue's `reactive()` API and consume it in different components. It shows examples for both Composition API (` ``` ```vue ``` ```vue ``` ```vue ``` -------------------------------- ### Access Props in Setup Function without Script Setup Source: https://github.com/vuejs/docs/blob/main/src/guide/essentials/component-basics.md Shows how to access props in the setup() function when not using ``` -------------------------------- ### Access Fallthrough Attributes in Composition API setup() Context Source: https://github.com/vuejs/docs/blob/main/src/guide/components/attrs.md Accesses fallthrough attributes via the `ctx.attrs` property in the `setup()` function context parameter. Provides access to all fallthrough attributes in the setup function without using `