uniapp mixin 简单使用

学习 · 2021-03-30

在目录下,或者找个目录编写mixin.js文件

/**
 * 
 */
import {cache} from "@/utils/util.js"
var setting = cache('setting');
export default {
    data() {
        return {
            
        }
    },
    onShow: function() {
        console.log('Page Show')
    },
    methods: {
        // 图片加载
        $img(url, option= {}){
            // local
            if(option.from == 'local'){
                return '/static/' + (option.model || 'reception') + '/' + url + '?t=' + (new Date).getTime()
            }else
                return setting.upload_domain + url + '?t=' + (new Date).getDate()
        },
        // 全局处理 model reception enterprise
        $navgate(url, option = {}){
            var that = this;
            // model
            option.model = (option.model || 'reception')
            // 退出登录
            if(url == 'logout'){
                uni.$emit('model', {
                    show: true,
                    content: 'Thoát tài khoản hiện tại!',
                    confirm: () => {
                        that.$cache('token', null);
                        that.$cache('userInfo', null);
                        // 登录
                        uni.redirectTo({
                            url: '/pages/' + option.model + '/auth/login'
                        })
                    }
                })
                return false
            }
            // 路径组合
            url = '/pages/' + option.model +  '/' + url;
            
            console.log(url, option)
            // 延迟
            setTimeout(() => {
                // 类型处理
                if(option.type == 'switch'){
                    uni.switchTab({url})
                }else if(option.type == 'redirect'){
                    uni.redirectTo({url})
                }else
                    uni.navigateTo({url})
            }, option.delay || 200)
        }
    }
}

在main.js 下引入

// mixin
import mixin from "common/lib/mixin.js"
Vue.mixin(mixin);
uniapp mixin vue
Theme Jasmine by Kent Liao | 桂ICP备15008025号-6