• uni-app学习笔记


    目录

    一、前期准备

    1、项目认识

    2、pages.json基本配置

    3、创建页面

    二、tabBar

    1、获取图标

    2、代码配置

    三、基础认识

    1、页面生命周期

    2、App.vue应用生命周期

    四、基础组件

    1、scroll-view可滚动视图区域

    2、提示框

    3、swiper滑块视图容器

    4、form表单组件


    一、前期准备

    1、项目认识

    (1)新建项目

    (2)启动项目

    ①运行到浏览器上

    ②运行到微信开发者工具

    如果出现以下弹框,选择已安装好的微信开发者工具路径,确定即可

    如果出现“工具的服务端口已关闭”,是因为微信开发者工具设置问题

    解决如下:

    随便打开微信开发者工具的一个工程=》点击设置=》通用设置=》安全=》打开服务端口

    (3)中文解释

    2、pages.json基本配置

    (1)globalStyle全局配置

    1. {
    2. "globalStyle": {
    3. "navigationBarTextStyle": "black",
    4. "navigationBarTitleText": "全局",
    5. "navigationBarBackgroundColor": "#F8F8F8",
    6. "backgroundColor": "#F8F8F8"
    7. },
    8. }

    (2)局部页面的配置

    1. {
    2. "pages": [ //pages数组中第一项表示应用启动页,参考:https://uniapp.dcloud.io/collocation/pages
    3. {
    4. "path": "pages/index/index",
    5. "style": {
    6. "navigationBarTitleText": "首页"
    7. }
    8. }
    9. ],
    10. }

    3、创建页面

    (1)右键pages文件夹=》点击新建页面

    (2)新页面配置

    ①如果没有自动生成需要手动添加配置

    ②注意:与微信小程序一样,配置时,哪个页面放在前面,就先显示哪个页面

    1. {
    2. "pages": [
    3. {
    4. "path" : "pages/about/about",
    5. "style" :
    6. {
    7. "navigationBarTitleText": "关于",
    8. "enablePullDownRefresh": false
    9. }
    10. },
    11. {
    12. "path": "pages/index/index",
    13. "style": {
    14. "navigationBarTitleText": "首页"
    15. }
    16. }
    17. ],
    18. "globalStyle": {
    19. "navigationBarTextStyle": "black",
    20. "navigationBarTitleText": "全局",
    21. "navigationBarBackgroundColor": "#F8F8F8",
    22. "backgroundColor": "#F8F8F8"
    23. },
    24. "uniIdRouter": {}
    25. }


    二、tabBar

    1、获取图标

    (1)小伙伴可以在iconfont选择需要的图标

    iconfont-阿里巴巴矢量图标库iconfont-国内功能很强大且图标内容很丰富的矢量图标库,提供矢量图标下载、在线存储、格式转换等功能。阿里巴巴体验团队倾力打造,设计和前端开发的便捷工具icon-default.png?t=N7T8https://www.iconfont.cn/(2)将下载好的图标放到static文件夹中

    2、代码配置

    1. {
    2. "pages": [
    3. {
    4. "path" : "pages/about/about",
    5. "style" :
    6. {
    7. "navigationBarTitleText": "关于",
    8. "enablePullDownRefresh": false
    9. }
    10. },
    11. {
    12. "path": "pages/index/index",
    13. "style": {
    14. "navigationBarTitleText": "首页"
    15. }
    16. }
    17. ],
    18. "globalStyle": {
    19. "navigationBarTextStyle": "white",
    20. "navigationBarTitleText": "全局",
    21. "navigationBarBackgroundColor": "#000000",
    22. "backgroundColor": "#ffffff"
    23. },
    24. "tabBar": {
    25. "color": "#7A7E83",
    26. "selectedColor": "#1296db",
    27. "borderStyle": "black",
    28. "backgroundColor": "#000000",
    29. "list": [{
    30. "pagePath": "pages/index/index",
    31. "iconPath": "/static/首页2.png",
    32. "selectedIconPath": "/static/首页.png",
    33. "text": "首页"
    34. }, {
    35. "pagePath": "pages/about/about",
    36. "iconPath": "/static/关于2.png",
    37. "selectedIconPath": "/static/关于.png",
    38. "text": "关于"
    39. }]
    40. },
    41. "uniIdRouter": {}
    42. }


    三、基础认识

    1、页面生命周期

    (1)onLoad 监听页面加载【类似于 vue2 生命周期中的 create】

    此时响应式数据、计算属性、方法、侦听器、props、slots 已设置完成,其参数为上个页面传递的数据,参数类型为 Object

    2、App.vue应用生命周期

    (1)globalData 全局变量,此处定义的变量,可以在任何页面获取

    ①App.vue

    ②index.vue


    四、基础组件

    1、scroll-view可滚动视图区域

    1. <script>
    2. export default {
    3. data() {
    4. return {
    5. // 设置竖向滚动条位置
    6. scrollTop: 0,
    7. old: {
    8. scrollTop: 0
    9. }
    10. }
    11. },
    12. onLoad() {
    13. console.log(getApp().globalData);
    14. },
    15. methods: {
    16. // 滚动到顶部/左边会触发
    17. upper: function(e) {
    18. console.log(e)
    19. },
    20. // 滚动到底部/右边会触发
    21. lower: function(e) {
    22. console.log(e)
    23. },
    24. // 滚动时触发
    25. scroll: function(e) {
    26. console.log(e)
    27. this.old.scrollTop = e.detail.scrollTop
    28. },
    29. // 返回顶部
    30. goTop: function(e) {
    31. // 解决view层不同步的问题
    32. this.scrollTop = this.old.scrollTop
    33. this.$nextTick(function() {
    34. this.scrollTop = 0
    35. });
    36. // 弹窗
    37. uni.showToast({
    38. icon: "none",
    39. title: "纵向滚动 scrollTop 值已被修改为 0"
    40. })
    41. }
    42. }
    43. }
    44. script>
    45. <style>
    46. .scroll-Y {
    47. height: 300rpx;
    48. }
    49. .scroll-view_H {
    50. white-space: nowrap;
    51. width: 100%;
    52. }
    53. .scroll-view-item {
    54. height: 300rpx;
    55. line-height: 300rpx;
    56. text-align: center;
    57. font-size: 36rpx;
    58. }
    59. .scroll-view-item_H {
    60. display: inline-block;
    61. width: 100%;
    62. height: 300rpx;
    63. line-height: 300rpx;
    64. text-align: center;
    65. font-size: 36rpx;
    66. }
    67. .uni-bg-red{
    68. background-color: red;
    69. }
    70. .uni-bg-green{
    71. background-color: green;
    72. }
    73. .uni-bg-blue{
    74. background-color: blue;
    75. }
    76. style>

    2、提示框

    (1)showToast显示消息提示框

    (2)showLoading显示 loading 加载中

    1. <script>
    2. export default {
    3. data() {
    4. return {
    5. }
    6. },
    7. methods: {
    8. // 显示消息提示框
    9. showToast: function(e) {
    10. uni.showToast({
    11. icon: "none",
    12. title: "显示消息内容"
    13. })
    14. },
    15. // 显示Loading提示框
    16. showLoading: function(e) {
    17. uni.showLoading({
    18. title: "加载中"
    19. })
    20. // 2秒后提示隐藏
    21. setTimeout(function () {
    22. uni.hideLoading();
    23. }, 2000);
    24. },
    25. }
    26. }
    27. script>
    28. <style>
    29. .btn{
    30. width: 80%;
    31. margin:20rpx auto;
    32. }
    33. style>

    3、swiper滑块视图容器

    1. <script>
    2. export default {
    3. data() {
    4. return {
    5. // 是否显示面板指示点
    6. indicatorDots: true,
    7. // 是否自动切换
    8. autoplay: true,
    9. interval: 2000,
    10. duration: 500,
    11. imgArr:[{
    12. title:"A",
    13. background:"uni-bg-red"
    14. },
    15. {
    16. title:"B",
    17. background:"uni-bg-green"
    18. },
    19. {
    20. title:"C",
    21. background:"uni-bg-blue"
    22. }]
    23. }
    24. },
    25. methods: {
    26. changeIndicatorDots(e) {
    27. this.indicatorDots = !this.indicatorDots
    28. },
    29. changeAutoplay(e) {
    30. this.autoplay = !this.autoplay
    31. },
    32. intervalChange(e) {
    33. this.interval = e.target.value
    34. },
    35. durationChange(e) {
    36. this.duration = e.target.value
    37. }
    38. }
    39. }
    40. script>
    41. <style>
    42. .uni-margin-wrap {
    43. width: 690rpx;
    44. width: 100%;
    45. }
    46. .swiper {
    47. height: 300rpx;
    48. }
    49. .swiper-item {
    50. display: block;
    51. height: 300rpx;
    52. line-height: 300rpx;
    53. text-align: center;
    54. }
    55. .swiper-list {
    56. margin-top: 40rpx;
    57. margin-bottom: 0;
    58. }
    59. .uni-common-mt {
    60. margin-top: 60rpx;
    61. position: relative;
    62. }
    63. .info {
    64. position: absolute;
    65. right: 20rpx;
    66. }
    67. .uni-padding-wrap {
    68. width: 550rpx;
    69. padding: 0 100rpx;
    70. }
    71. style>

     

    注意:如果滑块没有背景色,是因为小编把背景色的css配置在了App.vue的全局样式

    4、form表单组件

    (1)说明

    <form @submit="formSubmit" @reset="formReset">form>

    ① @submit:携带 form 中的数据,触发 submit 事件

    ② @reset:表单重置,触发 reset 事件

    (2)代码实例

    1. <script>
    2. export default {
    3. data() {
    4. return {
    5. }
    6. },
    7. methods: {
    8. formSubmit: function(e) {
    9. console.log('form发生了submit事件,携带数据为:' + JSON.stringify(e.detail.value))
    10. var formdata = e.detail.value
    11. uni.showModal({
    12. content: '表单数据内容:' + JSON.stringify(formdata),
    13. showCancel: false
    14. });
    15. },
    16. formReset: function(e) {
    17. console.log('清空数据')
    18. }
    19. }
    20. }
    21. script>
    22. <style>
    23. .uni-form-item .title {
    24. padding: 20rpx 0;
    25. }
    26. style>

     form 中的组件需要加上 name 来作为 key,使得 submit 事件会将组件中的 value 值进行提交

  • 相关阅读:
    JVM内存模型
    【Java程序员面试专栏 算法思维】一 高频面试算法题:排序算法
    java计算机毕业设计科研成果管理系统设计与实现MyBatis+系统+LW文档+源码+调试部署
    95后阿里P7晒出工资单:真的是狠狠扎心了...
    外贸SEO 站长工具
    Vue中的常用指令v-html / v-show / v-if / v-else / v-on / v-bind / v-for / v-model
    Swoole Compiler 加密PHP源代码(简版)
    RabbitMQ:消息队列的卓越之选
    Mygin上下文之sync.Pool复用
    vant3的option写法示例(部分组件:swipe、popup、picker、stepper、field、tab、tabbar)
  • 原文地址:https://blog.csdn.net/qq_51478745/article/details/133847463