Vue

Concepts

A .vue files may have HTML, CSS, JS template syntax inside. <template></template> is for HTML structure, <style></style> is for CSS style, <script></script> is for JavaScript script.

Vue have two code modes, Options and Composition Mode. But in this documentation. We use composition

OptionsComposition
Older and more structuralNewer and more flexible
More codes, with export in script for setupLess code, using <script setup>

Getting started

Run the code below to get started

npm install -g @vue/cli
vue create my-vue-app
cd my-vue-app
npm run dev
# or
npm run serve
# For deployment
npm run build

Then, select manually select feature to suits your need. Router, Pinia (global state management) is recommended. Starter template will be at port 8080

Project Structure
vue-app/
├── public/
├── src/
│   ├── assets/
│   ├── components/ # For reusable components
│   │   └── ButtonComponent.vue 
│   ├── router/
│   │   └── index.js
│   ├── store/
│   │   └── index.js
│   ├── views/ # For pages
│   │   ├── HomeView.vue
│   │   └── AboutView.vue
│   ├── App.vue
│   └── main.js
├── .gitignore
├── package.json
└── vue.config.js

Tutorial

This briefly shows some fundamentals concepts on Vue in two files. The concepts are declarative rendering, attribute bindings, event listeners, form bindings, conditional rendering, list rendering, computed property, lifecycle and template refs, watchers, components, props, emits, slots.

Show code

Router

To control and remember URL routes on the site

Router Configurations

The structure are as below. Configure index.js, add views and make sure main.js imports router to be use

src/router/index.js
import { createRouter, createWebHashHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import HomeView from '../views/LoginView.vue'

// You have two ways to configure router
const routes = [
  {
    path: '/', redirect: '/about/home', // Default route
  },
  {
    path: '/about', name: 'about',
    // Nested routes
    children: [
      { path: 'home', component: HomeView },
      { path: 'login', component: LogInView },
    ],
    // Route level code-splitting: this generates a separate chunk about.[hash].js)
    component: () => import(/* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

export default router
<script>
  import { useRouter } from 'vue-router';
  const router = useRouter();

  router.push('/path'); // Move to a route
</script>

<template>
  <router-link to="/path"><a> Link </a></router-link>
  <router-view> Router views will be rendered here </router-view>
</template>
src/views/HomeView.vue
<script setup>
// @ is an alias to /src
import HelloWorld from '@/components/HelloWorld.vue'
</script>

<template>
  <div class="home">
    <HelloWorld msg="Welcome to Your Vue.js App"/>
  </div>
</template>
src/views/AboutView.vue
<template>
  <div class="about">
    <h1>This is an about page. Without importing any other components</h1>
  </div>
</template>

Pinia

Am official global state management tool for vue

Pinia Configurations

Install pinia through npm

src/store/index.js
import { defineStore } from 'pinia';

export const useGlobalStore = defineStore('global', {
  state: () => ({
    // The global value
    value: null, 
    uname: null
  }),
  getters: {
    getValue: (state) => state.value,
    getUname: (state) => state.uname
  },
  actions: {
    setValue(newValue) {
      this.value = newValue;
    },
    setUname(uname) {
      this.uname = uname;
    },
    clear() {
      this.value = null;
      this.uname = null;
    }
  },
});

PrimeVue

Primevue is a framework to make modern UI by adding components into vue files. Visit PrimeVue to get started.