• uni-app学习笔记(二)


    目录

    一、路由与页面跳转

    1、tabar与普通页面跳转例子

    2、navigateTo

    3、switchTab

    二、vue组件

    1、传统vue组件的使用

    2、easycom

    三、uView组件库

    1、安装配置

    2、引入配置

    3、使用

    四、Vuex

    1、认识

    2、state基本使用

    3、mapState使用

    五、网络请求

    1、封装请求

    2、细化每个接口的配置

    3、实例调用

    六、媒体上传图片

    1、uni.chooseImage(OBJECT)

    2、代码实例


    一、路由与页面跳转

    1、tabar与普通页面跳转例子

    pages.json配置页面

    1. {
    2. "pages": [{
    3. "path": "pages/about/about",
    4. "style": {
    5. "navigationBarTitleText": "关于",
    6. "enablePullDownRefresh": false
    7. }
    8. },
    9. {
    10. "path": "pages/index/index",
    11. "style": {
    12. "navigationBarTitleText": "首页"
    13. }
    14. }, {
    15. "path": "pages/prompt/prompt",
    16. "style": {
    17. "navigationBarTitleText": "提示框",
    18. "enablePullDownRefresh": false
    19. }
    20. }, {
    21. "path": "pages/swiper/swiper",
    22. "style": {
    23. "navigationBarTitleText": "滑块",
    24. "enablePullDownRefresh": false
    25. }
    26. }, {
    27. "path": "pages/form/form",
    28. "style": {
    29. "navigationBarTitleText": "表单",
    30. "enablePullDownRefresh": false
    31. }
    32. }, {
    33. "path": "pages/router/router",
    34. "style": {
    35. "navigationBarTitleText": "路由",
    36. "enablePullDownRefresh": false
    37. }
    38. }
    39. ],
    40. "globalStyle": {
    41. "navigationBarTextStyle": "white",
    42. "navigationBarTitleText": "全局",
    43. "navigationBarBackgroundColor": "#000000",
    44. "backgroundColor": "#ffffff"
    45. },
    46. "tabBar": {
    47. "color": "#7A7E83",
    48. "selectedColor": "#1296db",
    49. "borderStyle": "black",
    50. "backgroundColor": "#000000",
    51. "list": [{
    52. "pagePath": "pages/index/index",
    53. "iconPath": "/static/首页2.png",
    54. "selectedIconPath": "/static/首页.png",
    55. "text": "首页"
    56. }, {
    57. "pagePath": "pages/about/about",
    58. "iconPath": "/static/关于2.png",
    59. "selectedIconPath": "/static/关于.png",
    60. "text": "关于"
    61. }, {
    62. "pagePath": "pages/form/form",
    63. "iconPath": "/static/表单 (1).png",
    64. "selectedIconPath": "/static/表单.png",
    65. "text": "表单"
    66. }, {
    67. "pagePath": "pages/router/router",
    68. "iconPath": "/static/路由器组2.png",
    69. "selectedIconPath": "/static/路由.png",
    70. "text": "路由"
    71. }]
    72. },
    73. "uniIdRouter": {}
    74. }

    router.vue文件,模拟页面跳转

    1. <script>
    2. export default {
    3. data() {
    4. return {
    5. }
    6. },
    7. methods: {
    8. toIndex(){
    9. uni.switchTab({
    10. url:"/pages/index/index"
    11. })
    12. },
    13. toSwiper(){
    14. uni.navigateTo({
    15. url:"/pages/swiper/swiper"
    16. })
    17. }
    18. }
    19. }
    20. script>

    2、navigateTo

    (1)跳转非tabBar页面的url路径,路径可以带参数

    格式:path?key=value&key2=value2

    path:要跳转页面的路径

    1. toSwiper(){
    2. uni.navigateTo({
    3. url:"/pages/swiper/swiper?name=sxx"
    4. })
    5. }

    (2)获取参数

    在跳转到的页面的onLoad函数获取

    1. onLoad(option) {
    2. console.log(option);
    3. },

    3、switchTab

    (1)跳转tabBar页面的url路径(需在 pages.json 的 tabBar 字段定义的页面),路径不能带参数

    (2)需要的参数可以写到全局变量去获取


    二、vue组件

    1、传统vue组件的使用

    (1)创建components文件夹=》创建.vue组件文件

    (2)在需要的页面引入、注册

    index.vue

    1. <script>
    2. // 注意路径components前没有/
    3. // 命名至少两个词组成,以小驼峰的形式
    4. import headerCpn from 'components/header.vue'
    5. export default {
    6. components:{
    7. headerCpn
    8. },
    9. data() {
    10. return {
    11. title: 'Hello'
    12. }
    13. },
    14. onLoad() {
    15. console.log(getApp().globalData);
    16. },
    17. methods: {
    18. }
    19. }
    20. script>
    21. <style>
    22. .content {
    23. display: flex;
    24. flex-direction: column;
    25. align-items: center;
    26. justify-content: center;
    27. }
    28. .logo {
    29. height: 200rpx;
    30. width: 200rpx;
    31. margin-top: 200rpx;
    32. margin-left: auto;
    33. margin-right: auto;
    34. margin-bottom: 50rpx;
    35. }
    36. .text-area {
    37. display: flex;
    38. justify-content: center;
    39. }
    40. .title {
    41. font-size: 36rpx;
    42. color: #8f8f94;
    43. }
    44. style>

    2、easycom

    (1)说明

    传统vue组件,需要安装、引用、注册,三个步骤后才能使用组件

    easycom将其精简为一步

    (2)使用

    只要组件安装在项目的 components 目录下,并符合components/组件名称/组件名称.vue

    通过插件库引入到components文件下然后直接引入即可

    DCloud 插件市场DCloud 插件市场icon-default.png?t=N7T8https://ext.dcloud.net.cn/可以看文件的命名和文档来决定引入标签的名字


    三、uView组件库

    1、安装配置

    (1)打开工程终端

    npm install uview-ui@1.8.8

    如果根目录又没有package.json文件,先执行以下命令

    npm init -y

    (2)uView依赖SCSS,必须要安装此插件,否则无法正常运行

    查看是否安装scss插件:点击工具=》插件安装

    ①前往插件市场安装

    ②终端命令安装

    1. // 安装node-sass
    2. npm i node-sass -D
    3. // 安装sass-loader
    4. npm i sass-loader -D

    2、引入配置

    (1)主JS库:在项目根目录中的main.js中,引入并使用uView的JS库

    1. // main.js
    2. import uView from "uview-ui";
    3. Vue.use(uView);

    注意要放在import Vue之后

    (2)全局SCSS:引入uView主题文件

    1. /* uni.scss */
    2. @import 'uview-ui/theme.scss';

    (3)引入基础样式

    App.vue首行的位置引入,注意给style标签加入lang="scss"属性

    (4) 配置easycom组件模式

    在项目根目录的pages.json中,配置easycom

    注意:

    ①只有一个easycom字段

    ②配置完要重启HX或重新编译项目

    1. // pages.json
    2. {
    3. "easycom": {
    4. "^u-(.*)": "uview-ui/components/u-$1/u-$1.vue"
    5. },
    6. // 此为本身已有的内容
    7. "pages": [
    8. // ......
    9. ]
    10. }

    3、使用

    (1)按钮

    1. <template>
    2. <view>
    3. 头部组件
    4. <u-button type="primary">主要按钮u-button>
    5. <u-button type="success">成功按钮u-button>
    6. <u-button type="info">信息按钮u-button>
    7. <u-button type="warning">警告按钮u-button>
    8. <u-button type="error">危险按钮u-button>
    9. view>
    10. template>


    四、Vuex

    1、认识

    uni-app 内置了Vuex

    (1)使用场景

    • 当一个组件需要多次派发事件时。例如购物车数量加减。
    • 跨组件共享数据、跨页面共享数据。例如订单状态更新。
    • 需要持久化的数据。例如登录后用户的信息。
    • 当您需要开发中大型应用,适合复杂的多模块多页面的数据交互,考虑如何更好地在组件外部管理状态时

    (2)规则

    • 应用层级的状态应该集中到单个 store 对象中。

    • 提交 mutation 是更改状态的唯一方法,并且这个过程是同步的。

    • 异步逻辑都应该封装到 action 里面

    2、state基本使用

    (1)在项目根目录下,新建 store 目录=》新建 index.js 文件

    1. import Vue from 'vue'
    2. import Vuex from 'vuex'
    3. Vue.use(Vuex);//vue的插件机制
    4. //Vuex.Store 构造器选项
    5. const store = new Vuex.Store({
    6. state:{
    7. msg:'存放的信息'
    8. },
    9. mutations:{},
    10. actions:{},
    11. getters:{}
    12. })
    13. export default store

    (2)在 main.js 中导入文件

    1. import store from './store'
    2. const app = new Vue({
    3. store,
    4. ...App
    5. })

    (3)获取数据

    3、mapState使用

    (1)当一个组件需要获取多个状态的时候,将这些状态都声明为计算属性会有些重复和冗余。 为了解决这个问题,可以使用 mapState 辅助函数 帮助我们生成计算属性

    1. <script>
    2. import { mapState } from 'vuex'//引入mapState
    3. export default {
    4. computed: mapState({
    5. // 从state中拿到数据 箭头函数可使代码更简练
    6. userName: state => state.userName,
    7. })
    8. }
    9. script>

    (2)当映射的计算属性名称与 state 的子节点名称相同时,可以给 mapState 传一个字符串数组

    1. <script>
    2. import { mapState } from 'vuex'//引入mapState
    3. export default {
    4. computed: mapState(['msg','userName'])
    5. }
    6. script>


    五、网络请求

    这里小编用天行API为实例

    天行数据TianAPI - 开发者API数据平台

    1、封装请求

    创建utils文件夹=》创建request.js文件

    1. const instance = (url,data,header,callback) => {
    2. const BASE_URL = "https://apis.tianapi.com"
    3. uni.request({
    4. url: BASE_URL + url,
    5. data,
    6. header,
    7. success: callback
    8. });
    9. }
    10. export default instance;

    2、细化每个接口的配置

    1. import instance from "../utils/request.js"
    2. // 获取用户信息(GET)
    3. export function getTianGou(data,callback){
    4. return instance(
    5. '/tiangou/index',
    6. data,
    7. {},
    8. callback
    9. )
    10. }

    3、实例调用


    六、媒体上传图片

    1、uni.chooseImage(OBJECT)

    参数介绍:

    ①count:最多可以选择的图片张数,默认9

    ②sizeType:original 原图,compressed 压缩图,默认二者都有

    1. uni.chooseImage({
    2. count: 6, //默认9
    3. sizeType: ['original', 'compressed'], //可以指定是原图还是压缩图,默认二者都有
    4. sourceType: ['album'], //从相册选择
    5. success: function (res) {
    6. console.log(JSON.stringify(res.tempFilePaths));
    7. }
    8. });

    2、代码实例

    1. <template>
    2. <view>
    3. <button type="default" @click="upImage">上传图片button>
    4. {{imgArr}}
    5. <image v-for="item in imgArr" :key="item" :src="item" mode="">image>
    6. view>
    7. template>
    8. <script>
    9. export default {
    10. data() {
    11. return {
    12. imgArr:[]
    13. }
    14. },
    15. methods: {
    16. // 上传图片事件
    17. upImage(){
    18. uni.chooseImage({
    19. count:6,
    20. success:res=> {
    21. console.log(res.tempFilePaths);
    22. console.log(this.imgArr);
    23. this.imgArr = res.tempFilePaths
    24. }
    25. })
    26. }
    27. }
    28. }
    29. script>

  • 相关阅读:
    台式电脑怎么格式化重装系统
    SpringBoot启动流程分析之创建SpringApplication对象(一)
    HTML+CSS美食静态网页设计——简单牛排美食餐饮(9个页面)公司网站模板企业网站实现
    Leetcode刷题【hot100】最长连续序列
    Tomcat 设置JVM内存大小
    LeetCode657.机器人能否返回原点
    23. 从零用Rust编写正反向代理,流控小姐姐的温柔一刀!
    C#,数值计算——函数计算,Dfridr的计算方法与源程序
    论文学习——降雨场次划分方法对降雨控制率的影响分析
    10.3使用Servlet写一个表白墙网站
  • 原文地址:https://blog.csdn.net/qq_51478745/article/details/134229293