1.解决 toast 中传入 html 的问题,经过假的 slot 来实现css
// plugins.js toast.$slots.default = [message] // toast.vue <div v-html="$slots.default[0]"></div> // 使用 created() { this.$toast('<p>我是<strong>hi</strong></p>',{}) },
2.在 toast 中加 html 是比较危险的一个动做,因此要加一个选项默认不开启。html
// toast.vue <slot v-if="!enableHtml"></slot> <div v-else v-html="$slots.default[0]"></div> // plugin.js,进行传递参数的改写 propsData:toastOptions // 使用 created() { this.$toast('<p>我是<strong>hi</strong></p><a href="http://qq.com">qq</a>',{ enableHtml: false }) },
3.flex-shrink的使用,flex-shrink属性定义了项目的缩小比例,默认为1,即若是空间不足,该项目将缩小。vue
.item { flex-shrink: <number>; /* default 1 */ }
若是全部项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。若是一个项目的flex-shrink属性为0,其余项目都为1,则空间不足时,前者不缩小。若是数值越大,缩小比例越大。git
4.line的高度问题,若是高度写了最小高度,那么子元素的height%就不生效了。用js去操做line的高度。app
// toast.vue <div class="toast" ref="wrapper"> <div class="line" ref="line"></div> </div> mounted() { this.$nextTick(()=>{ this.$refs.line.style.height = `${this.$refs.wrapper.getBoundingClientRect().height}px` }) }, // 这个计较太trick
debugger的技巧,若是眼睛观察到的是0,可是打印出来不是0,可能就是异步的问题。异步
5.增长toast的位置。函数
// toast.vue props: { position: { type: String, default: 'top', validator(value){ return ['top', 'bottom', 'middle'].indexOf(value) >= 0 } } }, computed:{ toastClasses(){ return { [`position-${this.position}`]:true } } } // 使用 this.$toast('你的智商须要充值', { position: 'bottom' }) // plugin.js export default { install(Vue, options){ Vue.prototype.$toast = function (message, toastOptions) { let Constructor = Vue.extend(Toast) let toast = new Constructor({ propsData:toastOptions // 在这里将position传进去 }) toast.$slots.default = [message] toast.$mount() document.body.appendChild(toast.$el) } } }
6.开始作若是已经有一个toast就把以前那个干掉,再出现。flex
// plugin.js import Toast from './toast' let currentToast export default { install(Vue, options){ Vue.prototype.$toast = function (message, toastOptions) { if(currentToast){ currentToast.close() } currentToast = createToast({Vue,message, propsData: toastOptions}) } } } /* helpers */ function createToast ({Vue,message,propsData}){ let Constructor = Vue.extend(Toast) let toast = new Constructor({propsData}) toast.$slots.default = [message] toast.$mount() document.body.appendChild(toast.$el) return toast }
7.实现动画优化
@keyframes fade { 0% {opacity: 0; transform: translateY(100%);} 100% {opacity: 1;transform: translateY(0);} } .toast { animation: fade 1s; }
// toast.vue close() { this.$el.remove() this.$emit('close') this.$destroy() } // plugin.js export default { install(Vue, options){ Vue.prototype.$toast = function (message, toastOptions) { if(currentToast){ currentToast.close() } currentToast = createToast({Vue,message, propsData: toastOptions,onclose: ()=>{ currentToast = null } }) // 加了这句话 } } } /* helpers */ function createToast ({Vue,message,propsData,onclose}){ let Constructor = Vue.extend(Toast) let toast = new Constructor({propsData}) toast.$slots.default = [message] toast.$mount() toast.$on('close',onclose) // 加了这句话 document.body.appendChild(toast.$el) return toast }
本文由博客一文多发平台 OpenWrite 发布!动画