关于前端:用vue3vite重构小项目

9次阅读

共计 5162 个字符,预计需要花费 13 分钟才能阅读完成。

2.0.0

我的项目阐明

该我的项目为 web 单页面利用,外围性能为显示工夫。

技术栈应用 pnpm、Vite、Vue3、typescript、tailwind、github/api

性能需要

  • 显示工夫
  • 显示随机壁纸
  • 每日热词录入

非性能需要

  • 挪动端自适应
  • 查看日前工夫具体属性

页面比照

新我的项目预览
旧我的项目预览

旧页面

新页面

开发记录

Vscode Extension

  • Vue 3 Pack
  • Vue Extension Pack
  • Tailwind CSS Extension Pack

alias 设置

‘@’ 默认失效、其余门路须要同时配置 vite 和 typescript 才失效,配置完最好重启 ide

1. 配置 vite.config 文件

{
resolve: {
    alias: {'@': path.resolve(__dirname, './src'),
      '#': path.resolve(__dirname, './src/views'),
      '$': path.resolve(__dirname, './src/components'),
    },
  }
}

2. 配置 tsconfig.json 文件

{
 "compilerOptions": {
    "paths": {"/@/*": ["src/*"],
      "/#/*": ["src/views/*"],
      "/$/*": ["src/components/*"]
    }
  },
}

script setup

<script setup lang="ts">
  import {reactive, watchEffect, computed, ref} from 'vue';
  import gitPut from '$/gitPut.vue'; // 组件引入即可间接应用,不须要 componet 注册
  //    以下变量都能够在 template 中应用
  const menu = [...routes];     // 非动静绑定
  const menuShow = ref(false);  // 动静绑定,set/get 时须要应用 menuShow.value
  const state = reactive({  // 动静绑定。相似 store/state,保护一组参数
       imgUrl: '',
       now: Date.now(),
       twDate: computed(() => new TwDate(state.now)),
       /* 应用 computed 代替 filter*/
       weekMix: computed(() => state.twDate.getWeekEn() + '|' + state.twDate.getWeekZh()),
       seasonMix: computed(() => state.twDate.getSeasonEn() + '|' + state.twDate.getSeasonZh()),
  })
  //  watchEffect,  区别于 watch 只监听一个变量
  watchEffect(() => {
    //  能够定义所有变量监听,此处只监听了 state.now
    state.localStr = new TwDate(state.now).toLocaleString();});
<script/>

Vue3 Hooks

最大区别就是勾销了 destroyed 改为 unMounted

创立hook.tslog 工具

import {
  onBeforeMount,
  onMounted,
  // onBeforeUpdate,
  // onUpdated,
  onBeforeUnmount,
  onUnmounted,
  // onDeactivated,
  // onActivated,
  // onRenderTracked,
  // onRenderTriggered
} from 'vue';

const HOOK_LIST: Map<string, any> = new Map([['BeforeMount', onBeforeMount],
  ['Mounted', onMounted],
  // ['BeforeUpdate', onBeforeUpdate],
  // ['Updated', onUpdated],
  ['BeforeUnmount', onBeforeUnmount],
  ['Unmounted', onUnmounted],
  // ['Deactivated', onDeactivated],
  // ['Activated', onActivated]
  // ['RenderTracked', onRenderTracked],
  // ['RenderTriggered', onRenderTriggered]
]);

/**
 * @description    Hook 日志与回调
 * @param cb
 */
export default (
  cb:
    | {afterBeforeMount: () => void;
        afterMounted: () => void;
        afterUnmounted: () => void;
        afterBeforeUnmount: () => void;}
    | any
) => {HOOK_LIST.forEach((hook, key) => {hook(() => {console.log(`#Hook////${key}`);
      let afterCb = cb?.[`after${key}`];
      afterCb && afterCb();});
  });
};

应用 hook.ts

<script setup lang="ts">
  import hook from '@/common/hook';
  import {reactive} from 'vue';
  const state = reactive({now: Date.now()
  })
  //  timeInterval
  let timer = setInterval(() => {state.now = Date.now();
  }, 800);
  //  test hook
  hook({afterUnmounted: () => {clearInterval(timer);
      timer = 0;
    },
  });

Class Date Extend

import dateEnum from './../enums/dateEnum';
export default class TwDate extends Date {
  //    返回当天是当年的第几天
  getDayInYear = () => {
    return ((Date.now() - new TwDate(new TwDate().getFullYear(), 0, 1, 0, 0, 0).getTime()) /
      (24 * 3600 * 1000)
    );
  };
  //    返回以后时刻是当天的第几毫秒
  getMillSecInDay = () => {const y = new TwDate().getFullYear();
    const m = new TwDate().getMonth();
    const d = new TwDate().getDate();
    return Date.now() - new TwDate(y, m, d, 0, 0, 0).getTime();};
  //    返回当天 / 当年占比
  getRatioYear = () => {return this.getDayInYear() / 365;
  };
  //    返回当天 / 当月占比
  getRatioMonth = () => {const y = new TwDate().getFullYear();
    const m = new TwDate().getMonth();
    return new TwDate().getDate() / new TwDate(y, m + 1, 0).getDate();};
  //    返回当天 / 当周占比
  getRatioWeek = () => {return (new TwDate().getDay() || 7) / 7;
  };
  //    返回以后时刻 / 当天占比
  getRatioDay = () => {return this.getMillSecInDay() / (24 * 3600 * 1000);
  };
  //    返回文本:周 -En
  getWeekEn = () => {return dateEnum.WEEK_EN[new TwDate().getDay()];
  };
  //    返回文本:周 -Zh
  getWeekZh = () => {return dateEnum.WEEK_ZH[new TwDate().getDay()];
  };
  //    返回文本:节令 -En
  getSeasonEn = () => {return dateEnum.SEASON_EN[Math.floor(new TwDate().getMonth() / 3)];
  };
  //    返回文本:节令 -Zh
  getSeasonZh = () => {return dateEnum.SEASON_ZH[Math.floor(new TwDate().getMonth() / 3)];
  };
}

随机图片地址

reference

unsplash: 收费的高清摄影素材网站

  const api = 'https://unsplash.it';
  const randomId = Math.random() * 10;
  const width = 1920;
  const height = 1080;
  let url = `${api}/${width}/${height}?random=${parseInt(randomId.toString(), 10)}`;

P.S.
也可用利用 api 获取 Bing 壁纸,但因为要解决跨域放弃了。如果是独立部署的话能够思考。

// const bingUrl = 'https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN';

tailwind 应用

因为目前应用的是 quasar 框架,其中也有一些原子类的款式,所以思维上还是能了解。

但不习惯 api 的话起手比拟吃力,有肯定门槛,对与团队推广还是有肯定难度。

个人感觉作为我的项目落地的话将来可能会有应用上的瓶颈,作为集体开发的话我目前还是比拟中意的,有种中庸之美。

github/api

每日热词需要由 github/issue 实现,即输出热词回车后将参数由 github/api 传递给 github。
写入操作须要 token 验证,能够在 github/ 开发者设置里创立。
因为 token 不可显性显示在代码中,所以对 token 采纳非对称加密,每次申请要输出密钥,解码后获取 token。

P.S. 如果 token 间接裸露在代码中,该 token 会主动删除。

1. 创立 token,输出 note 即可,checkbox 勾选 repo 即可


2. 在 github/repo/issue 里创立新 label,次要用于辨别。

3. 配置用户、我的项目 package.json

{
  "config": {
    "owner": "Mulander-J",
    "repo": "timeWaster",
    "label": "heart"
  }
}

4. 应用 api

import axios from 'axios';
import pkg from './../../package.json';

const {owner, repo, label} = pkg.config;
const url = `https://api.github.com/repos/${owner}/${repo}`;

export default class Api {static getIssueList = (since?: string): Promise<any> => {
    since = since ? '&since=' + since : '';
    return axios.get(`${url}/issues?filter=created&labels=${label + since}`);
  };
  static addIssue = (body: string, token: string): any => {return axios.post(`${url}/issues?access_token=${token}`, {labels: [label],
      title: 'TW/' + new Date().toLocaleDateString(),
      body,
    });
  };
}

打包及部署

打包前要依据仓库地址批改根目录属性

// [guide](https://vitejs.dev/config/)
export default defineConfig({base: '/timeWaster'});

应用 github-page 部署,这里用到了 gh-pages 插件,两行命令间接部署。

{
  "scripts": {
    "dev": "vite",
    "build": "vite build",
    "serve": "vite preview",
    "predeploy": "vite build",
    "deploy": "gh-pages -d dist"
  },
  "devDependencies": {"gh-pages": "^3.1.0"},
}

结语

本意是紧跟潮流学点新货色,利用空闲工夫查资料等了一周,理论开发用了两天左右。

对于 vue3 没波及具体业务还不分明,对于 vite 的速度真的很喜爱。

正文完
 0