1. 再次封裝 recordListModel
- 寫類型的兩種方法
- 聲明一個(gè) type
type RecordListModel = { data: }
- 強(qiáng)制斷言
data: [] as RecordItem[]
2. 給 window 加上屬性
// custom.d.ts
interface Window {
tagList: Tag[]
}
// 目的是防止自己手賤
3. 用 window 來封裝 api
-
tag 里面除了 id 的所有東西
window.updateTag = (id: string, object: Exclude<Tag, 'id'>){ }
-
類型一樣進(jìn)行簡寫
interface Window { tagList: Tag[], createTag: (name: string) => void, removeTag: (id:string) => boolean, // updateTag: (id:string, name: string) => 'success' | 'not found' | 'duplicated' updateTag: TagListModel['update'] }
4. 目前代碼存在的問題
- 全局變量太多
- 通過掛到 window.store = {}解決
- 嚴(yán)重依賴 window
5. 目前代碼存在的 bug
- 導(dǎo)致原因籍救,數(shù)據(jù)引用和對象引用么库,解決辦法數(shù)據(jù)和對象都放 computed,computed 的功能是原來的值變化就會(huì)更新外面的值
6. 使用 store 小技巧
- 在 main.ts 中寫
import store2 from '@/store/index2.ts'
Vue.prototype.$store2 = store2
// 就可以在任何一個(gè)實(shí)例中用this.$store2來訪問
- 解決類型下劃線的問題
https://cn.vuejs.org/v2/guide/typescript.html#%E5%A2%9E%E5%BC%BA%E7%B1%BB%E5%9E%8B%E4%BB%A5%E9%85%8D%E5%90%88%E6%8F%92%E4%BB%B6%E4%BD%BF%E7%94%A8