The framework is based on VUe3 + Vite2, with the configuration of Element-Plus + VUE-Router +sass+vuex. It will be improved in the future
1. Initialization
- In accordance with theThe official documentationThe steps I’m using here
vite
To build the project and locallynpm
If the version is 7+, run the commandnpm init vite@latest vue3-pc -- --template vue
Initialize a project named vue3-PC npm i
As well asnpm run dev
You can run the project as shown below:
2. Introduce the Element-Plus UI
- Install using the command line
element-plus
:cnpm i --save elemnt-plus
; - Introduced the global
element-plus
: to entermain.js
The introduction ofelement-plus
According to the official quoteelement
thecss
The document reads:import 'element-plus/theme-chalk/lib/index.css'
, but the error, look atnode_modules
, the discovery oftheme-chalk
Folders are placed directly onelement-plus
In the directory, not in thelib
In theimport { createApp } from 'vue' import App from './App.vue' const app = createApp(App) import ElementPlus from 'element-plus' import 'element-plus/theme-chalk/index.css' app.use(ElementPlus) app.mount('#app') Copy the code
After following the introduction above, the page is available as follows:
<template> <h1>{{ msg }}</h1> <el-button type="primary" @click="count++">count is: {{ count }}</el-button> </template> Copy the code
After running, open the page to see the effect
- Test build: Run
vite build
Script, successfully packaged generationdist
Folder, go todist
In the usehs
Simulation startup, startup success, as shown in the following figureIf you open the IP address, you can access it normally
Introduce SASS
- In turn, installation
node-sass
:cnpm i node-sass --save-dev
.sass
:cnpm i sass --save-dev
It was too slow to install one by one. I installed the last few at a time:cnpm i sass-loader style-loader style-resources-loader --save-dev
- After the previous installation is successful, use it partially on the page
scss
$text-color: purple; .msg{ color: $text-color; } Copy the code
Run and see the effect on the page:
- Based on the above, global introduction is required
scss
Style and define global variables inassets
Create a new file, place the global style, and thenvite.config.js
The introduction ofglobalExternal styleimport { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' // https://vitejs.dev/config/ export default defineConfig({ css: { preprocessorOptions: { scss: { additionalData: `@import "./src/assets/scss/variable.scss"; `}}},pluginOptions: { 'style-resources-loader': { preProcessor: 'sass'.patterns: []}},plugins: [ vue(), ], build: {}})Copy the code
variable.scss
Global variables are defined in$color-primary: #28cccc
; Use it directly on the pagecolor: $color-primary;
The effect is as follows: - After the above configuration is complete,
build
Then run again, no problem - In Step 3, if you want to use the @ alias when importing global SCSS files, you can also configure it as follows
import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import path from 'path' // https://vitejs.dev/config/ export default defineConfig({ resolve: { alias: { The '@': path.resolve(__dirname, 'src'), 'views': path.resolve(__dirname, 'src/views')}}})Copy the code
After the alias is configured, restart the project.
- I saw something online that needs to be introduced separately
path
Module, very strange, I do not need to also successfully configure alias, first put a doubt, behind to follow up under ~
4. Introduce vue-Router
-
CNPM I vue-router@next –save Install dependencies
-
In the SRC directory, create a router folder to store routes and add the following code to index.js. At the same time, create the home page and login page
import { createRouter, createWebHistory } from 'vue-router' export default createRouter({ history: createWebHistory(), routes: [{path: '/'.component: () = > import('views/index.vue')}, {path: '/login'.component: () = > import('views/login.vue')}]})Copy the code
-
Import the route in the entry file main.js with the following code:
import { createApp } from 'vue' import App from './App.vue' import router from './router/index' import ElementPlus from 'element-plus' import 'element-plus/theme-chalk/index.css' const app = createApp(App) app.use(ElementPlus) app.use(router) app.mount('#app') Copy the code
-
Add a
tag to app. vue -
Reboot to see the effect
5. Introduce Vuex state tree management
- Install dependencies:
cnpm i vuex@next --save
.Click to jump to the official Vuex document vue3
The correspondingVuex
Upgrade to4.xVersion, also used in writingModular APIThe writing; The followingVuex
Example, using the module with namespace, see detailsThe official documentation, Vuex also links to some cases on its website:Github example address;
The directory structure of the example is divided into two modulesmodules
In theuserInfo
The module andcommon
Module to set the namespace:namespaced: true
, and the rest are written as followsVuex3.x
The same,index.js
The introduction method is throughcreateStore({modules: {XXX}})
To introduceIn 3.main.js
, the code is as follows:
import { createApp } from 'vue'
import App from './App.vue'
import router from './router/index'
import ElementPlus from 'element-plus'
import 'element-plus/theme-chalk/index.css'
import store from './store/index'
const app = createApp(App)
app.use(ElementPlus)
app.use(router)
app.use(store)
app.mount('#app')
Copy the code
- In the page file, his call method basically follows
Vuex3.x
Consistent, but differentVue4.x
Additional calls to named modules;- If through
store
To get the status value, you need to be insetup(){}
In thereturn
Come out - To use a method in a module with a namespace, you need to pass in the module name as follows:
mapMutations
withmapActions
Both specify a module with a namespace by passing the first parameter;
import { mapMutations, mapState } from 'vuex' export default { setup () { }, data() { return { title: 'Login page'}},computed: { ...mapState({ userId: state= > state.userInfo.userId }) }, methods: { ...mapMutations('userInfo'['SET_USERID',])}}Copy the code
- References to namespaced modules can also pass
createNameSpacedHelpers
toCreate namespace-based helper functions, it returnsAn objectObject containingmapState
‘mapMutations’ and other auxiliary functions
import { createNamespacedHelpers } from 'vuex' const { mapMutations, mapState } = createNamespacedHelpers('userInfo') export default { setup () { }, data() { return { title: 'Login page'}},computed: { ...mapState({ userId: state= > state.userId }) }, methods: { ...mapMutations(['SET_USERID'])}}Copy the code
- If through
- You can see it when it runs
6. As for persistent storage, there is no configuration here. You can install this plug-in by yourselfvuex-persistedstate
Six, other configuration
Eslint (standard code format) and HUSKY (Submit automatic validation code format) will be added later, see the next article;