微信小程序> 封装微信小程序消息提示框

封装微信小程序消息提示框

浏览量:2113 时间: 来源:漂移的电子

在mpvue框架中,封装微信小程序消息提示框。快速开发,少敲代码。若要多次使用,建议封装

//新建toast.js文件(路径utils/toast.js),封装函数function showToast (params) {  wx.showToast({    title: params,    icon: 'success',    duration: 2000  })}export default {  showToast}
//在公共main.js,引入import Vue from 'vue'import App from './App'import WxToast from './utils/toast' //引入Vue.config.productionTip = falseVue.prototype.$toast = WxToastconst app = new Vue(App)app.$mount()

vue文件中使用

<template>  <div>    <button @click="fn">点击</button>  </div></template><script>export default {  data () {    return {}  },  methods: {    fn () {      this.$toast.showToast('我是消息提示框')  //一行代码搞定    }  }}</script>

以上效果等价于:

<template>  <div>    <button @click="fn">点击</button>  </div></template><script>export default {  data () {    return {}  },  methods: {    fn () {      wx.showToast({        title: '我是消息提示框',        icon: 'success',        duration: 2000      })    }  }}</script>

小程序

版权声明

即速应用倡导尊重与保护知识产权。如发现本站文章存在版权问题,烦请提供版权疑问、身份证明、版权证明、联系方式等发邮件至197452366@qq.com ,我们将及时处理。本站文章仅作分享交流用途,作者观点不等同于即速应用观点。用户与作者的任何交易与本站无关,请知悉。

  • 头条
  • 搜狐
  • 微博
  • 百家
  • 一点资讯
  • 知乎