SoFunction
Updated on 2025-04-04

Detailed explanation of the practical application of vite2.0+vue3 mobile terminal project

1. Technological points involved

  • vite version
  • vue3
  • ts
  • Integrated routing
  • Integrated vuex
  • Integrated axios
  • Configure Vant3
  • Mobile adaptation
  • Request a proxy

2. Steps

vite+ts+vue3 only requires one line of command

npm init @vitejs/app my-vue-app --template vue-ts

Configure routing

npm install vue-router@4 --save

Create a new router directory under src and create a new file

import { createRouter, createWebHashHistory, RouteRecordRaw } from "vue-router";
const routes: Array<RouteRecordRaw> = [
 {
 path: "/",
 name: "Home",
 meta: {
  title: "front page",
  keepAlive: true
 },
 component: () => import("../views/Home/"),
 },
 {
 path: "/login",
 name: "Login",
 meta: {
  title: "Log in",
  keepAlive: true
 },
 component: () => import("../views/Login/"),
 },
];
const router = createRouter({
 history: createWebHashHistory(),
 routes
});
export default router;

Mounting the route

import { createApp } from 'vue'
import App from './'
import router from "./router";createApp(App)
.use(router)
.mount('#app')

Configure data center vuex()

Install

npm i vuex@next --save

Configuration

Create the store directory under src and create it under store

import { createStore } from "vuex";
export default createStore({
 state: {
 listData:{1:10},
 num:10
 },
 mutations: {
 setData(state,value){
  =value
 },
 addNum(state){
  =+10
 }
 },
 actions: {
 setData(context,value){
  ('setData',value)
 },
 },
 modules: {}
});

Mount

Mounting the data center

import { createApp } from 'vue'
import App from './'
import router from "./router";
import store from "./store";
createApp(App)
.use(router)
.use(store)
.mount('#app')

Vant3

Install

npm i vant@next -S

The vite version does not require the on-demand loading of components, because all internal modules in Vant 3.0 are written based on ESM and naturally have the ability to introduce on-demand, but all styles must be introduced 137.2k.

Introducing styles globally

import { createApp } from 'vue'
import App from './'
import router from "./router";
import store from "./store";
import 'vant/lib/'; // Global introduction of stylescreateApp(App)
.use(router)
.use(store)
.use(ant)
.mount('#app')

Mobile adaptation

Install postcss-pxtorem

npm install postcss-pxtorem -D

Create in the root directory

 = {
 "plugins": {
 "postcss-pxtorem": {
  rootValue: 37.5, 
  // The official root font size of Vant is 37.5  propList: ['*'],
  selectorBlackList: ['.norem'] 
  // Filter out the class starting with .norem- and do not convert rem }
 }
}

Create new util directory in root directory src create new and other adapter files under the util directory

// Rem etc. adapt to configuration file//Basic sizeconst baseSize = 37.5 
// Note that this value should be consistent with the rootValue in the file// Set the rem functionfunction setRem () {
 // The current page width is 375 in size and can be modified according to your needs. Generally, the design draft is 750 in width (you can get the design drawing and modify it). const scale =  / 375
 // Set the font size of the page root node ("(scale, 2)" means that the maximum magnification ratio is 2, which can be adjusted according to actual business needs)  = baseSize * (scale, 2) + 'px'
}
// InitializationsetRem()
// Reset rem when changing the window size = function () {
 ("I executed it")
 setRem()
}

Introduced

import "./utils/rem"

Configure network request axios

Install

npm i -s axios

Configure axios

Create utils folder in src and create under utils

import axios from "axios";
const service = ({
 baseURL,
 timeout: 5000 // request timeout
});
// Interceptor before initiating a request(
 config => {
 // If there is a token, carry a tokon const token = ("accessToken");
 if (token) {
   = token;
 }
 return config;
 },
 error => (error)
);
// Response Interceptor(
 response => {
 const res = ;
 
 if ( !== 200) {
  return (new Error( || "Error"));
 } else {
  return res;
 }
 },
 error => {
 return (error);
 }
);
export default service;

use

import request from "../utils/request";
request({url: "/profile ",method: "get"})
.then((res)=>{
 (res)
})

Configure the request proxy

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'
import path from 'path';
 
// /config/
export default defineConfig({
 plugins: [vue()],
 base:"./",//Package path resolve: {
 alias:{
  '@': (__dirname, './src')//Set an alias }
 },
 server: {
 port:4000,//Start port open: true,
 proxy: {
  // Option writing  '/api': 'http://123.56.85.24:5000'//Proxy URL },
 cors:true
 }
 
})

Above, one of the most basic mobile development configurations has been completed.

3. Vite's support for <script setup> and <style vars> is particularly friendly

Normal writing

&lt;script lang="ts"&gt;
import { defineComponent } from "vue";
import { useRouter } from "vue-router";
export default defineComponent({
 setup() {
 const router = useRouter();
 const goLogin = () =&gt; {
  ("/");
 };
 return { goLogin };
 },
});
&lt;/script&gt;
&lt;script setup&gt;How to write
&lt;script lang="ts" setup="props"&gt;
import { useRouter } from "vue-router";
const router = useRouter();
const goLogin = () =&gt; {
 ("/");
};
&lt;/script&gt;

Isn't it much simpler?

&lt;style vars&gt;How to use?
&lt;script lang="ts" setup="props"&gt;
const state = reactive({
 color: "#ccc",
});
&lt;/script&gt;
&lt;style &gt;
.text {
 color: v-bind("");
}
&lt;/style&gt;

It's that simple!

Code

Original address:/p/351888882

Online preview:http://123.56.85.24/vite/#/

Code address:/huoqingzhu/vue3-vite-ts-Vant

vitejs Chinese website:/

This is the article about the detailed explanation of the practical combat of vite2.0+vue3 mobile terminal project. For more relevant practical combat content of vite2.0+vue3 project, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!