Vue 01
Why Vue? I figured, what if I could just extract the part that I really liked about React and build something really lightweight without all the extra concepts involved? I was also curious as to how its internal implementation worked. I started this experiment just trying to replicate this minimal feature set, like declarative data binding. That was basically how Vue started.
-- Evan You
Like React.js - but simplified.
Creating app
Vite based (10-100x faster dev server than Webpack, but has some issues)
npm create vue@latest
cd your_project
npm install
npm run dev
Basics
main.ts
1 2 3 4 5 |
|
App.vue
1 2 3 4 5 6 7 8 |
|
Template in index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 |
|
Templating
Vue uses an HTML-based template syntax - under the hood, Vue compiles the templates into highly-optimized JavaScript code.
Two main ways how to write vue components:
- options api
- composition api
Common style is to write single file components (SFC), known as *.vue files.
A Vue SFC encapsulates the component's logic (JavaScript), template (HTML), and styles (CSS) in a single file.
Options API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
|
Composition API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
|
Options Api - component instance (this
is used). Maybe better for OOP people.
Composition Api - declaring reactive state variables directly (ref()
).
Options API is implemented on top of the Composition API...
Official recommendation:
Go with Composition API + Single-File Components if you plan to build full applications with Vue.
Reactivity - ref()
1 2 3 |
|
ref()
takes the argument and returns it wrapped within a ref object with a .value property.
Typing ref
1 2 |
|
Reactivity - reactive()
ref()
wraps the inner value in a special object. reactive()
makes an object itself reactive.
1 2 3 4 5 6 7 8 |
|
Computed properties
The computed() function expects to be passed a getter function, and the returned value is a computed ref.
1 2 3 4 |
|
Computed properties are cached based on their reactive dependencies.
Computed properties are by default getter-only.
1 2 3 4 5 6 7 8 9 10 11 |
|
NB!
Getters should be side-effect free.
Avoid mutating computed value.
Component Structure Overview
3 main parts - Template, Script, Style. Vue3 – allows multiple root elements in template.
1 2 3 4 5 6 7 8 9 10 11 |
|
Using component
Main style is SFC - single file component. Template and Script and Style in on file.
ButtonCounter.vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
App.Vue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Properties in components
1 2 3 4 5 6 7 8 |
|
Default values for properties
1 2 3 4 5 6 7 8 9 |
|
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Directives in templates
- Directives are using v- prefix.
v-for
,v-bind
,v-on
,v-if
, etc. - Some directives take an argument, using colon
v-on:click="methodName"
- Shorthands
v-bind
-<a :href="url"> ... </a>
v-on -<a @click="doSomething"> ... </a>
Bindings in template
- Text interpolation with
{{variableName}}
<span>Message: {{ msg }}</span>
- One-time interpolation with
v-once
<span v-once>This will never change: {{ msg }}</span>
- Raw-html binding with
v-html="variableName"
<p>Using v-html directive: <span v-html="rawHtml"></span></p>
- Binding in html attributes with
v-bind="variableName"
<div v-bind:id="dynamicId"></div>
- Vue supports JavaScript expressions inside all data bindings
<div v-bind:id="list- + id"></div>
Conditional rendering – v-if, v-else, v-else-if
Hidding element – v-show
only toggles the display CSS property of the element.
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Rendering a list
- v-for directive to render a list of items based on an array. The v-for directive requires a special syntax in the form of item in items, where items is the source data array and item is an alias for the array element being iterated on. Use
(item, index)
to get counter also. - Also use
v-for
to iterate through the properties of an object. v-for
can also take an integer. In this case it will repeat the template that many times.
1 2 3 4 5 |
|
Dom events
Bind to the dom events with v-on or with shortcut @
. Use special variable to reference js event data.
1 2 3 4 5 6 7 8 9 |
|
Event modifiers
Vue provides event modifiers for v-on. Recall that modifiers are directive postfixes denoted by a dot.
- .stop
- .prevent
- .self
- .capture
- .once
- .passive
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
The .capture, .once, and .passive modifiers mirror the options of the native addEventListener method.
Form bindings
Manual vay
1 2 3 |
|
Use the v-model
directive instead to create two-way data bindings on form input, textarea, and select elements. It automatically picks the correct way to update the element based on the input type.
<input v-model="message" placeholder="edit me">
Custom events
A component can emit custom events directly in template expressions (e.g. in a v-on handler) using the built-in $emit
method:
1 |
|
And parent can listen
1 |
|
NB! Unlike native DOM events, component emitted events do not bubble.
Send and receive data with event
1 2 3 |
|
$emit
is not accessible in <script>
section.
use defineEmits macro and emit
function it returns.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
Watchers
With Composition API, we can use the watch function to trigger a callback whenever a piece of reactive state changes.
1 2 3 4 5 6 7 8 |
|
watch
's first argument can be different types of reactive "sources": it can be a ref (including computed refs), a reactive object, a getter function, or an array of multiple sources.
Component v-model
By default, v-model on a component uses modelValue
as the prop and update:modelValue
as the event. We can modify these names passing an argument to v-model:
1 |
|
and inside component
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Component content - slot
1 2 3 |
|
1 2 3 4 5 |
|
NB! Slot content has access to the data scope of the parent component, because it is defined in the parent. Does not have access to child scope!
Vue Lifecycle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|