uniapp pages.json 页面路由

pages.json 页面路由

pages.json 文件用来对 uni-app 进行全局配置,决定页面文件的路径、窗口样式、原生的导航栏、底部的原生tabbar 等。

它类似微信小程序中app.json页面管理部分。注意定位权限申请等原属于app.json的内容,在uni-app中是在manifest中配置。

配置项列表

属性类型必填描述平台兼容
globalStyleObject设置默认页面的窗口表现
pagesObject Array设置页面路径及窗口表现
easycomObject组件自动引入规则2.5.5+
tabBarObject设置底部 tab 的表现
conditionObject启动模式配置
subPackagesObject Array分包加载配置H5 不支持
preloadRuleObject分包预下载规则微信小程序
workersStringWorker 代码放置的目录微信小程序
leftWindowObject大屏左侧窗口H5
topWindowObject大屏顶部窗口H5
rightWindowObject大屏右侧窗口H5
uniIdRouterObject自动跳转相关配置,新增于HBuilderX 3.5.0
entryPagePathString默认启动首页,新增于HBuilderX 3.7.0微信小程序、支付宝小程序

以下是一个包含了所有配置选项的 pages.json

{
	"pages": [{
		"path": "pages/component/index",
		"style": {
			"navigationBarTitleText": "组件"
		}
	}, {
		"path": "pages/API/index",
		"style": {
			"navigationBarTitleText": "接口"
		}
	}, {
		"path": "pages/component/view/index",
		"style": {
			"navigationBarTitleText": "view"
		}
	}],
	"condition": { //模式配置,仅开发期间生效
		"current": 0, //当前激活的模式(list 的索引项)
		"list": [{
			"name": "test", //模式名称
			"path": "pages/component/view/index" //启动页面,必选
		}]
	},
	"globalStyle": {
		"navigationBarTextStyle": "black",
		"navigationBarTitleText": "演示",
		"navigationBarBackgroundColor": "#F8F8F8",
		"backgroundColor": "#F8F8F8",
		"usingComponents":{
			"collapse-tree-item":"/components/collapse-tree-item"
		},
		"renderingMode": "seperated", // 仅微信小程序,webrtc 无法正常时尝试强制关闭同层渲染
		"pageOrientation": "portrait", //横屏配置,全局屏幕旋转设置(仅 APP/微信/QQ小程序),支持 auto / portrait / landscape
		"rpxCalcMaxDeviceWidth": 960,
		"rpxCalcBaseDeviceWidth": 375,
		"rpxCalcIncludeWidth": 750
	},
	"tabBar": {
		"color": "#7A7E83",
		"selectedColor": "#3cc51f",
		"borderStyle": "black",
		"backgroundColor": "#ffffff",
		"height": "50px",
		"fontSize": "10px",
		"iconWidth": "24px",
		"spacing": "3px",
    	"iconfontSrc":"static/iconfont.ttf", // app tabbar 字体.ttf文件路径 app 3.4.4+
		"list": [{
			"pagePath": "pages/component/index",
			"iconPath": "static/image/icon_component.png",
			"selectedIconPath": "static/image/icon_component_HL.png",
			"text": "组件",
      		"iconfont": { // 优先级高于 iconPath,该属性依赖 tabbar 根节点的 iconfontSrc
       			"text": "\ue102",
        		"selectedText": "\ue103",
        		"fontSize": "17px",
        		"color": "#000000",
        		"selectedColor": "#0000ff"
      		}
		}, {
			"pagePath": "pages/API/index",
			"iconPath": "static/image/icon_API.png",
			"selectedIconPath": "static/image/icon_API_HL.png",
			"text": "接口"
		}],
		"midButton": {
			"width": "80px",
			"height": "50px",
			"text": "文字",
			"iconPath": "static/image/midButton_iconPath.png",
			"iconWidth": "24px",
			"backgroundImage": "static/image/midButton_backgroundImage.png"
		}
	},
  "easycom": {
    "autoscan": true, //是否自动扫描组件
    "custom": {//自定义扫描规则
      "^uni-(.*)": "@/components/uni-$1.vue"
    }
  },
  "topWindow": {
    "path": "responsive/top-window.vue",
    "style": {
      "height": "44px"
    }
  },
  "leftWindow": {
    "path": "responsive/left-window.vue",
    "style": {
      "width": "300px"
    }
  },
  "rightWindow": {
    "path": "responsive/right-window.vue",
    "style": {
      "width": "300px"
    },
    "matchMedia": {
      "minWidth": 768
    }
  }
}

复制代码

globalStyle

用于设置应用的状态栏、导航条、标题、窗口背景色等。

属性类型默认值描述平台差异说明
navigationBarBackgroundColorHexColor#F8F8F8导航栏背景颜色(同状态栏背景色)APP与H5为#F8F8F8,小程序平台请参考相应小程序文档
navigationBarTextStyleStringblack导航栏标题颜色及状态栏前景颜色,仅支持 black/white支付宝小程序不支持,请使用 my.setNavigationBar
navigationBarTitleTextString导航栏标题文字内容
navigationStyleStringdefault导航栏样式,仅支持 default/custom。custom即取消默认的原生导航栏,需看使用注意微信小程序 7.0+、百度小程序、H5、App(2.0.3+)
backgroundColorHexColor#ffffff下拉显示出来的窗口的背景色微信小程序
backgroundTextStyleStringdark下拉 loading 的样式,仅支持 dark / light微信小程序
enablePullDownRefreshBooleanfalse是否开启下拉刷新,详见页面生命周期
onReachBottomDistanceNumber50页面上拉触底事件触发时距页面底部距离,单位只支持px,详见页面生命周期
backgroundColorTopHexColor#ffffff顶部窗口的背景色(bounce回弹区域)仅 iOS 平台
backgroundColorBottomHexColor#ffffff底部窗口的背景色(bounce回弹区域)仅 iOS 平台
titleImageString导航栏图片地址(替换当前文字标题),支付宝小程序内必须使用https的图片链接地址支付宝小程序、H5、APP
transparentTitleStringnone导航栏整体(前景、背景)透明设置。支持 always 一直透明 / auto 滑动自适应 / none 不透明支付宝小程序、H5、APP
titlePenetrateStringNO导航栏点击穿透支付宝小程序、H5
pageOrientationStringportrait横屏配置,屏幕旋转设置,仅支持 auto / portrait / landscape 详见 响应显示区域变化App 2.4.7+、微信小程序、QQ小程序
animationTypeStringpop-in窗口显示的动画效果,详见:窗口动画App
animationDurationNumber300窗口显示动画的持续时间,单位为 msApp
app-plusObject设置编译到 App 平台的特定样式,配置项参考下方 app-plusApp
h5Object设置编译到 H5 平台的特定样式,配置项参考下方 H5H5
mp-alipayObject设置编译到 mp-alipay 平台的特定样式,配置项参考下方 MP-ALIPAY支付宝小程序
mp-weixinObject设置编译到 mp-weixin 平台的特定样式,配置项参考下方 MP-WEIXIN微信小程序
mp-baiduObject设置编译到 mp-baidu 平台的特定样式,配置项参考下方 MP-BAIDU百度小程序
mp-toutiaoObject设置编译到 mp-toutiao 平台的特定样式抖音小程序
mp-larkObject设置编译到 mp-lark 平台的特定样式飞书小程序
mp-qqObject设置编译到 mp-qq 平台的特定样式QQ小程序
mp-kuaishouObject设置编译到 mp-kuaishou 平台的特定样式快手小程序
mp-jdObject设置编译到 mp-jd 平台的特定样式京东小程序
usingComponentsObject引用小程序组件,参考 小程序组件
renderingModeString同层渲染,webrtc(实时音视频) 无法正常时尝试配置 seperated 强制关掉同层微信小程序
leftWindowBooleantrue当存在 leftWindow 时,默认是否显示 leftWindowH5
topWindowBooleantrue当存在 topWindow 时,默认是否显示 topWindowH5
rightWindowBooleantrue当存在 rightWindow 时,默认是否显示 rightWindowH5
rpxCalcMaxDeviceWidthNumber960rpx 计算所支持的最大设备宽度,单位 pxApp(vue2 且不含 nvue)、H5(2.8.12+)
rpxCalcBaseDeviceWidthNumber375rpx 计算使用的基准设备宽度,设备实际宽度超出 rpx 计算所支持的最大设备宽度时将按基准宽度计算,单位 pxApp(vue2 且不含 nvue)、H5(2.8.12+)
rpxCalcIncludeWidthNumber750rpx 计算特殊处理的值,始终按实际的设备宽度计算,单位 rpxApp(vue2 且不含 nvue)、H5(2.8.12+)
dynamicRpxBooleanfalse动态 rpx,屏幕大小变化会重新渲染 rpxApp-nvue(vue3 固定值为 true) 3.2.13+
maxWidthNumber单位px,当浏览器可见区域宽度大于maxWidth时,两侧留白,当小于等于maxWidth时,页面铺满;不同页面支持配置不同的maxWidth;maxWidth = leftWindow(可选)+page(页面主体)+rightWindow(可选)H5(2.9.9+)

注意

  • 支付宝小程序使用titleImage时必须使用https的图片链接地址,需要真机调试才能看到效果,支付宝开发者工具内无效果
  • globalStyle中设置的titleImage也会覆盖掉pages->style内的设置文字标题
  • 使用 maxWidth 时,页面内fixed元素需要使用–window-left,–window-right来保证布局位置正确
  • dynamicRpx vue3 nvue页面已移除此配置,升级为横竖屏切换自动rpx,如果不需要可以使用 px

topWindow

uni-app 2.9+ 新增 leftWindow, topWindow, rightWindow 配置。用于解决宽屏适配问题。

以现有的手机应用为mainWindow,在左、上、右,可以追加新的页面显示窗体。

整体的宽屏适配思路,参考单独的宽屏适配指南

属性类型默认值描述
pathString配置页面路径
styleObject配置页面窗口表现,配置项参考下方 pageStyle
matchMediaObject配置显示该窗口的规则,配置项参考下方 matchMedia

注意

  • 目前 style 节点仅支持配置 width,height 等 css 样式相关属性

  • 如果需求当存在 topwindow 时,自动隐藏页面的 navigationBar,根据需求不同在

    App.vue
    

    中配置如下 css:

    • 只需要隐藏某个的页面 navigationBar

      /* 隐藏路径为 pages/component/view/view 页面的 navigationBar */
      .uni-app--showtopwindow [data-page="pages/component/view/view"] uni-page-head {
      	display: none;
      }
      

      复制代码

    • 需要隐藏大部分页面的 navigationBar,显示某个页面的 navigationBar

      /* 隐藏所有页面的 navigationBar */
      .uni-app--showtopwindow uni-page-head {
      	display: none;
      }
      
      /* 显示路径为 pages/component/view/view 页面的 navigationBar */
      .uni-app--showtopwindow [data-page="pages/component/view/view"] uni-page-head {
      	display: block;
      }
      

      复制代码

matchMedia
属性类型默认值描述
minWidthNumber768当设备可见区域宽度 >= minWidth 时,显示该 window

通过matchMedia的调节,可以自适应在不同屏幕上显示指定的window。

{
  "pages": [
    {
      "path": "pages/login/login",
      "style": {
        "topWindow": false // 当前页面不显示 topWindow
        "leftWindow": false // 当前页面不显示 leftWindow
        "rightWindow": false // 当前页面不显示 rightWindow
      }
    }
  ],
  "topWindow": {
    "path": "responsive/top-window.vue", // 指定 topWindow 页面文件
    "style": {
      "height": "44px"
    }
  },
  "leftWindow": {
    "path": "responsive/left-window.vue", // 指定 leftWindow 页面文件
    "style": {
      "width": "300px"
    }
  },
  "rightWindow": {
    "path": "responsive/right-window.vue", // 指定 rightWindow 页面文件
    "style": {
      "width": "300px" // 页面宽度
    },
    "matchMedia": {
      "minWidth": 768 //生效条件,当窗口宽度大于768px时显示
    }
  }
}

复制代码

案例演示:HBuilderX 2.9.9+,新建项目选择hello uni-app或新闻模板,或直接浏览:https://hellouniapp.dcloud.net.cn/

leftWindow

topWindow相同

ightWindow

topWindow相同

窗口通信参考:https://uniapp.dcloud.net.cn/api/window/communication

pages

uni-app 通过 pages 节点配置应用由哪些页面组成,pages 节点接收一个数组,数组每个项都是一个对象,其属性值如下:

属性类型默认值描述
pathString配置页面路径
styleObject配置页面窗口表现,配置项参考下方 pageStyle
needLoginBooleanfalse是否需要登录才可访问

Tips:

  • pages节点的第一项为应用入口页(即首页)
  • 应用中新增/减少页面,都需要对 pages 数组进行修改
  • 文件名不需要写后缀,框架会自动寻找路径下的页面资源

代码示例:

开发目录为:

┌─pages
│  ├─index
│  │  └─index.vue
│  └─login
│     └─login.vue
├─static
├─main.js
├─App.vue
├─manifest.json
└─pages.json
	

则需要在 pages.json 中填写

{
    "pages": [
        {
            "path": "pages/index/index",
            "style": { ... }
        }, {
            "path": "pages/login/login",
            "style": { ... }
        }
    ]
}

复制代码

style

用于设置每个页面的状态栏、导航条、标题、窗口背景色等。

页面中配置项会覆盖 globalStyle 中相同的配置项

属性类型默认值描述平台差异说明
navigationBarBackgroundColorHexColor#F8F8F8导航栏背景颜色(同状态栏背景色)APP与H5为#F8F8F8,小程序平台请参考相应小程序文档
navigationBarTextStyleStringblack导航栏标题颜色及状态栏前景颜色,仅支持 black/white
navigationBarTitleTextString导航栏标题文字内容
navigationBarShadowObject导航栏阴影,配置参考下方 导航栏阴影
navigationStyleStringdefault导航栏样式,仅支持 default/custom。custom即取消默认的原生导航栏,需看使用注意微信小程序 7.0+、百度小程序、H5、App(2.0.3+)
disableScrollBooleanfalse设置为 true 则页面整体不能上下滚动(bounce效果),只在页面配置中有效,在globalStyle中设置无效微信小程序(iOS)、百度小程序(iOS)
backgroundColorHexColor#ffffff窗口的背景色微信小程序、百度小程序、抖音小程序、飞书小程序、京东小程序
backgroundTextStyleStringdark下拉 loading 的样式,仅支持 dark/light
enablePullDownRefreshBooleanfalse是否开启下拉刷新,详见页面生命周期
onReachBottomDistanceNumber50页面上拉触底事件触发时距页面底部距离,单位只支持px,详见页面生命周期
backgroundColorTopHexColor#ffffff顶部窗口的背景色(bounce回弹区域)仅 iOS 平台
backgroundColorBottomHexColor#ffffff底部窗口的背景色(bounce回弹区域)仅 iOS 平台
disableSwipeBackBooleanfalse是否禁用滑动返回App-iOS(3.4.0+)
titleImageString导航栏图片地址(替换当前文字标题),支付宝小程序内必须使用https的图片链接地址支付宝小程序、H5、App
transparentTitleStringnone导航栏透明设置。支持 always 一直透明 / auto 滑动自适应 / none 不透明支付宝小程序、H5、APP
titlePenetrateStringNO导航栏点击穿透支付宝小程序、H5
app-plusObject设置编译到 App 平台的特定样式,配置项参考下方 app-plusApp
h5Object设置编译到 H5 平台的特定样式,配置项参考下方 H5H5
mp-alipayObject设置编译到 mp-alipay 平台的特定样式,配置项参考下方 MP-ALIPAY支付宝小程序
mp-weixinObject设置编译到 mp-weixin 平台的特定样式微信小程序
mp-baiduObject设置编译到 mp-baidu 平台的特定样式百度小程序
mp-toutiaoObject设置编译到 mp-toutiao 平台的特定样式抖音小程序
mp-larkObject设置编译到 mp-lark 平台的特定样式飞书小程序
mp-qqObject设置编译到 mp-qq 平台的特定样式QQ小程序
mp-kuaishouObject设置编译到 mp-kuaishou 平台的特定样式快手小程序
mp-jdObject设置编译到 mp-jd 平台的特定样式京东小程序
usingComponentsObject引用小程序组件,参考 小程序组件App、微信小程序、支付宝小程序、百度小程序、京东小程序
leftWindowBooleantrue当存在 leftWindow时,当前页面是否显示 leftWindowH5
topWindowBooleantrue当存在 topWindow 时,当前页面是否显示 topWindowH5
rightWindowBooleantrue当存在 rightWindow时,当前页面是否显示 rightWindowH5
maxWidthNumber单位px,当浏览器可见区域宽度大于maxWidth时,两侧留白,当小于等于maxWidth时,页面铺满;不同页面支持配置不同的maxWidth;maxWidth = leftWindow(可选)+page(页面主体)+rightWindow(可选)H5(2.9.9+)

注意

  • 使用 maxWidth 时,页面内fixed元素需要使用–window-left,–window-right来保证布局位置正确

代码示例:

{
  "pages": [{
      "path": "pages/index/index",
      "style": {
        "navigationBarTitleText": "首页",//设置页面标题文字
        "enablePullDownRefresh":true//开启下拉刷新
      }
    },
    ...
  ]
}

复制代码

注意

  • 支付宝小程序使用titleImage时必须使用https的图片链接地址,需要真机调试才能看到效果,支付宝开发者工具内无效果

自定义导航栏使用注意

当navigationStyle设为custom或titleNView设为false时,原生导航栏不显示,此时要注意几个问题:

  • 非H5端,手机顶部状态栏区域会被页面内容覆盖。这是因为窗体是沉浸式的原因,即全屏可写内容。uni-app提供了状态栏高度的css变量–status-bar-height,如果需要把状态栏的位置从前景部分让出来,可写一个占位div,高度设为css变量。
<template>
    <view>
        <view class="status_bar">
            <!-- 这里是状态栏 -->
        </view>
        <view> 状态栏下的文字 </view>
    </view>
</template>
<style>
    .status_bar {
        height: var(--status-bar-height);
        width: 100%;
    }
</style>

复制代码

  • 如果原生导航栏不能满足需求,推荐使用uni ui的自定义导航栏NavBar。这个前端导航栏自动处理了状态栏高度占位问题。

  • 前端导航栏搭配原生下拉刷新时,会有问题,包括

    • 微信小程序下iOS需要拉更长才能看到下拉刷新的三个点,而Android是从屏幕顶部下拉,无法从导航栏下方下拉。如果一定要从前端导航栏下拉,小程序下只能放弃原生下拉刷新,纯前端模拟,参考mescroll插件,但这样很容易产生性能问题。目前小程序平台自身没有提供更好的方案
    • App和H5下,原生下拉刷新提供了circle样式,可以指定offset偏移量(pages.json的app-plus下配置),自定义下拉圈出现的位置。在hello uni-app的扩展组件中有示例。
  • 非H5端,前端导航盖不住原生组件。如果页面有video、map、textarea(仅小程序)等

    原生组件

    ,滚动时会覆盖住导航栏

    • 如果是小程序下,可以使用cover-view来做导航栏,避免覆盖问题
    • 如果是App下,建议使用titleNViewsubNVue,体验更好
  • 前端组件在渲染速度上不如原生导航栏,原生导航可以在动画期间渲染,保证动画期间不白屏,但使用前端导航栏,在新窗体进入的动画期间可能会整页白屏,越低端的手机越明显。

  • 以上讨论的是前端自定义导航栏,但在App侧,原生导航栏也提供了比小程序导航更丰富的自定义性

    • titleNView:给原生导航栏提供更多配置,包括自定义按钮、滚动渐变效果、搜索框等,详见titleNView
    • subNVue:使用nvue原生渲染,所有布局自己开发,具备一切自定义灵活度。详见subNVue
  • 页面禁用原生导航栏后,想要改变状态栏的前景字体样式,仍可设置页面的 navigationBarTextStyle 属性(只能设置为 black或white)。如果想单独设置状态栏颜色,App端可使用plus.navigator.setStatusBarStyle设置。注意部分低端Android手机(4.4)自身不支持设置状态栏前景色。

鉴于以上问题,在原生导航能解决业务需求的情况下,尽量使用原生导航。甚至有时需要牺牲一些不是很重要的需求。在App和H5下,uni-app提供了灵活的处理方案:titleNViewsubNVue、或整页使用nvue。但在小程序下,因为其自身的限制,没有太好的方案。有必要的话,也可以用条件编译分端处理。

app-plus

配置编译到 App 平台时的特定样式,部分常用配置 H5 平台也支持。以下仅列出常用,更多配置项参考 WebviewStyles

属性类型默认值描述平台兼容
backgroundHexColor#FFFFFF窗体背景色。无论vue页面还是nvue页面,在App上都有一个父级原生窗体,该窗体的背景色生效时间快于页面里的css生效时间App (vue 页面需要将 body 背景色设为透明)
titleNViewObject导航栏 ,详见:导航栏App、H5
subNVuesArray原生子窗体,详见:原生子窗体App 1.9.10+
bounceString页面回弹效果,设置为 “none” 时关闭效果。App-vue(nvue Android无页面级bounce效果,仅list、recycle-list、waterfall等滚动组件有bounce效果)
popGestureStringclose侧滑返回功能,可选值:“close”(启用侧滑返回)、“none”(禁用侧滑返回)App-iOS
softinputNavBarStringautoiOS软键盘上完成工具栏的显示模式,设置为 “none” 时关闭工具栏。App-iOS
softinputModeStringadjustPan软键盘弹出模式,支持 adjustResize、adjustPan 两种模式App
pullToRefreshObject下拉刷新App
scrollIndicatorString滚动条显示策略,设置为 “none” 时不显示滚动条。App
animationTypeStringpop-in窗口显示的动画效果,详见:窗口动画App
animationDurationNumber300窗口显示动画的持续时间,单位为 ms。App

Tips

  • .nvue 页面仅支持 titleNView、pullToRefresh、scrollIndicator 配置,其它配置项暂不支持
导航栏
属性类型默认值描述版本兼容性
backgroundColorString#F7F7F7背景颜色,支持16进制颜色或RGBA颜色。App端仅悬浮导航栏支持RGBA颜色
buttonsArray自定义按钮,详见 buttons纯nvue即render:native时暂不支持
titleColorString#000000标题文字颜色
titleOverflowStringellipsis标题文字超出显示区域时处理方式。“clip”-超出显示区域时内容裁剪;“ellipsis”-超出显示区域时尾部显示省略标记(…)。
titleTextString标题文字内容
titleSizeString标题文字字体大小
typeStringdefault导航栏样式。“default”-默认样式;“transparent”-滚动透明渐变;“float”-悬浮导航栏。App-nvue 2.4.4+ 支持、App-vue、H5
tagsArray原生 View 增强,详见:5+ View 控件
searchInputObject原生导航栏上的搜索框配置,详见:searchInput1.6.0
homeButtonBooleanfalse标题栏控件是否显示Home按钮
autoBackButtonBooleantrue标题栏控件是否显示左侧返回按钮App 2.6.3+
backButtonObject返回按钮的样式,详见:backButtonApp 2.6.3
backgroundImageString支持以下类型: 背景图片路径 - 如"/static/img.png",仅支持本地文件绝对路径,根据实际标题栏宽高拉伸绘制; 渐变色 - 仅支持线性渐变,两种颜色的渐变,如“linear-gradient(to top, #a80077, #66ff00)”, 其中第一个参数为渐变方向,可取值: "to right"表示从左向右渐变, “to left"表示从右向左渐变, “to bottom"表示从上到下渐变, “to top"表示从下到上渐变, “to bottom right"表示从左上角到右下角, “to top left"表示从右下角到左上角2.6.3
backgroundRepeatString仅在backgroundImage设置为图片路径时有效。 可取值: “repeat” - 背景图片在垂直方向和水平方向平铺; “repeat-x” - 背景图片在水平方向平铺,垂直方向拉伸; “repeat-y” - 背景图片在垂直方向平铺,水平方向拉伸; “no-repeat” - 背景图片在垂直方向和水平方向都拉伸。 默认使用 “no-repeat"2.6.3
titleAlignString“auto”可取值: “center”-居中对齐; “left”-居左对齐; “auto”-根据平台自动选择(Android平台居左对齐,iOS平台居中对齐)2.6.3
blurEffectString“none”此效果将会高斯模糊显示标题栏后的内容,仅在type为"transparent"或"float"时有效。 可取值: “dark” - 暗风格模糊,对应iOS原生UIBlurEffectStyleDark效果; “extralight” - 高亮风格模糊,对应iOS原生UIBlurEffectStyleExtraLight效果; “light” - 亮风格模糊,对应iOS原生UIBlurEffectStyleLight效果; “none” - 无模糊效果。 注意:使用模糊效果时应避免设置背景颜色,设置背景颜色可能覆盖模糊效果。2.4.3
coverageString“132px”标题栏控件变化作用范围,仅在type值为transparent时有效,页面滚动时标题栏背景透明度将发生变化。 当页面滚动到指定偏移量时标题栏背景变为完全不透明。 支持百分比、像素值
splitLineBooleanfalse标题栏的底部分割线(SplitLineStyles),设置此属性则在标题栏控件的底部显示分割线,可配置颜色值及高度。 设置此属性值为undefined或null则隐藏分割线。 默认不显示底部分割线2.6.6
subtitleColorString副标题文字颜色,颜色值格式为"#RRGGBB"和"rgba(R,G,B,A)“,如”#FF0000"表示标题文字颜色为红色。 默认值与主标题文字颜色一致2.6.6
subtitleSizeString“auto”副标题文字字体大小,字体大小单位为像素,如"14px"表示字体大小为14像素,auto表示自动计算,约为12像素。2.6.6
subtitleOverflowString“ellipsis”标题文字超出显示区域时处理方式,可取值: “clip” - 超出显示区域时内容裁剪; “ellipsis” - 超出显示区域时尾部显示省略标记(…)。2.6.6
subtitleTextString副标题文字内容,设置副标后将显示两行标题,副标显示在主标题(titleText)下方。 注意:设置副标题后将居左显示2.6.6
titleIconString标题图标,图标路径如"./img/t.png",仅支持本地文件路径, 相对路径,相对于当前页面的host位置,固定宽高为逻辑像素值"34px"。 要求图片的宽高相同。 注意:设置标题图标后标题将居左显示。2.6.6
titleIconRadiusString无圆角标题图标圆角,取值格式为"XXpx",其中XX为像素值(逻辑像素),如"10px"表示10像素半径圆角。2.6.6
SplitLineStyles
属性类型默认值描述版本兼容性
colorString#CCCCCC底部分割线颜色,可取值: “#RRGGBB"格式字符串,如”#FF0000"表示绘制红色分割线; “rgba(R,G,B,A)”,其中R/G/B分别代表红色值/绿色值/蓝色值,正整数类型,取值范围为0-255,A为透明度,浮点数类型,取值范围为0-1(0为全透明,1为不透明),如"rgba(255,0,0,0.5)",表示红色半透明
heightString“1px”可取值:像素值(逻辑像素),支持小数点,如"1px"表示1像素高;百分比,如"1%",相对于标题栏控件的高度。

Tips

  • 页面支持通过配置 navigationStyle为custom,或titleNView为false,来禁用原生导航栏。一旦禁用原生导航,请注意阅读自定义导航注意事项
  • titleNViewtype 值为 transparent 时,导航栏为滚动透明渐变导航栏,默认只有button,滚动后标题栏底色和title文字会渐变出现,不支持自定义按钮设置color属性; typefloat 时,导航栏为悬浮标题栏,此时页面内容上顶到了屏幕顶部,包括状态栏,但导航栏悬浮盖在页面上方,一般这种场景会同时设置导航栏的背景色为rgba半透明颜色。
  • titleNViewtype 值为 transparent 时,App-nvue 2.4.4+ 支持
  • titleNView 配置 buttons 后,监听按钮的点击事件,vue 页面及 nvue 的weex编译模式参考:uni.onNavigationBarButtonTap
  • titleNView 配置 searchInput 后,相关的事件监听参考:onNavigationBarSearchInputChanged 等
  • 可通过 [<navigation-bar>(/component/navigation-bar)] 配置
  • App下原生导航栏的按钮如果使用字体图标,注意检查字体库的名字(font-family)是否使用了默认的 iconfont,这个名字是保留字,不能作为外部引入的字体库的名字,需要调整为自定义的名称,否则无法显示。
  • 想了解各种导航栏的开发方法,请详读导航栏开发指南
自定义按钮
属性类型默认值描述
typeStringnone按钮样式,可取值见:buttons 样式
colorString默认与标题文字颜色一致按钮上文字颜色
backgroundString默认值为灰色半透明按钮的背景颜色,仅在标题栏type=transparent时生效
colorPressedString默认值为 color 属性值自动调整透明度为 0.3按下状态按钮文字颜色
floatStringright按钮在标题栏上的显示位置,可取值"left"、“right”
fontWeightStringnormal按钮上文字的粗细。可取值"normal"-标准字体、“bold”-加粗字体。
fontSizeString按钮上文字大小
fontSrcString按钮上文字使用的字体文件路径。不支持网络地址,请统一使用本地地址。
selectBooleanfalse是否显示选择指示图标(向下箭头),常用于城市选择
textString按钮上显示的文字。使用字体图标时 unicode 字符表示必须 ‘\u’ 开头,如 “\ue123”(注意不能写成"\e123")。
widthString44px按钮的宽度,可取值: “*px” - 逻辑像素值,如"10px"表示10逻辑像素值,不支持rpx。按钮的内容居中显示; “auto” - 自定计算宽度,根据内容自动调整按钮宽度
自定义返回按钮的样式

当autoBackButton设置为true时生效。 通过此属性可自定义返回按钮样式,如图标大小、红点、角标、标题等。

HBuilderX 2.6.3+ 支持

属性类型默认值描述
backgroundStringnone背景颜色,仅在标题栏type=transparent时生效,当标题栏透明时按钮显示的背景颜色。 可取值#RRGGBB和rgba格式颜色字符串,默认值为灰色半透明。
badgeTextString角标文本,最多显示3个字符,超过则显示为…
colorString窗口标题栏控件的标题文字颜色。图标和标题颜色,可取值: “#RRGGBB"格式字符串,如”#FF0000"表示红色; “rgba(R,G,B,A)”,其中R/G/B分别代表红色值/绿色值/蓝色值,正整数类型,取值范围为0-255,A为透明度,浮点数类型,取值范围为0-1(0为全透明,1为不透明),如"rgba(255,0,0,0.5)",表示红色半透明。
colorPressedString按下状态按钮文字颜色,可取值: “#RRGGBB"格式字符串,如”#FF0000"表示红色; “rgba(R,G,B,A)”,其中R/G/B分别代表红色值/绿色值/蓝色值,正整数类型,取值范围为0-255,A为透明度,浮点数类型,取值范围为0-1(0为全透明,1为不透明),如"rgba(255,0,0,0.5)",表示红色半透明。 默认值为color属性值自动调整透明度为0.3。
fontWeightString“normal”返回图标的粗细,可取值: “normal” - 标准字体; “bold” - 加粗字体。
fontSizeString返回图标文字大小,可取值:字体高度像素值,数字加"px"格式字符串,如"22px"。 窗口标题栏为透明样式(type=“transparent”)时,默认值为"22px"; 窗口标题栏为默认样式(type=“default”)时,默认值为"27px"。
redDotBooleanfalse是否显示红点,设置为true则显示红点,false则不显示红点。默认值为false。 注意:当设置了角标文本时红点不显示。
titleString返回按钮上的标题,显示在返回图标(字体图标)后,默认为空字符串。
titleWeightString“normal”返回按钮上标题的粗细,可取值: “normal” - 标准字体; “bold” - 加粗字体。
按钮样式

使用 type 值设置按钮的样式时,会忽略 fontSrc 和 text 属性。

说明
forward前进按钮
back后退按钮
share分享按钮
favorite收藏按钮
home主页按钮
menu菜单按钮
close关闭按钮
none无样式,需通过 text 属性设置按钮上显示的内容、通过 fontSrc 属性设置使用的字体库。
搜索框配置

searchInput可以在titleNView的原生导航栏上放置搜索框。其宽度根据周围元素自适应。

属性类型默认值描述
autoFocusBooleanfalse是否自动获取焦点
alignStringcenter非输入状态下文本的对齐方式。可取值: “left” - 居左对齐; “right” - 居右对齐; “center” - 居中对齐。
backgroundColorStringrgba(255,255,255,0.5)背景颜色
borderRadiusString0px输入框的圆角半径,取值格式为"XXpx",其中XX为像素值(逻辑像素),不支持rpx。
placeholderString提示文本。
placeholderColorString#CCCCCC提示文本颜色
disabledBooleanfalse是否可输入

searchInput Tips

searchInput的点击输入框onNavigationBarSearchInputClicked、文本变化onNavigationBarSearchInputChanged、点击搜索按钮onNavigationBarSearchInputConfirmed等生命周期,见文档页面生命周期

  • 在生命周期里通过参数e.text,可获取输入框内容。具体见hello uni-app中模板-顶部导航栏中的示例
  • 如需动态修改searchInput,或者获取searchInput的placehold,参考uni-app动态修改App端导航栏

常见titleNView配置代码示例

以下列出部分配置。关于全面的导航栏配置,这里有一个完善的演示工程,演示了导航栏的各种效果,详见:https://ext.dcloud.net.cn/plugin?id=1765

{
	"pages": [{
			"path": "pages/index/index", //首页
			"style": {
				"app-plus": {
					"titleNView": false //禁用原生导航栏
				}
			}
		}, {
			"path": "pages/log/log", //日志页面
			"style": {
				"app-plus": {
					"bounce": "none", //关闭窗口回弹效果
					"titleNView": {
						"buttons": [ //原生标题栏按钮配置,
							{
								"text": "分享" //原生标题栏增加分享按钮,点击事件可通过页面的 onNavigationBarButtonTap 函数进行监听
							}
						],
						"backButton": { //自定义 backButton
							"background": "#00FF00"
						}
					}
				}
			}
		}, {
			"path": "pages/detail/detail", //详情页面
			"style": {
				"navigationBarTitleText": "详情",
				"app-plus": {
					"titleNView": {
						"type": "transparent"//透明渐变导航栏 App-nvue 2.4.4+ 支持
					}
				}
			}
		}, {
			"path": "pages/search/search", //搜索页面
			"style": {
				"app-plus": {
					"titleNView": {
						"type": "transparent",//透明渐变导航栏 App-nvue 2.4.4+ 支持
						"searchInput": {
							"backgroundColor": "#fff",
							"borderRadius": "6px", //输入框圆角
							"placeholder": "请输入搜索内容",
							"disabled": true //disable时点击输入框不置焦,可以跳到新页面搜索
						}
					}
				}
			}
		}
		...
	]
}

复制代码

Tips

  • buttons 的 text 推荐使用字体图标。如果使用了汉字或英文,推荐把字体改小一点,否则文字长度增加后,距离屏幕右边距太近。
  • App平台,buttons动态修改,详见
  • App平台,buttons角标动态修改,见 hello uni-app 中模板-顶部导航标题栏-导航栏带红点和角标
  • App平台,设置searchInput的文字动态修改,API见文档。注意disable状态无法设置文字、placehold暂不支持动态设置
  • H5平台,如需实现上述动态修改,需在条件编译通过dom操作修改
原生子窗体

subNVues 是 vue 页面的原生子窗体。用于解决App中 vue 页面中的层级覆盖和原生界面灵活自定义用的。

它不是全屏页面,也不是组件,就是一个原生子窗体。它是一个 nvue 页面,使用 weex 引擎渲染,提供了比 cover-view、plus.nativeObj.view 更强大的原生排版能力,方便自定义原生导航或覆盖原生地图、视频等。请详读subNVues 开发指南

subNVue 也可以在 nvue 页面中使用。但目前在纯nvue下(render为native)还不支持。

属性类型描述
idStringsubNVue 原生子窗体的标识
pathString配置 nvue 文件路径,nvue 文件需放置到使用 subNvue 的页面文件目录下
typeString原生子窗口内置样式,可取值:‘popup’,弹出层;“navigationBar”,导航栏
styleObjectsubNVue 原生子窗体的样式,配置项参考下方 subNVuesStyle

Tips

  • subNVuesid 是全局唯一的,不能重复
  • 可以通过 uni.getSubNVueById(‘id’) 获取 subNVues 的实例
  • subNVuespath 属性只能是 nvue 文件路径
原生子窗体样式
属性类型默认值描述
positionStringabsolute原生子窗体的排版位置,排版位置决定原生子窗体在父窗口中的定位方式。 可取值:“static”,原生子窗体在页面中正常定位,如果页面存在滚动条则随窗口内容滚动;“absolute”,原生子窗体在页面中绝对定位,如果页面存在滚动条不随窗口内容滚动;“dock”,原生子窗体在页面中停靠,停靠的位置由dock属性值决定。 默认值为"absolute"。
dockStringbottom原生子窗体的停靠方式,仅当原生子窗体 “position” 属性值设置为 “dock” 时才生效,可取值:“top”,原生子窗体停靠则页面顶部;“bottom”,原生子窗体停靠在页面底部;“right”,原生子窗体停靠在页面右侧;“left”,原生子窗体停靠在页面左侧。 默认值为"bottom"。
maskHexColorrgba(0,0,0,0.5)原生子窗体的遮罩层,仅当原生子窗体 “type” 属性值设置为 “popup” 时才生效,可取值: rgba格式字符串,定义纯色遮罩层样式,如"rgba(0,0,0,0.5)",表示黑色半透明;
widthString100%原生子窗体的宽度,支持百分比、像素值,默认为100%。未设置width属性值时,可同时设置left和right属性值改变窗口的默认宽度。
heightString100%原生子窗体的高度,支持百分比、像素值,默认为100%。 当未设置height属性值时,优先通过top和bottom属性值来计算原生子窗体的高度。
topString0px原生子窗体垂直向下的偏移量,支持百分比、像素值,默认值为0px。 未设置top属性值时,优先通过bottom和height属性值来计算原生子窗体的top位置。
bottomString原生子窗体垂直向上偏移量,支持百分比、像素值,默认值无值(根据top和height属性值来自动计算)。 当同时设置了top和height值时,忽略此属性值; 当未设置height值时,可通过top和bottom属性值来确定原生子窗体的高度。
leftString0px原生子窗体水平向左的偏移量,支持百分比、像素值,默认值为0px。 未设置left属性值时,优先通过right和width属性值来计算原生子窗体的left位置。
rightString原生子窗体水平向右的偏移量,支持百分比、像素值,默认无值(根据left和width属性值来自动计算)。 当设置了left和width值时,忽略此属性值; 当未设置width值时,可通过left和bottom属性值来确定原生子窗体的宽度。
marginString原生子窗体的边距,用于定位原生子窗体的位置,支持auto,auto表示居中。若设置了left、right、top、bottom则对应的边距值失效。
zindexNumber原生子窗体的窗口的堆叠顺序值,拥有更高堆叠顺序的窗口总是会处于堆叠顺序较低的窗口的前面,拥有相同堆叠顺序的窗口后调用show方法则在前面。
backgroundString#FFFFFF窗口的背景颜色,Android平台4.0以上系统才支持“transparent”背景透明样式。比如subnvue为圆角时需要设置为transparent才能看到正确的效果

代码示例

{
	"pages": [{
		"path": "pages/index/index", //首页
		"style": {
			"app-plus": {
				"titleNView": false , //禁用原生导航栏
				"subNVues":[{//侧滑菜单
					"id": "drawer", //subNVue 的 id,可通过 uni.getSubNVueById('drawer') 获取
					"path": "pages/index/drawer.nvue", // nvue 路径
					"style": { //webview style 子集,文档可暂时开放出来位置,大小相关配置
						"position": "popup", //除 popup 外,其他值域参考 5+ webview position 文档
						"width": "50%"
					}

				}, {//弹出层
					"id": "popup",
					"path": "pages/index/popup",
					"style": {
						"position": "popup",
						"margin":"auto",
						"width": "150px",
						"height": "150px"
					}

				}, {//自定义头
					"id": "nav",
					"path": "pages/index/nav",
					"style": {
						"position": "dock",
						"height": "44px"
					}

				}]
			}
		}
	}]
}

复制代码

下拉刷新

在 App 平台下可以自定义部分下拉刷新的配置 page->style->app-plus->pullToRefresh

属性类型默认值描述
supportBooleanfalse是否开启窗口的下拉刷新功能
colorString#2BD009颜色值格式为"#RRGGBB",仅"circle"样式下拉刷新支持此属性。
styleStringAndroid 平台为 circle;iOS 平台为 default。可取值:“default”——经典下拉刷新样式(下拉拖动时页面内容跟随);“circle”——圆圈样式下拉刷新控件样式(下拉拖动时仅刷新控件跟随)。
heightString窗口的下拉刷新控件进入刷新状态的拉拽高度。支持百分比,如"10%“;像素值,如"50px”,不支持rpx。
rangeString窗口可下拉拖拽的范围。支持百分比,如"10%“;像素值,如"50px”,不支持rpx。
offsetString0px下拉刷新控件的起始位置。仅对"circle"样式下拉刷新控件有效,用于定义刷新控件下拉时的起始位置。支持百分比,如"10%“;像素值,如"50px”,不支持rpx。如使用了非原生title且需要原生下拉刷新,一般都使用circle方式并将offset调至自定义title的高度
contentdownObject目前支持一个属性:caption——在下拉可刷新状态时下拉刷新控件上显示的标题内容。仅对"default"样式下拉刷新控件有效。
contentoverObject目前支持一个属性:caption——在释放可刷新状态时下拉刷新控件上显示的标题内容。仅对"default"样式下拉刷新控件有效。
contentrefreshObject目前支持一个属性:caption——在正在刷新状态时下拉刷新控件上显示的标题内容。仅对"default"样式下拉刷新控件有效。

下拉刷新使用注意

  • enablePullDownRefreshpullToRefresh->support 同时设置时,后者优先级较高。
  • 如果期望在 App 和小程序上均开启下拉刷新的话,请配置页面的 enablePullDownRefresh 属性为 true。
  • 若仅期望在 App 上开启下拉刷新,则不要配置页面的 enablePullDownRefresh 属性,而是配置 pullToRefresh->support 为 true。
  • 开启原生下拉刷新时,页面里不应该使用全屏高的scroll-view,向下拖动内容时,会优先触发下拉刷新而不是scroll-view滚动
  • 原生下拉刷新的起始位置在原生导航栏的下方,如果取消原生导航栏,使用自定义导航栏,原生下拉刷新的位置会在屏幕顶部。如果希望在自定义导航栏下方拉动,只能使用circle方式的下拉刷新,并设置offset参数,将circle圈的起始位置调整到自定义导航栏下方。hello uni-app的扩展组件中有示例。
  • 如果想在app端实现更多复杂的下拉刷新,比如美团、京东App那种拉下一个特殊图形,可以使用nvue的<refresh>组件。HBuilderX 2.0.3+起,新建项目选择新闻模板可以体验
  • 如果想在vue页面通过web前端技术实现下拉刷新,插件市场有例子,但前端下拉刷新的性能不如原生,复杂长列表会很卡
  • iOS上,default模式的下拉刷新和bounce回弹是绑定的,如果设置了bounce:none,会导致无法使用default下拉刷新

代码示例

{
    "pages": [
        {
            "path": "pages/index/index", //首页
            "style": {
                "app-plus": {
                    "pullToRefresh": {
                        "support": true,
                        "color": "#ff3333",
                        "style": "default",
                        "contentdown": {
                            "caption": "下拉可刷新自定义文本"
                        },
                        "contentover": {
                            "caption": "释放可刷新自定义文本"
                        },
                        "contentrefresh": {
                            "caption": "正在刷新自定义文本"
                        }
                    }
                }
            }
        }
    ]
}

复制代码

h5

配置编译到 H5 平台时的特定样式

属性类型描述
titleNViewObject导航栏
pullToRefreshObject下拉刷新
导航栏
属性类型默认值描述最低版本
backgroundColorString#F7F7F7背景颜色,颜色值格式为"#RRGGBB"。
buttonsArray自定义按钮,参考 buttons
titleColorString#000000标题文字颜色
titleTextString标题文字内容
titleSizeString标题文字字体大小
typeStringdefault导航栏样式。“default”-默认样式;“transparent”-透明渐变。
searchInputObject导航栏上的搜索框样式,详见:searchInput1.6.5
自定义按钮
属性类型默认值描述
typeStringnone按钮样式,可取值见:buttons 样式
colorString默认与标题文字颜色一致按钮上文字颜色
backgroundString默认值为灰色半透明按钮的背景颜色,仅在标题栏type=transparent时生效
badgeTextString按钮上显示的角标文本,最多显示3个字符,超过则显示为…
colorPressed(暂不支持)String默认值为 color 属性值自动调整透明度为 0.3按下状态按钮文字颜色
floatStringright按钮在标题栏上的显示位置,可取值"left"、“right”
fontWeightStringnormal按钮上文字的粗细。可取值"normal"-标准字体、“bold”-加粗字体。
fontSizeString按钮上文字大小
fontSrcString按钮上文字使用的字体文件路径。
selectStringfalse是否显示选择指示图标(向下箭头)
textString按钮上显示的文字。使用字体图标时 unicode 字符表示必须 ‘\u’ 开头,如 “\ue123”(注意不能写成"\e123")。
widthString44px按钮的宽度,可取值: “*px” - 逻辑像素值,如"10px"表示10逻辑像素值,不支持rpx,按钮的内容居中显示; “auto” - 自定计算宽度,根据内容自动调整按钮宽度
按钮样式

使用 type 值设置按钮的样式时,会忽略 fontSrc 和 text 属性。

说明
forward前进按钮
back后退按钮
share分享按钮
favorite收藏按钮
home主页按钮
menu菜单按钮
close关闭按钮
none无样式,需通过 text 属性设置按钮上显示的内容、通过 fontSrc 属性设置使用的字体库。
下拉刷新

h5 平台下拉刷新动画,只有 circle 类型。

属性类型默认值描述
colorString#2BD009颜色值格式为"#RRGGBB"
offsetString0px下拉刷新控件的起始位置。支持百分比,如"10%“;像素值,如"50px”,不支持rpx。
搜索框样式
属性类型默认值描述
autoFocusBooleanfalse是否自动获取焦点
alignStringcenter非输入状态下文本的对齐方式。可取值: “left” - 居左对齐; “right” - 居右对齐; “center” - 居中对齐。
backgroundColorStringrgba(255,255,255,0.5)背景颜色
borderRadiusString0px输入框的圆角半径,取值格式为"XXpx",其中XX为像素值(逻辑像素),不支持rpx。
placeholderString提示文本
placeholderColorString#CCCCCC提示文本颜色
disabledBooleanfalse是否可输入

注意事项:

  • 如果 h5 节点没有配置,默认会使用 app-plus 下的配置。
  • 配置了 h5 节点,则会覆盖 app-plus 下的配置。
navigationBarShadow
属性类型描述
colorTypeString阴影的颜色,支持:grey、blue、green、orange、red、yellow

注意事项:

  • 微信/百度/头条 需要配置: “disableScroll”: true
  • 支付宝 “mp-alipay”: { “allowsBounceVertical”: “NO” }

mp-alipay

配置编译到 MP-ALIPAY 平台时的特定样式

属性类型默认值描述
allowsBounceVerticalStringYES是否允许向下拉拽。支持 YES / NO
titleImageString导航栏图片地址(替换当前文字标题),必须使用https的图片链接地址
transparentTitleStringnone导航栏透明设置。支持 always 一直透明 / auto 滑动自适应 / none 不透明
titlePenetrateStringNO导航栏点击穿透
showTitleLoadingStringNO是否进入时显示导航栏的 loading。支持 YES / NO
backgroundImageUrlString下拉露出显示的背景图链接
backgroundImageColorHexColor下拉露出显示的背景图底色
gestureBackStringNOiOS 用,是否支持手势返回。支持 YES / NO
enableScrollBarStringYESAndroid 用,是否显示 WebView 滚动条。支持 YES / NO

注意事项

  • titleImage仅支持https地址,设置了titleImage会替换页面文字标题
  • backgroundImageUrl支持网络地址和本地地址,尽量使用绝对地址
  • 部分配置可能会只在真机运行的时候生效,支付宝未来应该会改善

mp-weixin

配置编译到 MP-WEIXIN 平台时的特定样式

属性类型默认值描述
homeButtonBooleanfalse在非首页、非页面栈最底层页面或非tabbar内页面中的导航栏展示home键
backgroundColorTopHexColor#ffffff顶部窗口的背景色,仅 iOS 支持
backgroundColorBottomHexColor#ffffff底部窗口的背景色,仅 iOS 支持
restartStrategyStringhomePage重新启动策略配置。支持 homePage / homePageAndLatestPage
initialRenderingCacheString页面初始渲染缓存配置。支持 static / dynamic
visualEffectInBackgroundStringnone切入系统后台时,隐藏页面内容,保护用户隐私。支持 hidden / none
handleWebviewPreloadStringstatic控制预加载下个页面的时机。支持 static / manual / auto

mp-baidu

配置编译到 MP-BAIDU 平台时的特定样式

属性类型默认值描述
textSizeAdjustStringauto小程序页面是否禁止响应字体大小的设。支持 auto 默认响应 / none 不响应

FAQ

easycom

HBuilderX 2.5.5起支持easycom组件模式。

传统vue组件,需要安装、引用、注册,三个步骤后才能使用组件。easycom将其精简为一步。

只要组件路径符合规范(具体见下),就可以不用引用、注册,直接在页面中使用。如下:

<template>
	<view class="container">
		<comp-a></comp-a>
		<uni-list>
		</uni-list>
	</view>
</template>
<script>
	// 这里不用import引入,也不需要在components内注册uni-list组件。template里就可以直接用
	export default {
		data() {
			return {}
		}
	}
</script>

复制代码

路径规范指:

  1. 安装在项目根目录的components目录下,并符合components/组件名称/组件名称.vue
  2. 安装在uni_modules下,路径为uni_modules/插件ID/components/组件名称/组件名称.vue

工程目录:

┌─components
│  └─comp-a
│    └─comp-a.vue      符合easycom规范的组件
└─uni_modules          [uni_module](/plugin/uni_modules.md)中符合easycom规范的组件
   └─uni_modules
     └─uni-list
       └─components
         └─uni-list
           └─ uni-list.vue
	

不管components目录下安装了多少组件,easycom打包会自动剔除没有使用的组件,对组件库的使用尤为友好。

组件库批量安装,随意使用,自动按需打包。以官方的uni-ui为例,在HBuilderX新建项目界面选择uni-ui项目模板,只需在页面中敲u,拉出大量组件代码块,直接选择,即可使用。大幅提升开发效率,降低使用门槛。

uni-app插件市场下载符合components/组件名称/组件名称.vue目录结构的组件,均可直接使用。

自定义easycom配置的示例

easycom是自动开启的,不需要手动开启,有需求时可以在pages.jsoneasycom节点进行个性化设置,如关闭自动扫描,或自定义扫描匹配组件的策略。设置参数如下:

属性类型默认值描述
autoscanBooleantrue是否开启自动扫描,开启后将会自动扫描符合components/组件名称/组件名称.vue目录结构的组件
customObject-以正则方式自定义组件匹配规则。如果autoscan不能满足需求,可以使用custom自定义匹配规则

如果你的组件,不符合easycom前述的路径规范。可以在pages.json的easycom节点中自行定义路径规范。

如果需要匹配node_modules内的vue文件,需要使用packageName/path/to/vue-file-$1.vue形式的匹配规则,其中packageName为安装的包名,/path/to/vue-file-$1.vue为vue文件在包内的路径。

"easycom": {
  "autoscan": true,
  "custom": {
    "^uni-(.*)": "@/components/uni-$1.vue", // 匹配components目录内的vue文件
    "^vue-file-(.*)": "packageName/path/to/vue-file-$1.vue" // 匹配node_modules内的vue文件
  }
}

复制代码

说明

  • easycom方式引入的组件无需在页面内import,也不需要在components内声明,即可在任意页面使用。
  • easycom方式引入组件不是全局引入,而是局部引入。例如在H5端只有加载相应页面才会加载使用的组件。
  • 在组件名完全一致的情况下,easycom引入的优先级低于手动引入(区分连字符形式与驼峰形式)。
  • 考虑到编译速度,直接在pages.json内修改easycom不会触发重新编译,需要改动页面内容触发。
  • easycom只处理vue组件,不处理小程序专用组件(如微信的wxml格式组件)。不处理后缀为.nvue的组件。因为nvue页面引入的组件也是.vue组件。可以参考uni ui,使用vue后缀,同时兼容nvue页面。
  • nvue页面里引用.vue后缀的组件,会按照nvue方式使用原生渲染,其中不支持的css会被忽略掉。这种情况同样支持easycom
  • vueuvue 组件优先级,详见

Bug & Tips

  • HBuilderX 3.96 版本以下uni-app x项目,当页面文件名与easycom的组件名一样时,会渲染异常,可以通过调整页面文件名规避该Bug。

tabBar

如果应用是一个多 tab 应用,可以通过 tabBar 配置项指定一级导航栏,以及 tab 切换时显示的对应页。

在 pages.json 中提供 tabBar 配置,不仅仅是为了方便快速开发导航,更重要的是在App和小程序端提升性能。在这两个平台,底层原生引擎在启动时无需等待js引擎初始化,即可直接读取 pages.json 中配置的 tabBar 信息,渲染原生tab。

Tips

  • 当设置 position 为 top 时,将不会显示 icon
  • tabBar 中的 list 是一个数组,只能配置最少2个、最多5个 tab,tab 按数组的顺序排序。
  • tabbar 切换第一次加载时可能渲染不及时,可以在每个tabbar页面的onLoad生命周期里先弹出一个等待雪花(hello uni-app使用了此方式)
  • tabbar 的页面展现过一次后就保留在内存中,再次切换 tabbar 页面,只会触发每个页面的onShow,不会再触发onLoad。
  • 顶部的 tabbar 目前仅微信小程序上支持。需要用到顶部选项卡的话,建议不使用 tabbar 的顶部设置,而是自己做顶部选项卡,可参考 hello uni-app->模板->顶部选项卡。

属性说明:

属性类型必填默认值描述平台差异说明
colorHexColortab 上的文字默认颜色
selectedColorHexColortab 上的文字选中时的颜色
backgroundColorHexColortab 的背景色
borderStyleStringblacktabbar 上边框的颜色,可选值 black/white,也支持其他颜色值App 2.3.4+ 、H5 3.0.0+
blurEffectStringnoneiOS 高斯模糊效果,可选值 dark/extralight/light/none(参考:使用说明App 2.4.0+ 支持、H5 3.0.0+(只有最新版浏览器才支持)
listArraytab 的列表,详见 list 属性说明,最少2个、最多5个 tab
positionStringbottom可选值 bottom、toptop 值仅微信小程序支持
fontSizeString10px文字默认大小App 2.3.4+、H5 3.0.0+
iconWidthString24px图标默认宽度(高度等比例缩放)App 2.3.4+、H5 3.0.0+
spacingString3px图标和文字的间距App 2.3.4+、H5 3.0.0+
heightString50pxtabBar 默认高度App 2.3.4+、H5 3.0.0+
midButtonObject中间按钮 仅在 list 项为偶数时有效App 2.3.4+、H5 3.0.0+
iconfontSrcStringlist设置 iconfont 属性时,需要指定字体文件路径App 3.4.4+、H5 3.5.3+
backgroundImageString设置背景图片,优先级高于 backgroundColorApp
backgroundRepeatString设置标题栏的背景图平铺方式,可取值:“repeat” - 背景图片在垂直方向和水平方向平铺;“repeat-x” - 背景图片在水平方向平铺,垂直方向拉伸;“repeat-y” - 背景图片在垂直方向平铺,水平方向拉伸;“no-repeat” - 背景图片在垂直方向和水平方向都拉伸。 默认使用 “no-repeat”App
redDotColorStringtabbar上红点颜色App

其中 list 接收一个数组,数组中的每个项都是一个对象,其属性值如下:

属性类型必填说明平台差异
pagePathString页面路径,必须在 pages 中先定义
textStringtab 上按钮文字,在 App 和 H5 平台为非必填。例如中间可放一个没有文字的+号图标
iconPathString图片路径,icon 大小限制为40kb,建议尺寸为 81px * 81px,当 position 为 top 时,此参数无效,不支持网络图片,不支持字体图标
selectedIconPathString选中时的图片路径,icon 大小限制为40kb,建议尺寸为 81px * 81px ,当 position 为 top 时,此参数无效
visibleBoolean该项是否显示,默认显示App (3.2.10+)、H5 (3.2.10+)
iconfontObject字体图标,优先级高于 iconPathApp(3.4.4+)、H5 (3.5.3+)

midButton 属性说明

属性类型必填默认值描述
widthString80px中间按钮的宽度,tabBar 其它项为减去此宽度后平分,默认值为与其它项平分宽度
heightString50px中间按钮的高度,可以大于 tabBar 高度,达到中间凸起的效果
textString中间按钮的文字
iconPathString中间按钮的图片路径
iconWidthString24px图片宽度(高度等比例缩放)
backgroundImageString中间按钮的背景图片路径
iconfontObject字体图标,优先级高于 iconPathApp(3.4.4+)

midButton没有pagePath,需监听点击事件,自行处理点击后的行为逻辑。监听点击事件为调用API:uni.onTabBarMidButtonTap,详见https://uniapp.dcloud.io/api/ui/tabbar?id=ontabbarmidbuttontap

iconfont参数说明:

属性类型说明
textString字库 Unicode 码
selectedTextString选中后字库 Unicode 码
fontSizeString字体图标字号(px)
colorString字体图标颜色
selectedColorString字体图标选中颜色
tabbar常见问题
  • tabbar 的 js api 见接口-界面-tabbar,可实现动态显示隐藏(如弹出层无法覆盖tabbar)、内容修改(如国际化)、item加角标等功能。hello uni-app中也有示例。

  • tabbar 的 item 点击事件见页面生命周期的onTabItemTap

  • 代码跳转到 tabbar 页面,api只能使用uni.switchTab,不能使用uni.navigateTo、uni.redirectTo;使用navigator组件跳转时必须设置open-type=“switchTab”

  • tabbar 的默认高度,在不同平台不一样。App端的默认高度在HBuilderX 2.3.4起从56px调整为50px,与H5端统一。开发者也可以自行设定高度,调回56px。详见

  • tabbar 在H5端是div模拟的,属于前端屏幕窗口的一部分,如果要使用bottom居底定位方式,应该使用css变量--window-bottom,比如悬浮在tabbar上方10px的按钮,样式如下bottom: calc(var(--window-bottom) + 10px)

  • 中间带+号的tabbar模板例子,参考。可跨端,但+号不凸起。如需中间凸起,配置tabbar的midButton。

  • 如果是需要先登录、后进入tab页面,不需要把登录页设为首页,首页仍然是tabbar页,可参考云端一体登录模板

  • 前端弹出遮罩层挡不住tabbar的问题,跨端处理方式时动态隐藏tabbar。App端可以使用plus.nativeObj.view或subNVue做弹出和遮罩,可参考这个底部原生图标分享菜单例子

  • 微信小程序模拟器1.02.1904090版有bug,在缩放模拟器页面百分比后,tabbar点击多次后就会卡死。真机无碍,使用时注意。详见

  • PC宽屏上,当页面存在topWindow或leftWindow或rightWindow等多窗体结构时,若想改变 tabbar 显示的位置,请使用 custom-tab-bar组件 配置,若想隐藏 tabbar,可以使用如下 css(好处是可以和 leftwindow 等窗体联动):

      .uni-app--showleftwindow + .uni-tabbar-bottom {
      	display: none;
      }
    

    复制代码

代码示例

"tabBar": {
	"color": "#7A7E83",
	"selectedColor": "#3cc51f",
	"borderStyle": "black",
	"backgroundColor": "#ffffff",
	"list": [{
		"pagePath": "pages/component/index",
		"iconPath": "static/image/icon_component.png",
		"selectedIconPath": "static/image/icon_component_HL.png",
		"text": "组件"
	}, {
		"pagePath": "pages/API/index",
		"iconPath": "static/image/icon_API.png",
		"selectedIconPath": "static/image/icon_API_HL.png",
		"text": "接口"
	}]
}

复制代码

自定义tabbar

原生tabBar是相对固定的配置方式,可能无法满足所有场景,这就涉及到自定义tabBar。

但注意除了H5端,自定义tabBar的性能体验会低于原生tabBar。App和小程序端非必要不要自定义。

  • H5端的自定义tabBar组件:H5端不存在原生tabBar性能更高的概念,并且宽屏下常见的tabBar在顶部而不是底部,此时可以使用 custom-tab-bar组件 来自定义
  • 普通自定义tabBar:使用view自行绘制tabBar。如果页面是多页方式,切换tabBar将无法保持底部tabBar一直显示。所以这种情况建议使用单页方式。单页方式,如果是复杂页面,应用性能会下降明显,需减少页面复杂度。如果是App端,nvue单页的性能会显著高于vue页面
  • 微信小程序自定义tabbar:微信提供一直基于webview自定义tabBar的方案。该功能体验不佳,不太推荐使用。如果要使用,参考微信文档,项目根创建 custom-tab-bar 目录,注意里边的代码是 wxml,wxss,不是 vue,uni-app编译器会直接拷贝该目录到微信小程序中
  • 原生的tabbar有且只有一个且在首页。二级页如需的tab,需自行编写view来实现。一般二级页面更适合的导航是 segement组件

condition

启动模式配置,仅开发期间生效,用于模拟直达页面的场景,如:小程序转发后,用户点击所打开的页面。

属性说明:

属性类型是否必填描述
currentNumber当前激活的模式,list节点的索引值
listArray启动模式列表

list说明:

属性类型是否必填描述
nameString启动模式名称
pathString启动页面路径
queryString启动参数,可在页面的 onLoad 函数里获得

注意: 在 App 里真机运行可直接打开配置的页面,微信开发者工具里需要手动改变编译模式,如下图:

img

代码示例:

"condition": { //模式配置,仅开发期间生效
	"current": 0, //当前激活的模式(list 的索引项)
	"list": [{
			"name": "swiper", //模式名称
			"path": "pages/component/swiper/swiper", //启动页面,必选
			"query": "interval=4000&autoplay=false" //启动参数,在页面的onLoad函数里面得到。
		},
		{
			"name": "test",
			"path": "pages/component/switch/switch"
		}
	]
}

复制代码

subPackages

分包加载配置,此配置为小程序的分包加载机制。

因小程序有体积和资源加载限制,各家小程序平台提供了分包方式,优化小程序的下载和启动速度。

所谓的主包,即放置默认启动页面/TabBar 页面,以及一些所有分包都需用到公共资源/JS 脚本;而分包则是根据pages.json的配置进行划分。

在小程序启动时,默认会下载主包并启动主包内页面,当用户进入分包内某个页面时,会把对应分包自动下载下来,下载完成后再进行展示。此时终端界面会有等待提示。

App默认为整包。从uni-app 2.7.12+ 开始,也兼容了小程序的分包配置。其目的不用于下载提速,而用于首页是vue时的启动提速。App下开启分包,除在pages.json中配置分包规则外,还需要在manifest中设置在app端开启分包设置,详见:https://uniapp.dcloud.io/collocation/manifest?id=app-vue-optimization

subPackages 节点接收一个数组,数组每一项都是应用的子包,其属性值如下:

属性类型是否必填描述
rootString子包的根目录
pagesArray子包由哪些页面组成,参数同 pages

注意:

  • subPackages 里的pages的路径是 root 下的相对路径,不是全路径。

  • 微信小程序每个分包的大小是2M,总体积一共不能超过20M。

  • 百度小程序每个分包的大小是2M,总体积一共不能超过8M。

  • 支付宝小程序每个分包的大小是2M,总体积一共不能超过8M。

  • QQ小程序每个分包的大小是2M,总体积一共不能超过24M。

  • 抖音小程序每个分包的大小是2M,总体积一共不能超过16M(抖音小程序基础库 1.88.0 及以上版本开始支持,抖音小程序开发者工具请使用大于等于 2.0.6 且小于 3.0.0 的版本)。

  • 快手小程序每个分包的大小是2M,总体积一共不能超过24M。

  • 分包下支持独立的 static 目录,用来对静态资源进行分包。

  • uni-app内支持对微信小程序QQ小程序百度小程序支付宝小程序抖音小程序(HBuilderX 3.0.3+)快手小程序分包优化,即将静态资源或者js文件放入分包内不占用主包大小。详情请参考:关于分包优化的说明

  • 针对

    vendor.js
    

    过大的情况可以使用运行时压缩代码

    • HBuilderX创建的项目勾选运行-->运行到小程序模拟器-->运行时是否压缩代码
    • cli创建的项目可以在package.json中添加参数--minimize,示例:"dev:mp-weixin": "cross-env NODE_ENV=development UNI_PLATFORM=mp-weixin vue-cli-service uni-build --watch --minimize"

使用方法:

假设支持分包的 uni-app 目录结构如下:

┌─pages
│  ├─index
│  │  └─index.vue
│  └─login
│     └─login.vue
├─pagesA
│  ├─static
│  └─list
│     └─list.vue
├─pagesB
│  ├─static
│  └─detail
│     └─detail.vue
├─static
├─main.js
├─App.vue
├─manifest.json
└─pages.json
	

则需要在 pages.json 中填写

{
	"pages": [{
		"path": "pages/index/index",
		"style": { ...}
	}, {
		"path": "pages/login/login",
		"style": { ...}
	}],
	"subPackages": [{
		"root": "pagesA",
		"pages": [{
			"path": "list/list",
			"style": { ...}
		}]
	}, {
		"root": "pagesB",
		"pages": [{
			"path": "detail/detail",
			"style": { ...}
		}]
	}],
	"preloadRule": {
		"pagesA/list/list": {
			"network": "all",
			"packages": ["__APP__"]
		},
		"pagesB/detail/detail": {
			"network": "all",
			"packages": ["pagesA"]
		}
	}
}

复制代码

preloadRule

分包预载配置。

配置preloadRule后,在进入小程序某个页面时,由框架自动预下载可能需要的分包,提升进入后续分包页面时的启动速度

preloadRule 中,key 是页面路径,value 是进入此页面的预下载配置,每个配置有以下几项:

字段类型必填默认值说明
packagesStringArray进入页面后预下载分包的 rootname__APP__ 表示主包。
networkStringwifi在指定网络下预下载,可选值为:all(不限网络)、wifi(仅wifi下预下载)

app的分包,同样支持preloadRule,但网络规则无效。

FAQ

  • Q:为什么在pages.json里配置小程序定位权限描述,无法编译到小程序端,运行后一直提示getLocation需要在app.json中声明
  • A:微信小程序的权限描述配置在manifest中,不在pages.json中,具体参考文档:https://uniapp.dcloud.io/collocation/manifest?id=mp-weixin
  • 18
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
这是一款全新的ORM软件(仅依赖一个jar: gson): 如果你觉得hibernate难以驾驭,或许该软件正是你所想要的 如果你觉得mybatis成堆的xml/mapper太繁琐,或许该软件正是你所想要的 如果你觉得现有的ORM软件总有那么些不太满意的地方,或许该软件有你所想要的 如果你觉得将SQL的查询结果映射成Map使用起来不太方便,该软件能帮你自动产生class 如果你觉得配置型SQL语法书写起来不太方便,该软件借用JSP编辑器来书写SQL, IDE能帮助你实现代码提示 如果你想尝试下新的ORM软件, 欢迎来使用! 5分钟演示视频     参考:例子工程 一行代码引入数据库: @DB(url="jdbc:mysql://127.0.0.1:3306/test" ,username="root", password="root") 一些基本的数据库操作: //insert new User().setName("zzg.zhou").setStatus(1).save(); //parse data from type: Map, json/xml string, JsonObject(Gson) //, HttpServletRequest, JavaBean new User().parse("{'name':'oschina','status':0}").save(); new User().parse("<data> <name>china01</name><status>1</status> </data>").save(); //select User.SELECT().selectByPrimaryKey(1); //SQL: SELECT * FROM `user` WHERE `name` = 'zzg.zhou' User.SELECT().selectOne("name=?", "zzg.zhou"); //SQL: SELECT `name`, `status` FROM `user` User.SELECT().include("name","status").select();   Page<User> page=User.WHERE().name.like("zzg%").status.in(1,2,3) .SELECT().selectPage(10,0); System.out.println(page.getTotalRow()); //SQL: SELECT * FROM `user` WHERE `name` like 'zzg%' AND `status` IN(0, 1) for(User x:User.WHERE().name.like("zzg%").status.in(0, 1).SELECT().select()){ System.out.println(x); } //SQL: SELECT * FROM `user` WHERE (`name` like 'zzg%' AND `status` >= 0)  //                             OR (`name` = 'zzg' AND `status` > 1) // ORDER BY `status` ASC  for(User x:User.WHERE() .name.like("zzg%").status.ge(0) .OR() .name.eq("zzg").status.gt(1) .status.asc() .SELECT().select()){ //SELECT / delete / update System.out.println(x); }   //general query TestDB.DB.select("SELECT * FROM user WHERE name like ?","zzg%"); TestDB.DB.createQuery().add("SELECT * FROM user WHERE name like ?","zzg%") .getList(User.class);   Query q=new Query(TestDB.DB); DataTable<DataMap> rs=q.add("SELECT * FROM user WHERE name like ?","zzg%")  .add(" AND status ").in(1,2,3)  .getList(); for(User x:rs.as(User.class)){ System.out.println(x); } //DataTable query //SQL: SELECT name, count(*) as cnt FROM _THIS_TABLE // WHERE status>=0 GROUP BY name ORDER BY name ASC DataTable<DataMap> newTable= rs.select("name, count(*) as cnt","status>=0","name ASC","GROUP BY name"); //update User user=User.SELECT().selectOne("name=?", "zzg.zhou"); user.setStatus(3).update(); User updateTo=new User().setName("tsc9526"); User.WHERE().name.like("zzg%").update(updateTo); //transaction Tx.execute(new Tx.Atom() { public int execute() { new User().setName("name001").setStatus(1).save(); new User().setName("name002").setStatus(2).save(); //... other database operation return 0; } });   //Dynamic model: Record Record r=new Record("user").use(TestDB.DB); r.set("name", "jjyy").set("status",1)  .save(); //SQL: SELECT * FROM `user` WHERE (`name` like 'jjyy%' AND `status` >= 0) //                             OR (`name` = 'zzg' AND `status` > 1) // ORDER BY `status` ASC  for(Record x:r.WHERE() .field("name").like("jjyy%").field("status").ge(0) .OR() .field("name").eq("zzg").field("status").gt(1) .field("status").asc() .SELECT().select()){ System.out.println(x); }  //SQL: DELETE FROM `user` WHERE `name` like 'jjyy%' AND `status` >= 0 r.WHERE()  .field("name").like("jjyy%").field("status").ge(0)  .delete();   //delete user.delete(); //SQL: DELETE FROM `user` WHERE `name`='china01' User.WHERE().name.eq("china01").delete(); //User.DELETE().deleteAll();   SQL查询自动产生结果类: /** * 数据访问类 */ public class UserBlogDao { final static long $VERSION$= 18L; //!!! 版本号, 每次保存为自动 1 //@Select 注解指示该方法需自动生成结果类 //默认类名: Result 方法名, 默认包名:数据访问类的包名 "." 数据访问类的名称(小写) //可选参数:name 指定生成结果类的名称 @Select(name="test.result.UserBlogs") //!!! 保存后会自动修改该函数的返回值为: List -> List<UserBlogs> //第一次编写时,由于结果类还不存在, 为了保证能够编译正常, //函数的返回值 和 查询结果要用 泛值 替代, 保存后,插件会自动修改. //函数的返回值 和 查询结果 泛值的对应关系分三类如下: //1. List查询 //public DataTable method_name(...){... return Query.getList(); } 或 //public List method_name(...){... return Query.getList(); } // //2. Page查询 //public Page method_name(...){... return Query.Page(); } // //3. 单条记录 //public Object method_name(...){... return Query.getResult(); } // public List selectUserBlogs(int user_id){ Query q=TestDB.DB.createQuery(); q.add(""/**~{ SELECT a.id,a.name,b.title, b.content,b.create_time FROM user a, blog b WHERE a.id=b.user_id AND a.id=? }*/, user_id); //!!! 保存后会自动修改 查询结果为: getList() -> getList<UserBlogs.class> return q.getList(); } @Select //自动产生结果类: test.dao.userblogdao.ResultSelectUserBlogsOne public Object selectUserBlogsOne(int user_id){ Query q=TestDB.DB.createQuery(); q.add(""/**~{ SELECT a.id,a.name,b.title, b.content,b.create_time FROM user a, blog b WHERE a.id=b.user_id AND a.id=? }*/, user_id); return q.getResult(); } } Eclipse 插件使用效果:(插件下载) 1. 支持Java多行字符串编辑: 2. 自动生成模型类: 3. 动态SQL自动产生结果类 更多用法参考:https://github.com/11039850/monalisa-orm/wiki maven坐标: <dependency> <groupId>com.tsc9526</groupId> <artifactId>monalisa-orm</artifactId> <version>1.7.0</version> </dependency> 备注: 目前只实现了Mysql接口,其它类型的数据库后续补上 标签:monalisa
I. Spring Boot Documentation 1. About the Documentation 2. Getting Help 3. First Steps 4. Working with Spring Boot 5. Learning about Spring Boot Features 6. Moving to Production 7. Advanced Topics II. Getting Started 8. Introducing Spring Boot 9. System Requirements 9.1. Servlet Containers 10. Installing Spring Boot 10.1. Installation Instructions for the Java Developer 10.1.1. Maven Installation 10.1.2. Gradle Installation 10.2. Installing the Spring Boot CLI 10.2.1. Manual Installation 10.2.2. Installation with SDKMAN! 10.2.3. OSX Homebrew Installation 10.2.4. MacPorts Installation 10.2.5. Command-line Completion 10.2.6. Quick-start Spring CLI Example 10.3. Upgrading from an Earlier Version of Spring Boot 11. Developing Your First Spring Boot Application 11.1. Creating the POM 11.2. Adding Classpath Dependencies 11.3. Writing the Code 11.3.1. The @RestController and @RequestMapping Annotations 11.3.2. The @EnableAutoConfiguration Annotation 11.3.3. The “main” Method 11.4. Running the Example 11.5. Creating an Executable Jar 12. What to Read Next III. Using Spring Boot 13. Build Systems 13.1. Dependency Management 13.2. Maven 13.2.1. Inheriting the Starter Parent 13.2.2. Using Spring Boot without the Parent POM 13.2.3. Using the Spring Boot Maven Plugin 13.3. Gradle 13.4. Ant 13.5. Starters 14. Structuring Your Code 14.1. Using the “default” Package 14.2. Locating the Main Application Class 15. Configuration Classes 15.1. Importing Additional Configuration Classes 15.2. Importing XML Configuration 16. Auto-configuration 16.1. Gradually Replacing Auto-configuration 16.2. Disabling Specific Auto-configuration Classes 17. Spring Beans and Dependency Injection 18. Using the @SpringBootApplication Annotation 19. Running Your Application 19.1. Running from an IDE 19.2. Running as a Packaged Application 19.3. Using the Maven Plugin 19.4. Using the Gradle Plugin 19.5. Hot Swapping 20. Developer Tools 20.1. Property Defaults 20.2. Automatic Restart 20.2.1. Logging changes in condition evaluation 20.2.2. Excluding Resources 20.2.3. Watching Additional Paths 20.2.4. Disabling Restart 20.2.5. Using a Trigger File 20.2.6. Customizing the Restart Classloader 20.2.7. Known Limitations 20.3. LiveReload 20.4. Global Settings 20.5. Remote Applications 20.5.1. Running the Remote Client Application 20.5.2. Remote Update 21. Packaging Your Application for Production 22. What to Read Next IV. Spring Boot features 23. SpringApplication 23.1. Startup Failure 23.2. Customizing the Banner 23.3. Customizing SpringApplication 23.4. Fluent Builder API 23.5. Application Events and Listeners 23.6. Web Environment 23.7. Accessing Application Arguments 23.8. Using the ApplicationRunner or CommandLineRunner 23.9. Application Exit 23.10. Admin Features 24. Externalized Configuration 24.1. Configuring Random Values 24.2. Accessing Command Line Properties 24.3. Application Property Files 24.4. Profile-specific Properties 24.5. Placeholders in Properties 24.6. Using YAML Instead of Properties 24.6.1. Loading YAML 24.6.2. Exposing YAML as Properties in the Spring Environment 24.6.3. Multi-profile YAML Documents 24.6.4. YAML Shortcomings 24.7. Type-safe Configuration Properties 24.7.1. Third-party Configuration 24.7.2. Relaxed Binding 24.7.3. Merging Complex Types 24.7.4. Properties Conversion Converting durations 24.7.5. @ConfigurationProperties Validation 24.7.6. @ConfigurationProperties vs. @Value 25. Profiles 25.1. Adding Active Profiles 25.2. Programmatically Setting Profiles 25.3. Profile-specific Configuration Files 26. Logging 26.1. Log Format 26.2. Console Output 26.2.1. Color-coded Output 26.3. File Output 26.4. Log Levels 26.5. Custom Log Configuration 26.6. Logback Extensions 26.6.1. Profile-specific Configuration 26.6.2. Environment Properties 27. Developing Web Applications 27.1. The “Spring Web MVC Framework” 27.1.1. Spring MVC Auto-configuration 27.1.2. HttpMessageConverters 27.1.3. Custom JSON Serializers and Deserializers 27.1.4. MessageCodesResolver 27.1.5. Static Content 27.1.6. Welcome Page 27.1.7. Custom Favicon 27.1.8. Path Matching and Content Negotiation 27.1.9. ConfigurableWebBindingInitializer 27.1.10. Template Engines 27.1.11. Error Handling Custom Error Pages Mapping Error Pages outside of Spring MVC 27.1.12. Spring HATEOAS 27.1.13. CORS Support 27.2. The “Spring WebFlux Framework” 27.2.1. Spring WebFlux Auto-configuration 27.2.2. HTTP Codecs with HttpMessageReaders and HttpMessageWriters 27.2.3. Static Content 27.2.4. Template Engines 27.2.5. Error Handling Custom Error Pages 27.2.6. Web Filters 27.3. JAX-RS and Jersey 27.4. Embedded Servlet Container Support 27.4.1. Servlets, Filters, and listeners Registering Servlets, Filters, and Listeners as Spring Beans 27.4.2. Servlet Context Initialization Scanning for Servlets, Filters, and listeners 27.4.3. The ServletWebServerApplicationContext 27.4.4. Customizing Embedded Servlet Containers Programmatic Customization Customizing ConfigurableServletWebServerFactory Directly 27.4.5. JSP Limitations 28. Security 28.1. MVC Security 28.2. WebFlux Security 28.3. OAuth2 28.3.1. Client 28.3.2. Server 28.4. Actuator Security 28.4.1. Cross Site Request Forgery Protection 29. Working with SQL Databases 29.1. Configure a DataSource 29.1.1. Embedded Database Support 29.1.2. Connection to a Production Database 29.1.3. Connection to a JNDI DataSource 29.2. Using JdbcTemplate 29.3. JPA and “Spring Data” 29.3.1. Entity Classes 29.3.2. Spring Data JPA Repositories 29.3.3. Creating and Dropping JPA Databases 29.3.4. Open EntityManager in View 29.4. Using H2’s Web Console 29.4.1. Changing the H2 Console’s Path 29.5. Using jOOQ 29.5.1. Code Generation 29.5.2. Using DSLContext 29.5.3. jOOQ SQL Dialect 29.5.4. Customizing jOOQ 30. Working with NoSQL Technologies 30.1. Redis 30.1.1. Connecting to Redis 30.2. MongoDB 30.2.1. Connecting to a MongoDB Database 30.2.2. MongoTemplate 30.2.3. Spring Data MongoDB Repositories 30.2.4. Embedded Mongo 30.3. Neo4j 30.3.1. Connecting to a Neo4j Database 30.3.2. Using the Embedded Mode 30.3.3. Neo4jSession 30.3.4. Spring Data Neo4j Repositories 30.3.5. Repository Example 30.4. Gemfire 30.5. Solr 30.5.1. Connecting to Solr 30.5.2. Spring Data Solr Repositories 30.6. Elasticsearch 30.6.1. Connecting to Elasticsearch by Using Jest 30.6.2. Connecting to Elasticsearch by Using Spring Data 30.6.3. Spring Data Elasticsearch Repositories 30.7. Cassandra 30.7.1. Connecting to Cassandra 30.7.2. Spring Data Cassandra Repositories 30.8. Couchbase 30.8.1. Connecting to Couchbase 30.8.2. Spring Data Couchbase Repositories 30.9. LDAP 30.9.1. Connecting to an LDAP Server 30.9.2. Spring Data LDAP Repositories 30.9.3. Embedded In-memory LDAP Server 30.10. InfluxDB 30.10.1. Connecting to InfluxDB 31. Caching 31.1. Supported Cache Providers 31.1.1. Generic 31.1.2. JCache (JSR-107) 31.1.3. EhCache 2.x 31.1.4. Hazelcast 31.1.5. Infinispan 31.1.6. Couchbase 31.1.7. Redis 31.1.8. Caffeine 31.1.9. Simple 31.1.10. None 32. Messaging 32.1. JMS 32.1.1. ActiveMQ Support 32.1.2. Artemis Support 32.1.3. Using a JNDI ConnectionFactory 32.1.4. Sending a Message 32.1.5. Receiving a Message 32.2. AMQP 32.2.1. RabbitMQ support 32.2.2. Sending a Message 32.2.3. Receiving a Message 32.3. Apache Kafka Support 32.3.1. Sending a Message 32.3.2. Receiving a Message 32.3.3. Additional Kafka Properties 33. Calling REST Services with RestTemplate 33.1. RestTemplate Customization 34. Calling REST Services with WebClient 34.1. WebClient Customization 35. Validation 36. Sending Email 37. Distributed Transactions with JTA 37.1. Using an Atomikos Transaction Manager 37.2. Using a Bitronix Transaction Manager 37.3. Using a Narayana Transaction Manager 37.4. Using a Java EE Managed Transaction Manager 37.5. Mixing XA and Non-XA JMS Connections 37.6. Supporting an Alternative Embedded Transaction Manager 38. Hazelcast 39. Quartz Scheduler 40. Spring Integration 41. Spring Session 42. Monitoring and Management over JMX 43. Testing 43.1. Test Scope Dependencies 43.2. Testing Spring Applications 43.3. Testing Spring Boot Applications 43.3.1. Detecting Web Application Type 43.3.2. Detecting Test Configuration 43.3.3. Excluding Test Configuration 43.3.4. Testing with a running server 43.3.5. Using JMX 43.3.6. Mocking and Spying Beans 43.3.7. Auto-configured Tests 43.3.8. Auto-configured JSON Tests 43.3.9. Auto-configured Spring MVC Tests 43.3.10. Auto-configured Spring WebFlux Tests 43.3.11. Auto-configured Data JPA Tests 43.3.12. Auto-configured JDBC Tests 43.3.13. Auto-configured jOOQ Tests 43.3.14. Auto-configured Data MongoDB Tests 43.3.15. Auto-configured Data Neo4j Tests 43.3.16. Auto-configured Data Redis Tests 43.3.17. Auto-configured Data LDAP Tests 43.3.18. Auto-configured REST Clients 43.3.19. Auto-configured Spring REST Docs Tests Auto-configured Spring REST Docs Tests with Mock MVC Auto-configured Spring REST Docs Tests with REST Assured 43.3.20. User Configuration and Slicing 43.3.21. Using Spock to Test Spring Boot Applications 43.4. Test Utilities 43.4.1. ConfigFileApplicationContextInitializer 43.4.2. TestPropertyValues 43.4.3. OutputCapture 43.4.4. TestRestTemplate 44. WebSockets 45. Web Services 46. Creating Your Own Auto-configuration 46.1. Understanding Auto-configured Beans 46.2. Locating Auto-configuration Candidates 46.3. Condition Annotations 46.3.1. Class Conditions 46.3.2. Bean Conditions 46.3.3. Property Conditions 46.3.4. Resource Conditions 46.3.5. Web Application Conditions 46.3.6. SpEL Expression Conditions 46.4. Testing your Auto-configuration 46.4.1. Simulating a Web Context 46.4.2. Overriding the Classpath 46.5. Creating Your Own Starter 46.5.1. Naming 46.5.2. autoconfigure Module 46.5.3. Starter Module 47. Kotlin support 47.1. Requirements 47.2. Null-safety 47.3. Kotlin API 47.3.1. runApplication 47.3.2. Extensions 47.4. Dependency management 47.5. @ConfigurationProperties 47.6. Testing 47.7. Resources 47.7.1. Further reading 47.7.2. Examples 48. What to Read Next V. Spring Boot Actuator: Production-ready features 49. Enabling Production-ready Features 50. Endpoints 50.1. Enabling Endpoints 50.2. Exposing Endpoints 50.3. Securing HTTP Endpoints 50.4. Configuring Endpoints 50.5. Hypermedia for Actuator Web Endpoints 50.6. Actuator Web Endpoint Paths 50.7. CORS Support 50.8. Implementing Custom Endpoints 50.8.1. Receiving Input Input type conversion 50.8.2. Custom Web Endpoints Web Endpoint Request Predicates Path HTTP method Consumes Produces Web Endpoint Response Status Web Endpoint Range Requests Web Endpoint Security 50.8.3. Servlet endpoints 50.8.4. Controller endpoints 50.9. Health Information 50.9.1. Auto-configured HealthIndicators 50.9.2. Writing Custom HealthIndicators 50.9.3. Reactive Health Indicators 50.9.4. Auto-configured ReactiveHealthIndicators 50.10. Application Information 50.10.1. Auto-configured InfoContributors 50.10.2. Custom Application Information 50.10.3. Git Commit Information 50.10.4. Build Information 50.10.5. Writing Custom InfoContributors 51. Monitoring and Management over HTTP 51.1. Customizing the Management Endpoint Paths 51.2. Customizing the Management Server Port 51.3. Configuring Management-specific SSL 51.4. Customizing the Management Server Address 51.5. Disabling HTTP Endpoints 52. Monitoring and Management over JMX 52.1. Customizing MBean Names 52.2. Disabling JMX Endpoints 52.3. Using Jolokia for JMX over HTTP 52.3.1. Customizing Jolokia 52.3.2. Disabling Jolokia 53. Loggers 53.1. Configure a Logger 54. Metrics 54.1. Getting started 54.2. Supported monitoring systems 54.2.1. Atlas 54.2.2. Datadog 54.2.3. Ganglia 54.2.4. Graphite 54.2.5. Influx 54.2.6. JMX 54.2.7. New Relic 54.2.8. Prometheus 54.2.9. SignalFx 54.2.10. Simple 54.2.11. StatsD 54.2.12. Wavefront 54.3. Supported Metrics 54.3.1. Spring MVC Metrics 54.3.2. Spring WebFlux Metrics 54.3.3. RestTemplate Metrics 54.3.4. Cache Metrics 54.3.5. DataSource Metrics 54.3.6. RabbitMQ Metrics 54.4. Registering custom metrics 54.5. Customizing individual metrics 54.5.1. Per-meter properties 54.6. Metrics endpoint 55. Auditing 56. HTTP Tracing 56.1. Custom HTTP tracing 57. Process Monitoring 57.1. Extending Configuration 57.2. Programmatically 58. Cloud Foundry Support 58.1. Disabling Extended Cloud Foundry Actuator Support 58.2. Cloud Foundry Self-signed Certificates 58.3. Custom context path 59. What to Read Next VI. Deploying Spring Boot Applications 60. Deploying to the Cloud 60.1. Cloud Foundry 60.1.1. Binding to Services 60.2. Heroku 60.3. OpenShift 60.4. Amazon Web Services (AWS) 60.4.1. AWS Elastic Beanstalk Using the Tomcat Platform Using the Java SE Platform 60.4.2. Summary 60.5. Boxfuse and Amazon Web Services 60.6. Google Cloud 61. Installing Spring Boot Applications 61.1. Supported Operating Systems 61.2. Unix/Linux Services 61.2.1. Installation as an init.d Service (System V) Securing an init.d Service 61.2.2. Installation as a systemd Service 61.2.3. Customizing the Startup Script Customizing the Start Script when It Is Written Customizing a Script When It Runs 61.3. Microsoft Windows Services 62. What to Read Next VII. Spring Boot CLI 63. Installing the CLI 64. Using the CLI 64.1. Running Applications with the CLI 64.1.1. Deduced “grab” Dependencies 64.1.2. Deduced “grab” Coordinates 64.1.3. Default Import Statements 64.1.4. Automatic Main Method 64.1.5. Custom Dependency Management 64.2. Applications with Multiple Source Files 64.3. Packaging Your Application 64.4. Initialize a New Project 64.5. Using the Embedded Shell 64.6. Adding Extensions to the CLI 65. Developing Applications with the Groovy Beans DSL 66. Configuring the CLI with settings.xml 67. What to Read Next VIII. Build tool plugins 68. Spring Boot Maven Plugin 68.1. Including the Plugin 68.2. Packaging Executable Jar and War Files 69. Spring Boot Gradle Plugin 70. Spring Boot AntLib Module 70.1. Spring Boot Ant Tasks 70.1.1. spring-boot:exejar 70.1.2. Examples 70.2. spring-boot:findmainclass 70.2.1. Examples 71. Supporting Other Build Systems 71.1. Repackaging Archives 71.2. Nested Libraries 71.3. Finding a Main Class 71.4. Example Repackage Implementation 72. What to Read Next IX. ‘How-to’ guides 73. Spring Boot Application 73.1. Create Your Own FailureAnalyzer 73.2. Troubleshoot Auto-configuration 73.3. Customize the Environment or ApplicationContext Before It Starts 73.4. Build an ApplicationContext Hierarchy (Adding a Parent or Root Context) 73.5. Create a Non-web Application 74. Properties and Configuration 74.1. Automatically Expand Properties at Build Time 74.1.1. Automatic Property Expansion Using Maven 74.1.2. Automatic Property Expansion Using Gradle 74.2. Externalize the Configuration of SpringApplication 74.3. Change the Location of External Properties of an Application 74.4. Use ‘Short’ Command Line Arguments 74.5. Use YAML for External Properties 74.6. Set the Active Spring Profiles 74.7. Change Configuration Depending on the Environment 74.8. Discover Built-in Options for External Properties 75. Embedded Web Servers 75.1. Use Another Web Server 75.2. Disabling the Web Server 75.3. Configure Jetty 75.4. Add a Servlet, Filter, or Listener to an Application 75.4.1. Add a Servlet, Filter, or Listener by Using a Spring Bean Disable Registration of a Servlet or Filter 75.4.2. Add Servlets, Filters, and Listeners by Using Classpath Scanning 75.5. Change the HTTP Port 75.6. Use a Random Unassigned HTTP Port 75.7. Discover the HTTP Port at Runtime 75.8. Configure SSL 75.9. Configure HTTP/2 75.9.1. HTTP/2 with Undertow 75.9.2. HTTP/2 with Jetty 75.9.3. HTTP/2 with Tomcat 75.10. Configure Access Logging 75.11. Running Behind a Front-end Proxy Server 75.11.1. Customize Tomcat’s Proxy Configuration 75.12. Configure Tomcat 75.13. Enable Multiple Connectors with Tomcat 75.14. Use Tomcat’s LegacyCookieProcessor 75.15. Configure Undertow 75.16. Enable Multiple Listeners with Undertow 75.17. Create WebSocket Endpoints Using @ServerEndpoint 75.18. Enable HTTP Response Compression 76. Spring MVC 76.1. Write a JSON REST Service 76.2. Write an XML REST Service 76.3. Customize the Jackson ObjectMapper 76.4. Customize the @ResponseBody Rendering 76.5. Handling Multipart File Uploads 76.6. Switch Off the Spring MVC DispatcherServlet 76.7. Switch off the Default MVC Configuration 76.8. Customize ViewResolvers 77. HTTP Clients 77.1. Configure RestTemplate to Use a Proxy 78. Logging 78.1. Configure Logback for Logging 78.1.1. Configure Logback for File-only Output 78.2. Configure Log4j for Logging 78.2.1. Use YAML or JSON to Configure Log4j 2 79. Data Access 79.1. Configure a Custom DataSource 79.2. Configure Two DataSources 79.3. Use Spring Data Repositories 79.4. Separate @Entity Definitions from Spring Configuration 79.5. Configure JPA Properties 79.6. Configure Hibernate Naming Strategy 79.7. Use a Custom EntityManagerFactory 79.8. Use Two EntityManagers 79.9. Use a Traditional persistence.xml File 79.10. Use Spring Data JPA and Mongo Repositories 79.11. Expose Spring Data Repositories as REST Endpoint 79.12. Configure a Component that is Used by JPA 79.13. Configure jOOQ with Two DataSources 80. Database Initialization 80.1. Initialize a Database Using JPA 80.2. Initialize a Database Using Hibernate 80.3. Initialize a Database 80.4. Initialize a Spring Batch Database 80.5. Use a Higher-level Database Migration Tool 80.5.1. Execute Flyway Database Migrations on Startup 80.5.2. Execute Liquibase Database Migrations on Startup 81. Messaging 81.1. Disable Transacted JMS Session 82. Batch Applications 82.1. Execute Spring Batch Jobs on Startup 83. Actuator 83.1. Change the HTTP Port or Address of the Actuator Endpoints 83.2. Customize the ‘whitelabel’ Error Page 84. Security 84.1. Switch off the Spring Boot Security Configuration 84.2. Change the UserDetailsService and Add User Accounts 84.3. Enable HTTPS When Running behind a Proxy Server 85. Hot Swapping 85.1. Reload Static Content 85.2. Reload Templates without Restarting the Container 85.2.1. Thymeleaf Templates 85.2.2. FreeMarker Templates 85.2.3. Groovy Templates 85.3. Fast Application Restarts 85.4. Reload Java Classes without Restarting the Container 86. Build 86.1. Generate Build Information 86.2. Generate Git Information 86.3. Customize Dependency Versions 86.4. Create an Executable JAR with Maven 86.5. Use a Spring Boot Application as a Dependency 86.6. Extract Specific Libraries When an Executable Jar Runs 86.7. Create a Non-executable JAR with Exclusions 86.8. Remote Debug a Spring Boot Application Started with Maven 86.9. Build an Executable Archive from Ant without Using spring-boot-antlib 87. Traditional Deployment 87.1. Create a Deployable War File 87.2. Convert an Existing Application to Spring Boot 87.3. Deploying a WAR to WebLogic 87.4. Use Jedis Instead of Lettuce X. Appendices A. Common application properties B. Configuration Metadata B.1. Metadata Format B.1.1. Group Attributes B.1.2. Property Attributes B.1.3. Hint Attributes B.1.4. Repeated Metadata Items B.2. Providing Manual Hints B.2.1. Value Hint B.2.2. Value Providers Any Class Reference Handle As Logger Name Spring Bean Reference Spring Profile Name B.3. Generating Your Own Metadata by Using the Annotation Processor B.3.1. Nested Properties B.3.2. Adding Additional Metadata C. Auto-configuration classes C.1. From the “spring-boot-autoconfigure” module C.2. From the “spring-boot-actuator-autoconfigure” module D. Test auto-configuration annotations E. The Executable Jar Format E.1. Nested JARs E.1.1. The Executable Jar File Structure E.1.2. The Executable War File Structure E.2. Spring Boot’s “JarFile” Class E.2.1. Compatibility with the Standard Java “JarFile” E.3. Launching Executable Jars E.3.1. Launcher Manifest E.3.2. Exploded Archives E.4. PropertiesLauncher Features E.5. Executable Jar Restrictions E.6. Alternative Single Jar Solutions F. Dependency versions

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

可口可乐Vip

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值