Run Vue3 Project

Vue3 project init

  1. Ensure Node.js version >= 16.0;
  2. Command-line npm init vue@latest to install vue.
  • [✔] add Typscript
  • [✔] add Vue Router
  • [✔] add Pinia (state management)
  • [✔] add ESLint (code vertify)
  • [✔] add Prettier (code formatting)
  1. Add scss
    1
    npm add -D sass
  2. Run Project
    1
    2
    3
    cd [your-project-name]
    npm install
    npm run dev

Analyze Dirctory File

  1. vite.config.js

    Every time you run npm run dev, Vite will analyze this dirctory.
    You can config Package、Local service and more.

1
2
3
4
5
6
7
8
9
export default defineConfig({
plugins: [vue()],
resolve: {
alias: {
'@': fileURLToPath(new URL('./src', import.meta.url))
// Path alias(别名), Use @ replace ./scr
}
}
})
  1. Other Dirctory
    :file assets: static file, like images, css;
    :file components: public components;
    :file stores: Pinia state management;
    :file router: Vue Router file;
    :file views: vue pages;
    :file public: file which will not be packaged, like icon;

build project

1
2
3
4
5
"scripts": {
"dev": "vite",
"build": "vite build",
"preview": "vite preview --port 4173"
}
  1. npm run dev | npx vite

Run local service, and provide Hot Module Replacement (HMR热更新)

  1. npm run build | npx vite build

Build file to :file dist

  1. npm run preview | npx vite preview --port 4173

Run local service and preview the project in :file dist

why to build

  1. Syntax compilation(语法编译)
  • Trans .vue file to js and css
  • ES6 to ES5
  1. File compression(文件压缩)
  • Reduce HTTP Requests
  1. Code compression
  • Remove space、annotate(注释),Change name of variable、function
  1. Use the plugin only have used
  • Splice file from node_moudle to build file

Options API vs Composition API

Options API

According to different characteristics, split to different option, like data, method, computed, life cycle

Composition API

With <script set>, you can write code like original javascript;
According to ‘function’ to distinguish(区分) code area and It can improve readability;
Use reactive() and ref() to add relative.

distinguish


本文作者: 孟 虎
本文链接: https://menghu1994.github.io/blog/2023/10/Vue/start/
版权声明: 本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!