Hello World Vue 3
Life is short, I learn Vue. Vue is a JavaScript framework for building user interfaces. It builds on top of standard HTML, CSS, and JavaScript and provides a declarative, component-based programming model that helps you efficiently develop user interfaces of any complexity.
Basic concepts
- code language: TypeScript
- API style: Composition API
Vue 3 recommends Composition API over Options API for several key reasons:
- Better Logic Organization
Composition API lets you group related logic together (state, computed props, methods) in a cohesive way, rather than splitting them across different options blocks (data
,methods
,computed
).- TypeScript Support
Composition API works naturally with TypeScript, offering better type inference and code completion compared to Options API’sthis
-based patterns.- Code Reusability
You can extract reactive logic into composable functions that work across components, solving the mixin limitations of Options API.
- shorthand:script setup syntax
Create vue3
Two method
- vue-cli
- vite
vite:
1
npm create vue@latest
1 2 3 4 5 6 7 8 9 10 11 12 13
✔ Project name: … <your-project-name> ✔ Add TypeScript? … No / Yes ✔ Add JSX Support? … No / Yes ✔ Add Vue Router for Single Page Application development? … No / Yes ✔ Add Pinia for state management? … No / Yes ✔ Add Vitest for Unit testing? … No / Yes ✔ Add an End-to-End Testing Solution? … No / Cypress / Nightwatch / Playwright ✔ Add ESLint for code quality? … No / Yes ✔ Add Prettier for code formatting? … No / Yes ✔ Add Vue DevTools 7 extension for debugging? (experimental) … No / Yes Scaffolding project in ./<your-project-name>... Done.
Code structure
- /
./env.d.ts
: typeScript definition file./index.html
: entry html file./package.json
,./package-lock.json
: package manager file./tsconfig.json
,./tsconfig.node.json
,./tsconfig.app.json
: TypeScript configuration file./vite.config.ts
: vite configuration file, install plugin, configure server, etc./src
main.ts
: entry file1 2 3 4 5
import { createApp } from 'vue' // import the root component App from a single-file component. import App from './App.vue' createApp(App).mount('#app')
most real applications are organized into a tree of nested, reusable components. For example, a Todo application’s component tree might look like this:
1 2 3 4 5 6 7 8
App (root component) ├─ TodoList │ └─ TodoItem │ ├─ TodoDeleteButton │ └─ TodoEditButton └─ TodoFooter ├─ TodoClearButton └─ TodoStatistics
App.vue
: root component1 2 3 4 5 6 7 8 9 10 11
<template> <!-- html code --> </template> <script setup lang="ts"> // script code </script> <style scoped> /* css code */ </style>
/components
Person.vue
1 2 3 4 5 6 7 8 9 10 11
<script setup lang="ts"> // script code </script> <template> <!-- html code --> </template> <style scoped> /* css code */ </style>
Core conceptes
Composition API vs Options API
Options API: the function is split into several options, such as data
, methods
, computed
, watch
. Every time you want to change the requirement, you need to modify data
, methods
, computed
and watch
.
Composition API: the function is merged into one function.
Start with the setup function
|
|
let vs const
const
: constant, the value is not allowed to be changed, suitable for 95% situationlet
: the value is allowed to be changedvar
: old way, not recommended
function vs const + arrow function
function
: the function will be promoted to the global scope, can be used before the declarationconst + arrow function
: can’t be used before the declaration. Vue3 recommends using arrow function. Keep the same style with the Composition API.
ref: define basic reactive variables
- Function: define a reactive variable
- Syntax:
const name = ref('Jerry')
- Return: a
RefImpl
object,ref.value
is reactive - Usage:
- using
ref.value
in the js code - using
{{ ref }}
in the template - ref is not reactive, but
ref.value
is reactive
- using
reactive: define a reactive object
- Function: define a reactive object
- Syntax:
const obj = reactive({ name: 'Jerry', age: 18 })
- Return: a
Proxy
object, the properties of the object are reactive - Usage:
- using
obj.name
in the js code - using
{{ obj.name }}
in the template
- using
ref vs reactive
- basic type and object who has few levels use ref
- object who has multiple levels use reactive