TypeScript(4)——Vue全家桶+ TypeScript + Vuex + LocalStorage 完成的本地便签案例

前言

之前已经学过TS学习笔记3——如何在vue项目中使用TypeScript语法
这篇完成VueCil4 + TypeScript + Vuex + LocalStorage 完成的本地便签案例

需求:本地便签的增删改查,便签分类查看;

这个案例主要使用的是Class类,着重于面向对象开发而非面向过程,所以也复用了TS学习笔记2中的便签案例部分代码,大家看我的博客和我一起学习ts的时候,还是建议从TS学习笔记1开始,每一篇都看一下;

正文

项目源码

开始

样式----使用bootstrap框架

我用的最简单的方法,使用cdn在index.html直接引入

  <link href="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/css/bootstrap.min.css" rel="stylesheet">
  <script src="https://cdn.jsdelivr.net/npm/jquery@1.12.4/dist/jquery.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.3.7/dist/js/bootstrap.min.js"></script>

分析需求

需求:本地便签的增删改查,便签分类;

  1. 进页面时候,获取localStorage中存储的全部便签,把数据存到vuex中方便全局使用;
  2. 便签根据类型不同可以的分类查看;
  3. 新增便签,页面新增及更改本地存储;
  4. 删除便签,页面删除及本地存储;
  5. 编辑便签,页面删除及本地存储;

页面分析——/scr/components

  • 头部导航文件–MenuBar.vue
  • 便签主体—ItemList.vue
  • 编辑弹框–StickyEditor.vue
  • 单个便签框–StickyItem.vue
  • 整体的页面–/src/page/stickyNotePage.vue

数据分析——/scr/model

  • 生成单个便签所需的类–ItemData.ts
  • 枚举类别数据文件–CateEnum.ts

过程分析——/scr/store

  • 操作LocalStorage的单个类–DataHelper.ts
  • 操作页面增删改查的类–ActionHelper.ts

项目分析

  • 路由:/scr/router/index.ts
  • VUEX:/scr/store/index.ts
  • 入口:/scr/App.vue
  • 全局入口ts:/scr/main.ts

操作代码(激动的搓搓手)

  • 本身博客是准备一个一个页面去写的,但是代码好多啊。所以直接附上github地址:可以下载参考,源码中包含学习笔记和之前的代码合集及vue/cil3的配置与插件的使用;
  • 这个案例是有视频链接的,代码很简单,主要是思路,如果代码看不懂的可以去看着视频练习~

结语

通过几篇ts学习笔记的学习,在vue和html中使用TS应该已经很熟练了,还是那句话:我认为的TypeScript其实就是给JavaScript加了一些规范,ts是只js的超集,学好ts的根本还是熟练运用js!

附:TS学习笔记合集:

over~有其他问题留言
如果本文对你有帮助的话,请不要忘记给我点赞打call哦~o( ̄▽ ̄)do

实现 token 的方法,可以参考下面的代码实现: 在 `src/store` 目录下新建 `auth` 目录,用于存放认证相关的代码。 ```typescript // src/store/auth/state.ts export interface AuthState { token: string | null; } export const state: AuthState = { token: localStorage.getItem('token') || null, }; ``` ```typescript // src/store/auth/mutations.ts import { AuthState } from './state'; export const mutations = { setToken(state: AuthState, token: string) { state.token = token; localStorage.setItem('token', token); }, clearToken(state: AuthState) { state.token = null; localStorage.removeItem('token'); }, }; ``` ```typescript // src/store/auth/actions.ts import { ActionContext } from 'vuex'; import { AuthState } from './state'; import { RootState } from '../root-state'; export const actions = { async login(context: ActionContext<AuthState, RootState>, payload: { username: string; password: string }) { // 发送登录请求 const response = await fetch('/api/login', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify(payload), }); if (response.ok) { const { token } = await response.json(); context.commit('setToken', token); } else { throw new Error('登录失败'); } }, logout(context: ActionContext<AuthState, RootState>) { context.commit('clearToken'); }, }; ``` ```typescript // src/store/auth/index.ts import { Module } from 'vuex'; import { AuthState, state } from './state'; import { mutations } from './mutations'; import { actions } from './actions'; import { RootState } from '../root-state'; export const auth: Module<AuthState, RootState> = { namespaced: true, state, mutations, actions, }; ``` 然后,在 `src/store` 目录下新建 `index.ts` 文件,用于创建 store 实例。 ```typescript // src/store/index.ts import { createStore } from 'vuex'; import { auth } from './auth'; import { RootState } from './root-state'; export const store = createStore<RootState>({ modules: { auth, }, }); ``` 最后,在 `main.ts` 中引入 store 实例,并在根组件中注入。 ```typescript // main.ts import { createApp } from 'vue'; import App from './App.vue'; import { store } from './store'; const app = createApp(App); app.use(store); app.mount('#app'); ``` 这样,就可以在组件中通过 `this.$store.state.auth.token` 访问 token 了。在登录成功后,通过 `this.$store.dispatch('auth/login', { username, password })` 更新 token;在注销时,通过 `this.$store.dispatch('auth/logout')` 清除 token。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值