vue2 + ts + vuex 构建脚手架
创建项目
vue create vue2-ts-demo
暂时选择默认的!
npm i 安装依赖
安装ts
vue add @vue/typescript ( 一路 yes )
安装vuex
npm i vuex || vue add vuex
- 关闭eslint 变量未使用的时候 报异常 package.json
"eslintConfig": {
"root": true,
"rules": {
"no-unused-vars":0
}
},
model 数据的类型 限制
model / ItemData.ts 文章的每一项数据的类型
import Category from './CateEnum'
class ItemData {
id!: number
categoryId!: Category
title!: string
content!: string
createTime!: string
constructor(
id: number = -1,
categoryId: Category = -1,
title: string = '',
content: string = ''
) {
this.id = id
this.categoryId = categoryId
this.title = title
this.content = content
this.createTime = this.toSelfDateStr(Date.now())
}
toSelfDateStr(
timeSpan: number,
sign: string = '-',
timeSign: string = ':'
): string {
const date = new Date(timeSpan)
let resDate = ''
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hours = date.getHours()
const hours2 = hours < 10 ? '0' + hours : hours
const minutes = date.getMinutes()
const minutes2 = minutes < 10 ? '0' + minutes : minutes
const seconds = date.getSeconds()
const seconds2 = seconds < 10 ? '0' + seconds : seconds
resDate =
year +
sign +
month +
sign +
day +
' ' +
hours2 +
timeSign +
minutes2 +
timeSign +
seconds2
return resDate
}
}
export default ItemData
model / CateEnum.ts 文章的类型 枚举
enum Category {
Work = 0,
Life = 1,
Stydy = 2
}
export default Category;
main.ts 测试效果
import Vue from 'vue'
import App from './App.vue'
import store from './store'
Vue.config.productionTip = false
import ItemData from './model/ItemData'
import Category from './model/CateEnum'
new Vue({
store,
render: h => h(App)
}).$mount('#app')
const item1 = new ItemData(1, Category.Stydy, '我是title', '学习vue2+ts')
console.log('item1', item1)
测试 DateHelper 本地存储 与 ActionsHelper 笔记数组数据的转化
store / DateHelper.ts 存储类
class DateHelper{
dataKey:string;
privateKey:string;
constructor(dataKey:string,privateKey:string){
this.dataKey =dataKey;
this.privateKey = privateKey;
}
readData():any{
const strData:string |null = localStorage.getItem(this.dataKey);
let arrData:any = [];
if ( strData != null ) {
arrData = JSON.parse(strData)
}
return arrData;
}
savaData(arrData:Array<Object>):void{
const str:string = JSON.stringify(arrData);
localStorage.setItem(this.dataKey,str)
}
addData(conStr:string):number{
const arr:any = this.readData();
interface locObject {
[key: string]: any
}
const obj:locObject = {
content:conStr,
}
const newId:any = arr.length > 0 ? arr[arr.length - 1][this.privateKey] + 1: 1;
obj[this.privateKey] = newId;
arr.push(obj)
this.savaData(arr)
return newId;
}
removeDataById(id:string | number):boolean{
const arr = this.readData()
interface locObject {
[key: string]: any
}
const index = arr.findIndex( (ele:locObject) => {
return ele[this.privateKey] == id;
})
if ( index > -1) {
arr.splice(index,1);
this.savaData(arr)
return true;
}
return false;
}
}
export default DateHelper
store / ActionsHelper.ts 业务处理类 ( 新增笔记、修改笔记、删除笔记)
import DateHelper from './DateHelper'
import ItemData from '@/model/ItemData'
class ActionsHelper {
dataHelper: DateHelper = new DateHelper('menoData', 'id')
menoList!: Array<ItemData>
constructor() {
this.menoList = this.readonly()
}
readonly(): Array<ItemData> {
const arrObj = this.dataHelper.readData()
const arrItem = arrObj.map((ele: any) => {
const item: ItemData = new ItemData()
item.id = ele.id
item.categoryId = ele.categoryId
item.title = ele.title
item.content = ele.content
item.createTime = ele.createTime
return item
})
return arrItem
}
add(item: ItemData): number {
item.id = this.dataHelper.addData(item.content)
this.menoList.push(item)
this.dataHelper.savaData(this.menoList)
return -1
}
edit(item: ItemData): void {
const eleItem: ItemData | undefined = this.menoList.find((ele: any) => {
return ele.id === item.id
})
if (eleItem) {
eleItem.categoryId = item.categoryId
eleItem.title = item.title
eleItem.content = item.content
this.dataHelper.savaData(this.menoList)
}
}
remove(id: number): void {
const index: number = this.menoList.findIndex((ele: any) => {
return ele.id === id
})
if (index > -1) {
this.menoList.splice(index, 1)
this.dataHelper.savaData(this.menoList)
}
}
}
export default ActionsHelper
测试 使用 上面两个类 main.ts ( 新增笔记、修改笔记、删除笔记)
import Vue from 'vue'
import App from './App.vue'
import store from './store'
Vue.config.productionTip = false
import ItemData from './model/ItemData'
import Category from './model/CateEnum'
import ActionsHelper from './store/ActionsHelper'
new Vue({
store,
render: h => h(App)
}).$mount('#app')
const ah = new ActionsHelper()
console.log('ah', ah.menoList)