赞
踩
uni - app
的 vuex
,常用api
,常用组件
,自定义组件
,第三方插件运用
等内容!import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex) const store = new Vuex.Store({ state: { //公共的变量,这里的变量不能随便修改,只能通过触发mutations的方法才能改变 }, mutations: { //相当于同步的操作 }, actions: { //相当于异步的操作,不能直接改变state的值,只能通过触发mutations的方法才能改变 } }) export default store
import App from './App' // #ifndef VUE3 import Vue from 'vue' Vue.config.productionTip = false App.mpType = 'app' const app = new Vue({ ...App }) app.$mount() // #endif // #ifdef VUE3 import { createSSRApp } from 'vue' export function createApp() { const app = createSSRApp(App) return { app } } // #endif //导入vuex 导入定义全局$store import store from '.ore/index.js' Vue.prototype.$store = store;
例如当取值:直接在页面中使用this.$store.state.变量名
//导入vuex import Vuex from 'vuex'; //导入vue import Vue from 'vue'; //使用Vuex Vue.use(Vuex); //导出Vuex export default new Vuex.Store({ state: { gTitle: { text: "你好vuex", color: "#000", fontSize: "24px", background: "#f70" }, joks: [], }, mutations: { setJoks(state, data) { state.joks = data; }, setSize(state, data) { state.gTitle.fontSize = data + "px"; }, SetBackgroundColor(state, data) { state.gTitle.background = data; } }, actions: { //和后端交互,异步操作都会放在actions中 getJok(context, data) { uni.request({ url: "http://520mg.com/mi/list.php", method: 'get', data: data, //axios get 请求参数用params,post用data //uni.request post和get传参都用data //根据content-type,如果是application/json,那么data是json,如果是urlencoded data是url编码形式 success: (res) => { console.log(res); context.commit('setJoks', res.data.result); } }) } }, //内部计算 getters: { //计算所有笑话的字数总和 "totalLen": function(state) { var count = 0; for (var i = 0; i < state.joks.length; i++) { count += state.joks[i].summary.length; } return count; } }, modules: {}, })
pages/glodal/glodal.vue
<template> <view> <view class="title">vuex数据</view> <!-- --> <view :style="$store.state.gTitle"> {{$store.state.gTitle.text}}--简写前 </view> <view :style="gTitle">{{gTitle.text}}--简写后</view> <navigator url="./fontSize">修改文本大小</navigator> <navigator url="./backgroundColor">修改背景颜色</navigator> <br> <view> 总{{$store.state.joks.length}}条笑话 </view> <view>{{totalLen}}字</view> <view> <view class="item" v-for="item in $store.state.joks"> {{item.summary}} </view> </view> </view> </template> <script> //state的简写 import {mapState,mapActions,mapGetters} from 'vuex' export default{ computed:{ //把全局的vuex数据转换为组件计算出来的只读的 ...mapState(["gTitle"]), ...mapGetters(['totalLen']) }, onLoad() { //调用getJok方法,并传入参数 // this.$store.dispatch("getJok",{page:1}) this.getJok({page:1}); }, methods:{ ...mapActions(["getJok"]) } } </script> <style lang="scss"> .item{ margin-top: 40rpx; } </style>
自行复制代码运行,查看
Vuex
的奥妙,知识面广,详细内容可阅读官方文档!
<button @click="onuploadphoto">图片上传</button> <button @click="ondownload">下载</button> <image :src="downloadfile" style="width: 300rpx;height: 270rpx;" mode="aspectFill"></image> data() { return { downloadfile:'' } }, onuploadphoto(){ uni.chooseImage({ count:1, sizeType:[], sourceType:[], success: (res) => { console.log(JSON.stringify(res)); }, fail: () => { console.log(JSON.stringify(err)); } }) }, ondownload(){ uni.downloadFile({ url:'https://img2.baidu.com/it/u=190271553,1135687029&fm=253&fmt=auto&app=138&f=JPEG?w=640&h=452', timeout:30000, success: (res) => { console.log(JSON.stringify(res)); this.downloadfile = res.tempFilePath; //显示图片 }, fail: (err)=>{ console.log(JSON.stringify(err)); } }) }
<!-- 循环显示选中多张图片 --> <view style="display: flex;"> <view v-for="(item,index) in picpaths" :key="index"> <image :src="item" style="width: 300rpx;height: 400rpx;" mode="aspectFill"></image> </view> </view> <image :src="picpath" style="width: 300rpx;height: 250rpx;" mode="aspectFill"></image> <!-- 显示图片 --> return { picpath:'', picpaths:[] } onchooseimg(){ let that = this; //外部定义this uni.chooseImage({ count:3, sizeType:[], success: (res) => { //base6写法 不用外部定义this console.log(JSON.stringify(res)); //回调显示图片 that.picpath = res.tempFilePaths[0]; that.picpaths = res.tempFilePaths; console.log(that.picpath); } }) }
<button @click="onpreimg">预览图片</button> onpreimg(){ uni.previewImage({ loop:false, indicator:'default', count:this.picpre.length, //存入图片列表长度 current:'', urls:this.picpre, //关键参数 success: (res) => { console.log(JSON.stringify(res)); }, fail: (err) => { console.log(err); } }) }
<!-- 动态显示图片,结合图片信息api -->
<image :style="{width: imgwidth+'rpx',height: imgheight+'rpx'}" src="../../static/logo.png"></image>
onLoad() {
//页面加载获取本地图片
uni.getImageInfo({
src:'../../static/logo.png',
success: (res) => {
console.log(res);
//动态获取真实图片宽高
this.imgwidth = res.width;
this.imgheight = res.height;
}
})
},
//条件编译 放到onload页面周期内 // #ifndef H5 let url = 'https://img2.baidu.com/it/u=297478628,2059211344&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=278'; uni.downloadFile({ url:url, success: (res) => { console.log(res); let file = res.tempFilePath; //保存到相册api 不支持h5 条件编译 uni.saveImageToPhotosAlbum({ filePath:file, success: (re) => { console.log(re); }, fail: (err) => { console.log(err); } }) } }) // #endif
uni.getSystemInfo
获取系统信息 经常用 屏幕宽度,高度
//onloaad生命周期 //同步操作会有等待 当建一个异步处理 可以用同步处理 let sync = uni.getSystemInfoSync(); new Promise((resolve,refuse)=>{ let sync1 = uni.getSystemInfoSync(); let sync2 = uni.getSystemInfoSync(); resolve && resolve(); }).then(res=>{ }).catch(err=>{ }) uni.getSystemInfo({ success: (res) => { console.log(JSON.stringify(res)); } })
uni.canIUse 判断应用的 API,回调,参数,组件等是否在当前版本可用
uni-app
常用组件(uni-app内部组件)最全的toast:
uni.showToast({
title:"请输入用户名密码!" ,
icon: 'none',
mask: true,
duration: 2000
})
默认success。
(1)none:没有图标
(2)error:错误图标
(3)loading:加载 (
4)success:成功
如果只是最简单的,超过7个字会显示不完全。
uni.showToast({
title:"请输入用户名密码!"
})
成功信息:
uni.showToast({
title: '提交成功',
duration: 2000
});
失败信息:
uni.showToast({
title: '提交失败',
icon: 'error',
duration: 2000
});
用户交互点击
uni.showModal({
title: '哦,答错了',
content: '是否加入错题本?',
success: function (res) {
if (res.confirm) {
console.log('用户点击确定');
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
存储:
uni.setStorageSync('token', 'hello');
取值:
uni.getStorageSync('token');
文本域
textarea值得注意的是默认限制输入长度140,如果打破这个限制,将其设置为-1
<textarea class="input-val" v-model="form.content" maxlength="-1" auto-height placeholder="请输入句子内容"/>
uni-app
实现自定义组件
props
属性一定要写在前面,写方法后面不生效!!
uni-app
第三方ui组件推荐&&引入的方法
引入的方法大多在官方文档里就有,这里写一下我的引入方法
既然是
uni项目
引入的ui组件 你应该要了解easycom,传统vue组件,需要安装、引用、注册
,三个步骤后才能使用组件。easycom将其精简为一步。
官方文档u-View的引入非常简单
// 安装 npm install uview-ui@2.0.31 //然后配置easycom组件模式 // pages.json { "easycom": { "^u-(.*)": "uview-ui/components/u-$1/u-$1.vue" }, // 此为本身已有的内容 "pages": [ // ...... ] }
但是我引入后不生效,是因为缺少了前面的css样式文件
// main.js
import uView from "uview-ui";
Vue.use(uView);
/* uni.scss */
@import 'uview-ui/theme.scss';
uView
基础样式<style lang="scss">
/* 注意要写在第一行,同时给style标签加入lang="scss"属性 */
@import "uview-ui/index.scss";
</style>
vant weapp
vantweapp 官方文档给出的只有引入微信开发者工具的,没有引入uni项目的
我依然使用easycom引入uni项目,步骤如下:
下载源文件:https://github.com/youzan/vant-weapp/releases
easycom:
只要组件安装在项目的components目录下
,并符合components/组件名称/组件名称
.vue目录结构。就可以不用引用、注册,直接在页面中使用
// pages.json-globalStyle同级下添加
"easycom": {
"autoscan": true, // 是否开启自动扫描
"custom": {
"^u-(.*)": "uview-ui/components/u-$1/u-$1.vue", // 这个是上面引入的uView
"van-(.*)": "@/wxcomponents/vant/$1/index"
}
}
正则表达式解释
“van-(.*)”-----指的是你用的标签,这种@/wxcomponents/vant/$1/index.vue------前面一段是正常路径,$1表示匹配vant下的所有文件夹,index.vue是目标文件,一开始是没有的,编译后会出现
当然使用在pages.json文件添加usingComponents也可以(你不嫌烦的话)
"globalStyle": {
...
"usingComponents": {
"van-button": "/wxcomponents/vant/button/index", // 以 Button 组件为例
"van-notice-bar": "/wxcomponents/vant/notice-bar/index"
}
},
问:为什么vant weapp不使用npm来安装到uni项目呢?而且相对轻便、快捷
答: 按理说 【npm i @vant/weapp -S --production 】后使用正则【“van-(.*)”:
“@vant/weapp/dist/$1/index”】引入到easycom是没错的,但是编译小程序后就是会有各种各样的报错,所以放弃了
vant weapp
的目录结构如下:
温馨提示,官方文本教程最全,可以仔细阅读
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。