Skip to content

Commit

Permalink
Merge pull request #48 from School-of-Website-Engineering/develop
Browse files Browse the repository at this point in the history
  • Loading branch information
mason369 authored Jan 20, 2023
2 parents d42a14a + 1e01b36 commit 35b78ee
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 4 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"version": "1.1.5",
"private": true,
"scripts": {
"serve": "vue-cli-service serve",
"serve": "vue-cli-service serve --mode dev",
"servepro": "vue-cli-service serve --mode prod",
"build": "vue-cli-service build",
"lint": "vue-cli-service lint"
},
Expand Down
62 changes: 62 additions & 0 deletions src/components/Toast.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<template>
<!-- 封装提示框组件 -->
<!--
看type的值:
success iconfont icon-toast_chenggong
warning iconfont icon-toast-jinggao
danger iconfont icon-toast-shibai_huaban
-->
<div class="toast">
<i
:class="
type == 'success'
? 'iconfont icon-toast_chenggong'
: type == 'warning'
? 'iconfont icon-toast-jinggao'
: 'iconfont icon-toast-shibai_huaban'
"
></i>
<span>{{ msg }}</span>
</div>
</template>

<script>
import { mapState } from "vuex";
export default {
data() {
return {};
},
computed: {
...mapState({
msg : (state) => state.showToast.msg,
type: (state) => state.showToast.type
})
}
};
</script>

<style lang="scss" scoped>
.toast {
background-color: #fff;
position: fixed;
left: 50%;
top: 0;
transform: translateX(-50%);
padding: 10px 20px;
border-radius: 6px;
box-shadow: 0 0 10px #fff;
i {
margin-right: 8px;
}
.icon-toast_chenggong {
color: green;
}
.icon-toast-jinggao {
color: orange;
}
.icon-toast-shibai_huaban {
color: red;
}
}
</style>
5 changes: 3 additions & 2 deletions src/router/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Vue from "vue";
import VueRouter from "vue-router";
import routes from "./routes";
import store from "@/store";

Vue.use(VueRouter);

Expand Down Expand Up @@ -38,8 +39,7 @@ VueRouter.prototype.replace = function(location, resolve, reject) {
}
};


export default new VueRouter({
const router = new VueRouter({
routes,
scrollBehavior() {
return { y: 0 };
Expand All @@ -48,3 +48,4 @@ export default new VueRouter({
mode: "hash"
});

export default router;
3 changes: 2 additions & 1 deletion src/store/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import Vuex from "vuex";
import isShowLoginModal from "@/store/isShowLoginModal";
import loginStatus from "@/store/loginStatus";
import userInfo from "@/store/userInfo";
import showToast from "@/store/showToast";

Vue.use(Vuex);

export default new Vuex.Store({modules: { isShowLoginModal, loginStatus, userInfo }});
export default new Vuex.Store({modules: { isShowLoginModal, loginStatus, userInfo, showToast }});
52 changes: 52 additions & 0 deletions src/store/showToast/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
export default {
namespaced: true,
state : {
// 显示与否 true 仅仅之决定了一个显示属性
// 提示文本 "登录成功"
// 提示类型 "sucess" "warning" "danger"
// 用来表示提示框的显示true或隐藏false
isShowToast: false,
msg : "",
type : ""
},
mutations: {
// 修改isShowToast的值
chanIsShowToast(state, payload) {
state.isShowToast = payload.isShow;
state.msg = payload.msg;
state.type = payload.type;
}
},
actions: {
asyncChanIsShowToast({ commit }, payload) {
// let {commit} = context
// 先展示,就是调用原先写的上面的这个方法chanIsShowToast
commit("chanIsShowToast", {
isShow: true,
msg : payload.msg,
type : payload.type
});

// 关闭的代码
setTimeout(() => {
commit("chanIsShowToast", {
isShow: false,
msg : payload.msg,
type : payload.type
});
}, 1500);
}
}
};

// 封装Toast组件总结:
// 样式结构
// 显示隐藏变量存在Vuex中
// 过渡效果(进场离场)
// msg和type属性处理
// 定时器,关闭Toast组件
// 抽取定时器到actions的方法中

// 如何使用这个组件???

//this.xxx({msg:"xxx", type:"success"}) 展示提示组件

0 comments on commit 35b78ee

Please sign in to comment.