Template -- Vue3

Vue3

版本

  • Node 21.6.0
  • Npm 10.2.4

项目

创建

npm init vite 项目名称
  • vue
  • js
  • npm i
  • npm run dev

依赖

npm i element-plus          # UI
npm i axios                 # 网络
npm i vue-router@4          # 路由
npm i vuex@next             # 状态管理

npm i sass -D               # sass
npm i vite-plugin-html -D   # html

环境区分

  • package.json
"scripts": {
   
    "dev": "vite --mode test",
    "dev1": "vite --mode production",
    "build": "vite build --mode test",
    "build1": "vite build --mode production"
}
  • package.json 同级
  • .env.test
NODE_ENV = "test"
VITE_APP_TITLE = "测试"
VITE_APP_BASE_API = "test"
  • .env.production
NODE_ENV = "production"
VITE_APP_TITLE = "生产"
VITE_APP_BASE_API = "production"
  • vite.config.js
import {
    defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";
import {
    createHtmlPlugin } from "vite-plugin-html";
import {
    resolve } from "path";

export default defineConfig(({
     mode, command }) => {
   
  const env = loadEnv(mode, process.cwd(), ""); 
  return {
   
    plugins: [
      vue(),
      createHtmlPlugin({
   
        inject: {
   
          data: {
   
            title: env.VITE_APP_TITLE,
          },
        },
      }),
    ],
    resolve: {
   
      alias: {
   
        "@": resolve(__dirname, "src"), // 设置 `@` 指向 `src` 目录
      },
    },
    base: "./", // 设置打包路径
    server: {
   
      port: 4000, // 设置服务启动端口号
      open: true, // 设置服务启动时是否自动打开浏览器
      cors: true, // 允许跨域
      // 设置代理,根据我们项目实际情况配置
      // proxy: {
   
      //   '/api': {
   
      //     target: 'http://xxx.xxx.xxx.xxx:8000',
      //     changeOrigin: true,
      //     secure: false,
      //     rewrite: (path) => path.replace('/api/', '/')
      //   }
      // }
    },
  };
});
  • index.html
<title><%= title %></title>

axios

  • src/http/index.js
import axios from "axios";

let service = axios.create({
   
  // baseURL: "https://cnodejs.org/api/v1", //相同绝对路径
  baseURL:import.meta.env.VITE_APP_BASE_API,
  timeout: 100000, //超过这么多时间,则请求终止
  headers: {
   
    //请求头携带数据的格式
    "Content-Type": "application/json;charset=UTF-8",
    // "Content-Type": "application/x-www-form-urlencoded;charset=utf-8",
  },
});
// 添加请求拦截器(Interceptors)
service.interceptors.request.use(
  function (config) {
   
    // 发送请求之前做写什么
    let token = localStorage.getItem("token");
    // 如果有
    if (token) {
   
      // 放在请求头(token跟后端沟通,他需要什么该成什么就可以了)
      config.headers.authorization = token;
    }
    return config;
  },
  function (error) {
   
    // 请求错误的时候做些什么
    return Promise.reject(error);
  }
);
// 添加响应拦截器
service.interceptors.response.use(
  function (response) {
   
    // 对响应数据做点什么
    return response.data;
  },
  function (error) {
   
    // 对响应错误做点什么
    return Promise.reject(error);
  }
);

export default service;
  • src/http/api.js
import request from "./index";

export async function Ceshi(params) {
   
    return request({
   
      url: "/topics",
      method: "GET",
      params,
    });
  }
  export async function Ceshi2(data) {
   
    return request({
   
      url: "/Storage/GetStorageLack",
      method: "POST",
      data,
    });
  }
  • 使用
<script setup>
import {
   Ceshi} from '@/http/api.js'
var a = await Ceshi()
console.log(a);
</script>

store

  • src/store/index.js
import {
    createStore } from "vuex";

const defaultState = {
   
  count: 0,
};

// Create a new store instance.
export default createStore({
   
  state() {
   
    return defaultState;
  }
});
  • 使用
import {
   useStore} from 'vuex'
console.log(useStore().state.count);

router

  • src/router/index.js
import {
    createRouter, createWebHashHistory } from "vue-router";

const routes = [
  {
   
    path: "/",
    name: "Home",
    component: () => import("@/views/a.vue"), // 懒加载组件
  },
];

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

export default router;
  • 使用
// suspense 配合异步使用
<suspense>
  <router-view></router-view>
</suspense>

main.js

import {
    createApp } from 'vue'
import './style.css'
import App from './App.vue'
import store from './store/index'
import router from './router/index'
import ElementPlus from 'element-plus'
import 'element-plus/dist/index.css'

createApp(App).use(router).use(store).use(ElementPlus).mount('#app')

sass

.ddd{
  span{
    color: red;
  }
}

相关推荐

  1. Template -- Vue3

    2024-01-19 03:52:01       52 阅读
  2. Vue3-admin-template的表格合计计算

    2024-01-19 03:52:01       60 阅读
  3. Template -- Vue2

    2024-01-19 03:52:01       54 阅读
  4. vuetemplate原理

    2024-01-19 03:52:01       45 阅读
  5. VUE3与Uniapp 五 (v-if、v-show和template的使用)

    2024-01-19 03:52:01       30 阅读
  6. vue-template-compiler 的原理

    2024-01-19 03:52:01       49 阅读
  7. vue-template-loader 是如何工作的?

    2024-01-19 03:52:01       50 阅读
  8. Vue-admin-template关于TagView缓存问题

    2024-01-19 03:52:01       31 阅读
  9. vue中slot和template用法传值

    2024-01-19 03:52:01       61 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-19 03:52:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-19 03:52:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-19 03:52:01       82 阅读
  4. Python语言-面向对象

    2024-01-19 03:52:01       91 阅读

热门阅读

  1. Backtrader 文档学习-Indicators 开发

    2024-01-19 03:52:01       55 阅读
  2. 速盾网络:高防ip是什么

    2024-01-19 03:52:01       47 阅读
  3. 文件上传报错总结

    2024-01-19 03:52:01       49 阅读
  4. LC674. 最长连续递增序列

    2024-01-19 03:52:01       61 阅读
  5. Fastapi打包exe后无限启动导致死机的解决办法

    2024-01-19 03:52:01       74 阅读
  6. nlk学习笔记 新闻摘要自动提取

    2024-01-19 03:52:01       46 阅读
  7. Pandas实战100例 | 案例 49: 数值运算

    2024-01-19 03:52:01       49 阅读
  8. D - Disastrous Doubling

    2024-01-19 03:52:01       60 阅读