vite初始化项目

  1. yarn create vite <project-name> --template vue
  2. cd <project-name>
  3. yarn
  4. yarn dev

安装依赖

  1. npm i vue-router@4 vuex axios -S
  2. npm i @types/node sass -D

配置vite.config.js

  1. ...
  2. export default defineConfig({
  3. ...
  4. server: {
  5. host: "127.0.0.1" /* 设置为0.0.0.0则所有的地址均能访问 */,
  6. // 配置代理服务器,解决跨域
  7. proxy: {
  8. "/api": {
  9. target: "http://localhost:3002",
  10. changeOrigin: true,
  11. },
  12. },
  13. },
  14. resolve: {
  15. // 配置别名
  16. alias: {
  17. "@": "/src",
  18. "@css": "/src/assets/css",
  19. "@js": "/src/assets/js",
  20. "@api": "/src/api",
  21. "@view": "/src/views",
  22. },
  23. },
  24. });

Vue Router创建路由

  1. import { createRouter, createWebHashHistory } from "vue-router";
  2. const router = createRouter({
  3. history: createWebHashHistory(),
  4. routes: [
  5. {
  6. path: "/my",
  7. component: () => import("@view/my.vue"),
  8. },
  9. ],
  10. });
  11. export default router;
  1. import router from "./router";
  2. ...
  3. createApp(App).use(router).mount("#app");
  1. <template>
  2. <router-view></router-view>
  3. </template>

配置Vuex

封装axios

移动端适配