uniapp开发总结

CSS样式

推荐使用scss

学习地址:https://www.sass.hk/

学习地址:http://www.ruanyifeng.com/blog/2012/06/sass.html

html标签嵌套和css样式一一对应,方便维护

页面或组件的第一个标签的class名称和文件名称一样

尽量使用 > 子元素选择器

尽量不在html标签内写样式,当html标签内的样式超过三个时,应为其建立一个class

image

动态写入样式,样式名应改为小驼峰命名,例如:padding-top => paddingTop

image

当同类别标签只有一个时,可以不命名class,使用 > 子元素选择器对相应的标签设置样式

image

页面中不同功能模块之间应加注释并空一行

image

JavaScript

尽量使用es6语法

学习地址:http://es6.ruanyifeng.com/

举例几点常用规范:

使用let代替var,常量应使用const定义

使用箭头函数,可以正常取到页面中的this对象

methods中的方法命名时应加一个前缀,作者使用的是hd,handle的缩写,以区别外部引入的方法

方法应有备注,并且上下空一行

在功能调试完后,相关的打印代码可保留,但应该注释掉

image

数据请求应封装成promise进行调用,并使用try catch处理异常,对错误进行提示

尽量减少代码嵌套

image

应使用 === 进行强类型比较,而不是 ==

当情况只有两种时,尽量用三元运算,而不是if else

字符的处理尽量用模板字符串 `` 而不是 + ,变量放在${}里面

image

vuex

学习资料:https://vuex.vuejs.org/zh/guide/

vuex的文件结构

store文件夹位于根目录


image.png

模块模板,文件名article.js

export default {
        // 命名空间
    namespaced: true,
        // 变量数据
    state: {
        detail: {}
    },
        // 获取变量的快捷方法
    getters: {},
        // 变量写操作
    mutations: {
        SET_detail(state, obj) {
            state.detail = obj
        }
    },
        // 复杂且异步的方法
    actions: {
        functionName({state, commit, dispatch}, data) {
            return new Promise((resolve, reject) => {
                // 发出一个请求
                request().then(res => {
                    // 正确的请求响应
                    // 处理数据,调用同模块中的mutations
                    commit('SET_detail', res)
                    // 处理数据,调用同模块中的actions
                    dispatch('functionName2', res)
                    // 返回正确
                    resolve(res)
                }).catch(err => {
                    // 错误的请求响应
                    // 返回错误
                    reject(err)
                })
            })
        },
        functionName2({state, commit, dispatch}, data) {
            return new Promise((resolve, reject) => {
                resolve()
            })
        }
    }
}

写好的模块要在index.js中引用

import Vue from 'vue'
import Vuex from 'vuex'
import article from './modules/article'
Vue.use(Vuex)
export default new Vuex.Store({
    modules: {
        article
    }
})

最终在main.js中引用

import Vue from 'vue'
import App from './App'

import store from './store/index'
Vue.prototype.$store = store

App.mpType = 'app'

const app = new Vue({
    ...App,
    store,
})
app.$mount()

引用模块中的变量和方法

// 引用变量
const { detail } = this.$store.state.article;
// 引用mutations
this.$store.commit('article/SET_detail', {});
// 引用actions
this.$store.dispatch('article/functionName', {})

Nativejs

js操作移动端底层的相关API

学习地址:http://www.html5plus.org/doc/h5p.html

制作一个全局显示的组件

相应api:http://www.html5plus.org/doc/zh_cn/nativeobj.html#plus.nativeObj.View

export default {    
    showWeixinS: {},    
    showWeixinV: {},    
    showWeixinO: {},    
    init() {    
        const screenHeight = plus.screen.resolutionHeight;  
        const screenWidth = plus.screen.resolutionWidth;    
        // 初始化普通遮罩  
        this.showWeixinS = new plus.nativeObj.View("showWeixinS", {
            top: '0px',
        left: '0px',    
            height: '100%', 
            width: '100%',  
            backgroundColor: 'rgba(0,0,0,0.5)'  
        })  
        // 内容   
        this.showWeixinV = new plus.nativeObj.View("showWeixinV", { 
            top: (screenHeight - 315) / 2,  
            height: 315,    
            left: (screenWidth - 300) / 2,  
            width: 300  
        }); 
        const time = new Date().getTime();
        this.showWeixinV.draw([
            {   
                // 背景   
                tag: 'rect',    
                color: 'rgb(255,255,255)',  
                rectStyles: {   radius: '10px'  },  
                position: {  width: '300px',  height: '315px'  }
            },  {   
                // 头像   
                tag: 'img',
                id: time + 'img',   
                src: '/static/logo.png',    
                position: {  top: '28px',  left: (300 - 45) / 2,  width: '45px',  height: '45px'  } 
            },  {   
                tag: 'font',    id: Math.random() * 1000 + time,    
                text: '导师微信',   
                textStyles: {  size: 15,  align: 'center'  },   
                position: {   top: '80px',  left: 0,  width: 300,  height: 21  }    
            },  {
            // 微信号
                tag: 'font',    
                id: time + 'num',   
                text: 'num',    
                textStyles: {  size: 18,  align: 'center',  overflow: 'ellipsis'  },    
                position: {  top: '104px',  left: 0,  width: 300,  height: 25  }    
            },  {   
                // 按钮   
                tag: 'rect',    
                color: 'rgba(255, 140, 148, 1)',    
                rectStyles: {  radius: 23  },   
                position: {  top: 150,  left: 40,  width: 220,  height: 46  }   
            },  {   
                // 按钮文字 
                tag: 'font',    id: Math.random() * 1000 + time,    
                text: '复制并打开微信',    
                textStyles: {  size: 18,  color: 'rgba(255, 255, 255, 1)',  align: 'center',  verticalAlign: 'middle',  overflow: 'ellipsis'  },    
                position: {  top: 150,  left: 40,  width: 220,  height: 46  }   
            },  {
                // 图标
                tag: 'img', 
                id: Math.random() * 1000 + time,    
                src: '/static/images/comm/remind.png', 
                position: {   top: 219,  left: 22,  width: 17,  height: 17  }   
            }, {    
                // 提示文字
                tag: 'font',    
                id: Math.random() * 1000 + time,    
                text: ' 温馨提示,为了保障您的权益,跳出APP切勿进行私人转账',   
                textStyles: {  size: 12,  color: 'rgba(153, 153, 153, 1)',  align: 'left',  whiteSpace: 'normal'  },    
                position: {  top: 216,  left: 22,  width: 260,  height: 40  }   
            },  
        ]); 
        //处理遮罩层点击   
        this.showWeixinS.addEventListener("click", () => { 
            this.showWeixinS.hide();
            this.showWeixinV.hide();    
        }); 
        this.showWeixinO = {    
            imgId: time + 'img',    
            numId: time + 'num'
    };  
    },  

    showWeixin({ num, imgUrl }) {   
        const { showWeixinS, showWeixinV, showWeixinO } = this; 
        const screenHeight = plus.screen.resolutionHeight;  
        const screenWidth = plus.screen.resolutionWidth;    
        showWeixinV.clearRect({ top: '0px', left: '0px', width: '300px', height: '315px' }, showWeixinO.imgId); 
        showWeixinV.clearRect({ top: '0px', left: '0px', width: '300px', height: '315px' }, showWeixinO.numId); 
        showWeixinV.draw([  
            {   
                // 头像   
                tag: 'img',
                id: showWeixinO.imgId,  
                src: imgUrl,    
                position: { top: '28px',    left: (300 - 45) / 2,   width: '45px', height: '45px',  }   
            },  
            {   
                // 微信号  
                tag: 'font',
                id: showWeixinO.numId,
                text: num,  
                textStyles: {   size: 18,   align: 'center',    overflow: 'ellipsis'    }, 
                position: { top: '104px',   left: 0,    width: 300, height: 25  }   
            },  
        ]); 
        // 点击按钮 
        this.showWeixinV.addEventListener("click", (e) => { 
            const top = (screenHeight - 315) / 2 + 150; 
            const left = (screenWidth - 300) / 2 + 40;  
            if (top < e.screenY && e.screenY < top + 46 && left < e.screenX && e.screenX < left + 220) {    
                uni.setClipboardData({  
                    data: num,  
                    success: () => {    
                        console.log('success'); 
                        plus.runtime.launchApplication({ 
                            pname: 'com.tencent.mm', 
                            extra: { url: 'http://www.html5plus.org' }
                        },  function(e) {   
                            alert('Open system default browser failed: ' + e.message);  
                        }); 
                    },  
                    fail:(err) => { 
                        console.log('err', err);    
                    }   
                }); 
            }   
        }); 
        showWeixinS.show()  
        showWeixinV.show()  
    },  
    // 关闭弹窗 
    hideWeixinPanel() { 
        const { showWeixinS, showWeixinV } = this;  
        showWeixinS.hide(); 
        showWeixinV.hide(); 
    }
}  

在main.js引用到全局进行调用

// 私有子窗口
import msv from './utils/mySubView.js'
Vue.prototype.$msv = msv;

效果


image.png

使用uniapp的subNVue

配置:https://uniapp.dcloud.io/collocation/pages?id=subpackages
相关API:https://uniapp.dcloud.io/api/window/subNVues?id=app-getsubnvuebyid

以下举例制作全局引用

文件路径


image.png

在引用的页面需在pages.json中配置


image.png

子窗口:showWeixin.nvue

<template>
    <div class="show-weixin">
        <image class="sw-image" :src="imgUrl"></image>
        <text style="font-size: 30rpx; margin-top: 10rpx;">导师微信</text>
        <text style="font-size: 36rpx; font-weight:500; margin-top: 10rpx;">{{ num }}</text>
        <div class="sw-button" @click="hdGotoWeixin"><text class="swb-text">复制并打开微信</text></div>
        <div class="sw-remind">
            <image class="swr-image" src="../../../static/images/comm/remind.png"></image>
            <text class="swr-text">温馨提示,为了保障您的权益,跳出APP切勿</text>
            <text class="swr-text">进行私人转账</text>
        </div>
    </div>
</template>

<script>
export default {
    data() {
        return {
            num: '',
            imgUrl: ''
        };
    },
    created() {
        const subNVue = uni.getSubNVueById('showWeixin');
        subNVue.onMessage(msg => {
            // console.log('监听来自所属页面的 message' + JSON.stringify(msg));
            const { num, imgUrl } = msg.data;
            this.num = num;
            this.imgUrl = imgUrl;
        });
    },
    methods: {
        // 前往微信
        hdGotoWeixin() {
            const { num } = this;
            // console.log('hdGotoWeixin', num);
            uni.setClipboardData({
                data: num,
                success: () => {
                    console.log('success');
                    plus.runtime.launchApplication(
                        { pname: 'com.tencent.mm', extra: { url: 'http://www.html5plus.org' } },
                        function(e) {
                            alert('Open system default browser failed: ' + e.message);
                        }
                    );
                    const subNVue = uni.getSubNVueById('showWeixin');
                    subNVue.hide('fade-in', 200);
                },
                fail: err => {
                    console.log('err', err);
                }
            });
        }
    }
};
</script>

<style scoped>
.show-weixin {
    flex: 1;
    border-radius: 20rpx;
    background-color: #ffffff;
    flex-direction: column;
    align-items: center;
}
.sw-image {
    width: 90rpx;
    height: 90rpx;
    border-radius: 45rpx;
    margin-top: 56rpx;
}
.sw-button {
    width: 440rpx;
    height: 90rpx;
    margin-top: 40rpx;
    background-color: rgba(255, 140, 148, 1);
    border-radius: 45rpx;
    justify-content: center;
    align-items: center;
}
.swb-text {
    font-size: 30rpx;
    color: #ffffff;
}
.sw-remind {
    width: 520rpx;
    margin-top: 48rpx;
    flex-direction: row;
    flex-wrap: wrap;
}
.swr-image {
    width: 34rpx;
    height: 34rpx;
    margin-right: 10rpx;
}
.swr-text {
    font-size: 24rpx;
    color: rgba(153, 153, 153, 1);
}
</style>

相关页面引用

        // 把微信号写到粘贴板
        hdGetNum() {
            const { weChatNo, merchantImage } = this.audio;
            const subNVue = uni.getSubNVueById('showWeixin');
            subNVue.postMessage({
                num: weChatNo,
                imgUrl: `${this._API.fileUrl}${merchantImage}`
            });
            subNVue.show('fade-in', 250);
        },

常用uniapp插件

下拉刷新,可npm安装:https://ext.dcloud.net.cn/plugin?id=343

自定义头部:https://ext.dcloud.net.cn/plugin?id=974

图片裁剪:https://ext.dcloud.net.cn/plugin?id=1076

分享:https://ext.dcloud.net.cn/plugin?id=482

常用npm模块

时间日期格式转化 moment:http://momentjs.cn/docs/#/displaying/

表单校验 validator:https://www.npmjs.com/package/validator

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 216,843评论 6 502
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,538评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,187评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,264评论 1 292
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,289评论 6 390
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,231评论 1 299
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,116评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,945评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,367评论 1 313
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,581评论 2 333
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,754评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,458评论 5 344
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,068评论 3 327
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,692评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,842评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,797评论 2 369
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,654评论 2 354

推荐阅读更多精彩内容

  • uni-app跨平台框架官方教程 链接:https://ke.qq.com/course/343370 一、框架简...
    Neyo_凉阅读 36,217评论 0 43
  • 1.基础问题 1. = 和 == 和 === 的区别? = : 用于赋值== : 用于判断=== : 用于判断,必...
    月光在心中阅读 2,366评论 0 6
  • 选择qi:是表达式 标签选择器 类选择器 属性选择器 继承属性: color,font,text-align,li...
    love2013阅读 2,314评论 0 11
  • 前端开发面试知识点大纲: HTML&CSS: 对Web标准的理解、浏览器内核差异、兼容性、hack、CSS基本功:...
    秀才JaneBook阅读 2,362评论 0 25
  • 前端开发知识点 HTML&CSS对Web标准的理解、浏览器内核差异、兼容性、hack、CSS基本功:布局、盒子模型...
    Hebborn_hb阅读 845评论 0 1