今天把整個項目剩餘都講完,後面將會學習一下react,而後用react寫個後臺,而後淺談一下使用心得,以及學習技巧 當前項目demo預覽css
首先咱們先在圖形話界面去下載當前須要到依賴(版本號以及axios都須要一致不知道能夠去咱們都第一章看看 vue
state
文件(不瞭解的能夠先去看下vuex
官網)這裏咱們把全局的變量放裏面(說一下不是爲了用vuex才用vuex的,有些項目徹底能夠不用固然能夠不用)react
import { getToken, setToken, removeToken } from '@/views/utils/auth'
const state: any = {
token: getToken(),
imgUrl: 'https://api.uat.iyuedian.com/iyd-imall-manage/imall/v1/upload'
}
export default state
複製代碼
mutations
文件這個文件就是提交改變當前的state
裏面的值的不懂interface
的能夠看下慕課網ios
export default mutations{
## 老方法
SET_TOKEN(state: any, data: any) {
state.token = data
},
}
import { MutationTree } from 'vuex'
## 新方法 MutationTree<any> 相信應該有些人不理就是一個接口
const mutations: MutationTree<any> = {
'SET_TOKEN'(
state: any,
data: any
): void {
state.token = data
}
}
複製代碼
vuex 裏面的源碼能夠看一下 git
actions
文件這個文件能夠執行mutations
文件裏面的方法公共的方法均可以放到這個裏面async
定義一個一步函數老是實際返回值老是一個 Promise 對象github
import { sysUserLogin } from '@/views/interface/login';
import { getToken, setToken, removeToken } from '@/views/utils/auth';
import { ActionTree } from 'vuex';
import { Message } from 'element-ui';
const actions: ActionTree<any, any> = {
/**
* 登錄
* @param param0
* @param userInfo 登錄信息
*/
async Login({state, commit} , userInfo: any) {
return new Promise((resolve, reject) => {
sysUserLogin(userInfo).then((response: any) => {
setToken(response.data.systoken);
console.log(response.data.systoken);
commit('SET_TOKEN', response.data.systoken); ## 這邊調用了上面的方法
resolve(response);
}).catch((error) => {
reject(error);
});
});
},
/**
* 深拷貝
* @param param0
* @param params
*/
async deep({state, commit} , params: any) {
let obj = {};
obj = JSON.parse(JSON.stringify(params));
return obj;
},
};
export default actions;
複製代碼
getters
文件getters
能夠定義是 store
的計算屬性能夠將 state
進行過濾而後return
出來web
## 老方法
export default {
token: (state:any) => state.token,
}
## 新方法
import {GetterTree} from 'vuex'
const mutations: GetterTree<any,any> = {
'token'(
state: any,
): any {
return state.token
}
}
export default mutations
複製代碼
vuex
使用方式vuex-class
## 獲取state的值 (先要定義一遍纔可使用)
@State imgUrl
@Action('Login') Login;
@Getter('Login') getterFoo;
@Mutation('Login') mutationFoo;
// 簡寫方式
@State foo
@Getter bar
@Action baz
@Mutation qux
//最早開始執行
created() {
## 後面的是原來的使用方式
console.log(this.imgUrl); // -> this.store.state.imgUrl
console.log(this.getterFoo(2)) // -> this.store.getters.Login
this.Login({ value: true }).then() // -> this.store.dispatch('Login', { value: true })
this.mutationFoo({ value: true }) // -> this.store.commit('Login', { value: true })
}
複製代碼
這裏咱們用的是element的圖片上傳 若有不懂的能夠看下element
的組件vuex
咱們想要更佳簡便的使用方式element-ui
// 單張圖片上傳(組件)
<template>
<div>
<el-upload
class="avatar-uploader"
:action="$store.state.imgUrl" ## 咱們圖片上傳的地址
:show-file-list="false"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload">
<img v-if="BeforeUploadImg" :src="BeforeUploadImg" class="avatar">
<i v-else class="el-icon-plus avatar-uploader-icon"></i>
</el-upload>
</div>
</template>
<script lang="ts">
import { Component, Vue, Model, Watch, Prop } from 'vue-property-decorator';
@Component
export default class BeforeUpload extends Vue {
## 初始值
@Prop(String)BeforeUploadImg:string;
## 生命週期最早開始執行 void 表示沒有返回值
created():void {
}
public handleAvatarSuccess(res:any, file:any) {
## 本地圖片預覽; update: 這樣配合父組件能夠實現父子組件的雙向綁定
this.$emit('update:BeforeUploadImg',res.data[0].newFileName);
}
public beforeAvatarUpload(file:any) {
// const isJPG = file.type === 'image/jpeg';
const isLt2M = file.size / 1024 / 1024 < 2;
// if (!isJPG) {
// this.$message.error('上傳頭像圖片只能是 JPG 格式!');
// }
if (!isLt2M) {
this.$message.error('上傳頭像圖片大小不能超過 2MB!');
}
// return isJPG && isLt2M;
return isLt2M;
}
}
</script>
# 使用方式
# html .sync 配合update能夠實現雙向綁定
<BeforeUpload :BeforeUploadImg.sync="BeforeUploadImg"></BeforeUpload>
# script
import BeforeUpload from '@/components/beforeUpload/beforeUpload.vue';
import { Component, Vue, Model, Watch, Prop } from 'vue-property-decorator';
@Component({
components: {
BeforeUpload,
}
})
export default class Content extends Vue {
## 默認圖片
public BeforeUploadImg: string = '';
}
複製代碼
咱們想要更佳簡便的使用方式
// 批量圖片上傳(組件)
<template>
<div>
<el-upload
class="upload-demo"
:action="$store.state.imgUrl"
:on-preview="handlePreview"
:on-success="handleOnSuccess"
:on-remove="handleRemove"
:file-list="UploadListsImg"
list-type="picture">
<el-button size="small" type="primary">點擊上傳</el-button>
<div slot="tip" class="el-upload__tip">只能上傳jpg/png文件,且不超過1024kb</div>
</el-upload>
</div>
</template>
<script lang="ts">
import { Component, Vue, Model, Watch, Prop } from 'vue-property-decorator';
@Component
export default class UploadListImg extends Vue {
// 初始值
@Prop(null)UploadListsImg:object[];
//最早開始執行
created():void {
// tinyMce.init({})
}
/**
* 刪除圖片
* @param file 刪除的圖片
* @param fileList 剩下的圖片
*/
public handleRemove(file:any, fileList:any) {
console.log(file, fileList);
this.$emit('update:UploadListsImg',fileList)
this.$emit('removeListImg',file)
}
public handlePreview(file:any) {
console.log(file);
}
/**
* 添加圖片
* @param response 成功的返回值
* @param file 當前的這個圖片
* @param fileList 當前全部的圖片
*/
public handleOnSuccess(response:any, file:any, fileList:any){
file.url = response.data[0].newFileName;
file.name = response.data[0].originalFilename;
this.$emit('update:UploadListsImg',fileList)
}
}
</script>
## html UploadListsImg爲當前剩下的圖片的list removeListImg爲刪除掉的list 使用方式
<UploadListImg :UploadListsImg.sync="UploadListsImg" @removeListImg="removeListImg" style="width: 400px"></UploadListImg>
## script
import UploadListImg from '@/components/uploadListImg/uploadListImg.vue';
import { Component, Vue, Model, Watch, Prop } from 'vue-property-decorator';
@Component({
components: {
UploadListImg,
}
})
export default class Content extends Vue {
public UploadListsImg: object[] = [
{
name: 'food.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
},
{
name: 'food2.jpeg',
url: 'https://fuss10.elemecdn.com/3/63/4e7f3a15429bfda99bce42a18cdd1jpeg.jpeg?imageMogr2/thumbnail/360x360/format/webp/quality/100'
}
];
public removeListImg: object[] = []
}
複製代碼
分頁咱們想要更加簡便的使用方式
// 分頁組件
<style scoped lang="scss">
.t-pagination{
width: 100%;
overflow: hidden;
}
.t-pagination-content{
float: right;
margin: 20px;
}
</style>
<template>
<div class="t-pagination">
<div class="t-pagination-content">
<el-pagination
@size-change="handleSizeChange"
@current-change="handleCurrentChange"
:current-page="currentPage4"
:page-sizes="[10, 20, 30, 40, 100]"
:page-size="pageSize"
layout="total, sizes, prev, pager, next, jumper"
:total="Paginationtotal">
</el-pagination>
</div>
</div>
</template>
<script lang="ts">
import Utils from '@/utils/utils'
import { Component, Vue, Model, Prop, Watch } from 'vue-property-decorator';
@Component
export default class Reset extends Vue {
// props聲明
@Prop() private Paginationtotal!: number;
private pageSize:number = 20;
private currentPage4:number = 1;
//最早開始執行
created():void {
if (this.$route.query.pageNum) {
this.currentPage4 = Number(Utils.deep(this.$route.query.pageNum));
this.pageSize = Number(Utils.deep(this.$route.query.pageSize));
}else {
this.currentPage4 = 1;
this.pageSize = 20;
}
}
//監聽路由變化
@Watch('$route')
onRouteChanged(route: any, oldRoute: any) :void {
if (route.query.pageNum) {
this.currentPage4 = Number(Utils.deep(route.query.pageNum))
this.pageSize = Number(Utils.deep(route.query.pageSize));
}else {
this.currentPage4 = 1;
this.pageSize = 20;
}
this.$forceUpdate()//強刷當前
}
private handleSizeChange(val:any) {
let data:any = Utils.deep(this.$route.query);
[data.pageNum,data.pageSize] = [1,val]
this.start(data)
console.log(`每頁 ${val} re條`);
}
private handleCurrentChange(val:any) {
let data:any = Utils.deep(this.$route.query);
data.pageNum = val
data.pageSize = this.pageSize
this.start(data)
console.log(`當前頁: ${val}re`);
}
private start(ret:any) {
this.$store.dispatch('paramsUrl',ret).then((res:any) => {
this.$router.push(`${this.$route.path}${res}`)
})
}
}
</script>
# html 使用方式
<Pagination :Paginationtotal="Paginationtotal"></Pagination>
# script
import Pagination from '@/components/pagination/pagination.vue';
import { Component, Vue, Model, Watch, Provide } from 'vue-property-decorator';
@Component({
components: {
Pagination
}
})
export default class Content extends Vue {
Paginationtotal:number = 0;
}
複製代碼
咱們重置只須要把當前的分頁重置成第一頁 20跳數據便可
// 重置按鈕
<template>
<el-button size="mini" @click="reset(searchReserved)">重置</el-button>
</template>
<script lang="ts">
import { Component, Vue, Model, Prop } from 'vue-property-decorator';
@Component({
})
export default class Reset extends Vue {
// props聲明 paramsUrl 爲定義的當前的vuex裏面的方法
@Prop() private searchReserved!:object
public reset(search:any) {
[search.pageNum,search.pageSize] = [1,20]
this.$store.dispatch('paramsUrl',search).then((res:any) => {
this.$router.push(`${this.$route.path}${res}`)
})
}
}
</script>
## html 使用方式
<Reset :searchReserved="searchReserved"></Reset>
## script
import Reset from '@/components/reset/reset.vue';
@Component({
components: {
Reset
}
})
export default class Content extends Vue {
searchReserved = {}
}
複製代碼
關於富文本編譯器,我想你們應該不陌生了,我推薦一筐tinyMce
來使用到咱們當前到項目裏面來富文本編譯器ts
都支持還不是多,我找來不少來嘗試,最後決定用tinyMce
下載一下這兩個,中間踩了不少坑,給咱們伸手黨線上開箱即食的代碼把
## 富文本編譯器 ## EditorContent 默認值 onChangeHandler 改變的事件 editorInit 初始配置
## 基本上圖片上傳都須要form表單方式上傳 FormData
<template>
<div>
<Editor :initial-value="EditorContent" @onChange="onChangeHandler" id="tinyMce" :init="editorInit"></Editor>
</div>
</template>
<script lang="ts">
import 'tinymce/themes/modern/theme';
import Editor from '@tinymce/tinymce-vue';
import { Component, Vue, Model, Watch, Prop } from 'vue-property-decorator';
@Component({
components: {
Editor
}
})
export default class Content extends Vue {
// 父組件傳進來的html
@Prop(String) EditorContent: any;
// 初始值
public initialVal:any = '';
public editorInit:any = {
language_url: './static/zh_CN.js',
language: 'zh_CN',
selector: 'textarea',
skin_url: './static/skins/lightgray',
height: 300,
// width:600,
images_upload_url: 'https://api.uat.iyuedian.com/iyd-imall-manage/imall/v1/upload',
plugins: 'link lists image code table colorpicker textcolor wordcount contextmenu',
toolbar: 'bold italic underline strikethrough | fontsizeselect | forecolor backcolor | alignleft aligncenter alignright alignjustify | bullist numlist | outdent indent blockquote | undo redo | link unlink image code | removeformat',
images_upload_handler: function (blobInfo:any, success:any, failure:any) {
let xhr:any, formData:any;
xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.open('POST', this.$store.state.imgUrl);
xhr.onload = function() {
if(xhr.status<200||xhr.status>=300){
failure(xhr.status);
return;
}
let json = JSON.parse(xhr.responseText);
if(json.code==0){
success(json.data[0].newFileName);
} else {
failure('HTTP Error: ' + json.msg);
}
};
formData = new FormData();
formData.append('file', blobInfo.blob(), blobInfo.filename());
xhr.send(formData);
},
// images_upload_url: 'https://api.iyuedian.com/iyd-imall-manage/imall/v1/upload'
}
//最早開始執行
created():void {
// tinyMce.init({})
}
//監聽路由變化
@Watch('$route')
onRouteChanged(route: any, oldRoute: any) :void {
this.$forceUpdate()
}
/**
* 富文本內容變化時事件
*/
public onChangeHandler(res:any,index:any,tag:any){
//width height小程序不支持這個把這個替換了
let html = res.level.content.replace("width","xxx")
html = html.replace("height","yyy")
// console.log(html)
this.$emit('update:EditorContent',html)
}
}
</script>
## html 使用方式
<Editor :EditorContent.sync="EditorContent"></Editor>
## script
import Editor from '@/components/tinyMceEditor/tinyMceEditor.vue';
@Component({
components: {
Editor
}
})
export default class Content extends Vue {
// 默認圖文詳情
public EditorContent: string = '';
}
複製代碼
使用方式
基本的插件都講了一下有問題能夠添加上面的qq羣