Vue 3 Flashcards
What is vue
Js frame work for buildin ui
Builds on top of standard html css js
provides a declarative and component-based programming model
model–view–viewmodel front end
Connects view and model with two way data binding
core features of Vue
Declarative Rendering - Reactivity
Vue extends standard HTML with a template syntax that allows us to declaratively describe HTML output based on JavaScript state.
Vue automatically tracks JavaScript state changes and efficiently updates the DOM when changes happen.
vue capabilites
Enhancing static HTML without a build step
Embedding as Web Components on any page
Single-Page Application (SPA)
Fullstack / Server-Side Rendering (SSR)
Jamstack / Static Site Generation (SSG)
Targeting desktop, mobile, WebGL, and even the terminal
Single-File Components
SFC
In most build-tool-enabled Vue projects, we author Vue components using an HTML-like file format called Single-File Component (also known as *.vue files, abbreviated as SFC). A Vue SFC, as the name suggests, encapsulates the component’s logic (JavaScript), template (HTML), and styles (CSS) in a single file
API Styles
Vue components can be authored in two different API styles: Options API and Composition API.
Options API
With Options API, we define a component’s logic using an object of options such as data, methods, and mounted. Properties defined by options are exposed on this inside functions
Composition API
With Composition API, we define a component’s logic using imported API functions. In SFCs, Composition API is typically used with
. The setup attribute is a hint that makes Vue perform compile-time transforms that allow us to use Composition API with less boilerplate. For example, imports and top-level variables / functions declared inare directly usable in the template.
options vs composition api
The Options API is centered around the concept of a “component instance” (this as seen in the example), which typically aligns better with a class-based mental model for users coming from OOP language backgrounds. It is also more beginner-friendly by abstracting away the reactivity details and enforcing code organization via option groups.
The Composition API is centered around declaring reactive state variables directly in a function scope and composing state from multiple functions together to handle complexity. It is more free-form and requires an understanding of how reactivity works in Vue to be used effectively. In return, its flexibility enables more powerful patterns for organizing and reusing logic.
The application instance
Every Vue application starts by creating a new application instance with the createApp function:
The Root Component
The object we are passing into createApp is in fact a component. Every app requires a “root component” that can contain other components as its children.
If you are using Single-File Components, we typically import the root component from another file:
import { createApp } from ‘vue’
// import the root component App from a single-file component.
import App from ‘./App.vue’
const app = createApp(App)
Mounting the App
An application instance won’t render anything until its .mount() method is called. It expects a “container” argument, which can either be an actual DOM element or a selector string:
App Configurations
The application instance exposes a .config object that allows us to configure a few app-level options, for example, defining an app-level error handler that captures errors from all descendant components:
app.config.errorHandler = (err) => {
/* handle error */
}
Make sure to apply all app configurations before mounting the app!
Template Syntax
Vue uses an HTML-based template syntax that allows you to declaratively bind the rendered DOM to the underlying component instance’s data. All Vue templates are syntactically valid HTML that can be parsed by spec-compliant browsers and HTML parsers.
Under the hood, Vue compiles the templates into highly-optimized JavaScript code. Combined with the reactivity system, Vue can intelligently figure out the minimal number of components to re-render and apply the minimal amount of DOM manipulations when the app state changes.
jsx vue
If you are familiar with Virtual DOM concepts and prefer the raw power of JavaScript, you can also directly write render functions instead of templates, with optional JSX support. However, do note that they do not enjoy the same level of compile-time optimizations as templates.
Text Interpolation
The most basic form of data binding is text interpolation using the “Mustache” syntax (double curly braces):
JavaScript expressions
In Vue templates, JavaScript expressions can be used in the following positions:
Inside text interpolations (mustaches)
In the attribute value of any Vue directives (special attributes that start with v-)
Expressions Only
Each binding can only contain one single expression. An expression is a piece of code that can be evaluated to a value. A simple check is whether it can be used after return
Calling Functions
It is possible to call a component-exposed method inside a binding expression:
Functions called inside binding expressions will be called every time the component updates, so they should not have any side effects, such as changing data or triggering asynchronous operations.
Restricted Globals Access
Template expressions are sandboxed and only have access to a restricted list of globals. The list exposes commonly used built-in globals such as Math and Date.
Globals not explicitly included in the list, for example user-attached properties on window, will not be accessible in template expressions. You can, however, explicitly define additional globals for all Vue expressions by adding them to app.config.globalProperties.
Directives
Directives are special attributes with the v- prefix. Vue provides a number of built-in directives, including v-html and v-bind which we have introduced above.
Directive attribute values are expected to be single JavaScript expressions (with the exception of v-for, v-on and v-slot, which will be discussed in their respective sections later). A directive’s job is to reactively apply updates to the DOM when the value of its expression changes.