uni組件符合easycom規(guī)范,HBuilderX 2.5.5起,只需將本組件導入項目,在頁面template中即可直接使用,無需在頁面中import和注冊components
HBuilder插件市場(uni-ui組件庫)
uni-ui
不支持使用Vue.use()
的方式安裝
npm安裝uni-ui庫(vue-cli項目需先安裝sass及sass-loader,HBuilder可省略)
安裝sass:npm i sass -D 或者 yarn add sass -D
安裝sass-loader(使用低于@11.0.0版本,sass-loader@11.0.0不支持vue@2.6.12):npm i sass-loader@10.1.1 -D 或者 yarn add sass-loader@10.1.1 -D
安裝uni-ui:npm i @dcloudio/uni-ui 或者 yarn add @dcloudio/uni-ui
在
script
中引用組件:
import {uniBadge} from '@dcloudio/uni-ui'
//import uniBadge from '@dcloudio/uni-ui/lib/uni-badge/uni-badge.vue' //也可使用此方式引入組件
export default {
components: {uniBadge}
}
在template中使用組件:
<uni-badge text="1"></uni-badge>
<uni-badge text="2" type="success" @click="bindClick"></uni-badge>
<uni-badge text="3" type="primary" :inverted="true"></uni-badge>
CLI引用方式:H5端不支持在main.js中全局注冊組件,可使用
easyCom
的方式引用組件easyCom
:傳統(tǒng)vue組件,需要安裝、引用、注冊三個步驟后才能使用組件,easyCom
將其精簡為一步,只要組件安裝在項目的components目錄下,并符合components/組件名稱/組件名稱.vue
目錄結(jié)構(gòu),就可以不用引用、注冊,直接在頁面中使用
使用npm安裝的組件,默認情況下babel-loader
會省略所有node-modules
中的文件,導致條件編譯失效,需要通過配置vue.config.js
文件解決:
// 在根目錄創(chuàng)建 vue.config.js 文件,并配置如下
module.exports = {
transpileDependencies: ['@dcloudio/uni-ui']
}
》使用npm+easycom
使用npm安裝好
uni-ui
之后,需要配置easycom
規(guī)則,讓npm
安裝的組件支持easycom
打開項目根目錄下的pages.json
并添加easycom
節(jié)點
// pages.json
{
"easycom": {
"autoscan": true,
"custom": {
// uni-ui 規(guī)則如下配置
"^uni-(.*)": "@dcloudio/uni-ui/lib/uni-$1/uni-$1.vue"
}
},
// 其他內(nèi)容
pages:[
// ...
]
}
- uni-badge:數(shù)字角標
<uni-badge size="small" :text="100" absolute="rightBottom" type="primary">
<button type="default">右上</button>
</uni-badge>
<uni-badge text="1"></uni-badge>
<uni-badge text="2" type="purple" @click="bindClick"></uni-badge>
<uni-badge text="3" type="primary" :inverted="true"></uni-badge>
- uni-calendar:日歷
- 本組件農(nóng)歷轉(zhuǎn)換使用的js是
@1900-2100
區(qū)間內(nèi)的公歷、農(nóng)歷互轉(zhuǎn) -
date
屬性傳入的應該是一個String
,格式為:2019-06-01
- 通過 insert 屬性來確定當前的事件是 @change 還是 @confirm 。理應合并為一個事件,但是為了區(qū)分模式,現(xiàn)使用兩個事件,這里需要注意
- 彈窗模式下無法阻止后面的元素滾動,如有需要阻止,請在彈窗彈出后,手動設(shè)置滾動元素為不可滾動
<view>
<uni-calendar
:insert="true"
:lunar="true"
:start-date="'2019-3-2'"
:end-date="'2019-5-20'"
@change="change"
/>
</view>
通過方法打開日歷
<view>
<uni-calendar
ref="calendar"
:insert="false"
@confirm="confirm"
/>
<button @click="open">打開日歷</button>
</view>
export default {
data() {
return {};
},
methods: {
open(){
this.$refs.calendar.open();
},
confirm(e) {
console.log(e);
}
}
};
- uni-card:卡片
- 因為平臺兼容問題,目前
APP-NVUE
安卓平臺下不支持陰影
<!-- 一般用法 -->
<uni-card title="標題文字" thumbnail="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/460d46d0-4fcc-11eb-8ff1-d5dcf8779628.png" extra="額外信息" note="Tips">
內(nèi)容主體,可自定義內(nèi)容及樣式
</uni-card>
<!-- 內(nèi)容通欄 -->
<uni-card is-full="true" title="DCloud" thumbnail="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/460d46d0-4fcc-11eb-8ff1-d5dcf8779628.png" extra="2018.12.12" >
<image src="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/460d46d0-4fcc-11eb-8ff1-d5dcf8779628.png" style="width: 100%;"></image>
</uni-card>
<!-- 圖文卡片模式 -->
<uni-card
title="標題文字"
mode="style"
:is-shadow="true"
thumbnail="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/460d46d0-4fcc-11eb-8ff1-d5dcf8779628.png"
extra="Dcloud 2019-05-20 12:32:19"
note="Tips"
>
uni-app 是一個使用 Vue.js 開發(fā)所有前端應用的框架,開發(fā)者編寫一套代碼,可編譯到iOS、Android、H5、以及各種小程序等多個平臺。即使不跨端,uni-app同時也是更好的小程序開發(fā)框架。
</uni-card>
<!-- 標題卡片模式 -->
<uni-card
title="Dcloud"
mode="title"
:is-shadow="true"
thumbnail="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/460d46d0-4fcc-11eb-8ff1-d5dcf8779628.png"
extra="技術(shù)沒有上限"
note="Tips"
>
uni-app 是一個使用 Vue.js 開發(fā)所有前端應用的框架,開發(fā)者編寫一套代碼,可編譯到iOS、Android、H5、以及各種小程序等多個平臺。即使不跨端,uni-app同時也是更好的小程序開發(fā)框架。
</uni-card>
<!-- 自定義底部按鈕 -->
<uni-card title="Dcloud" note="true">
默認內(nèi)容
<template v-slot:footer>
<view class="footer-box">
<view>喜歡</view>
<view>評論</view>
<view>分享</view>
</view>
</template>
</uni-card>
- uni-collapse:折疊面板
- 新增
show-arrow
屬性,控制是否顯示右側(cè)箭頭 - 組件需要依賴
sass
插件 - App端默認關(guān)閉組件動畫:因為height動畫開銷比較大,會導致頁面卡頓
- 在小程序端組件內(nèi)容發(fā)生變化,需要手動調(diào)用
resize()
方法,手動更新幾點信息,避免出現(xiàn)內(nèi)容錯位 - 如需自定義組件默認邊框顏色等,需使用插槽自定義內(nèi)容并合理使用
border
和title-border
屬性 - 組件支持
nvue
,需要在manifest.json>app-plus
節(jié)點下配置"nvueStyleCompiler":"uni-app"
<uni-collapse>
<uni-collapse-item title="默認開啟" :open="true">
<text>折疊內(nèi)容</text>
</uni-collapse-item>
<uni-collapse-item title="折疊內(nèi)容">
<text>折疊內(nèi)容</text>
</uni-collapse-item>
<uni-collapse-item title="禁用狀態(tài)" disabled>
<text>折疊內(nèi)容</text>
</uni-collapse-item>
</uni-collapse>
》手風琴效果:
使用accordion
屬性,open
屬性則生效在最后一個組件
<uni-collapse accordion>
<uni-collapse-item title="手風琴效果">
<text>折疊內(nèi)容</text>
</uni-collapse-item>
<uni-collapse-item title="手風琴效果">
<text>折疊內(nèi)容</text>
</uni-collapse-item>
<uni-collapse-item title="禁用狀態(tài)" disabled>
<text>折疊內(nèi)容</text>
</uni-collapse-item>
</uni-collapse>
》動態(tài)設(shè)置折疊面板打開狀態(tài)
- 使用
v-model
屬性,動態(tài)設(shè)置面板的顯示狀態(tài) - 使用
name
屬性設(shè)置每個面板的唯一標識,如不設(shè)置使用默認索引,從字符串"0"開始記數(shù) -
v-model
屬性最好不要和open
屬性同時使用 -
accordion
屬性值: - ”true“,類型為String
- ”false“,類型為Array
<uni-collapse v-model="value">
<uni-collapse-item name="key1" title="默認開啟">
<text>折疊內(nèi)容</text>
</uni-collapse-item>
<uni-collapse-item name="key2" title="默認開啟">
<text>折疊內(nèi)容</text>
</uni-collapse-item>
<uni-collapse-item name="key3" title="默認不開啟">
<text>折疊內(nèi)容</text>
</uni-collapse-item>
</uni-collapse>
export default {
data(){
return {
value:['key1','key2'],
// 如果設(shè)置了 accordion 屬性,則使用 string 類型
// value:'key1'
}
}
}
》使用動畫
使用show-animation
屬性開啟或關(guān)閉面板折疊動畫,默認動畫開啟
<uni-collapse>
<uni-collapse-item :show-animation="true" title="開啟動畫">
<text>折疊內(nèi)容</text>
</uni-collapse-item>
<uni-collapse-item :show-animation="true" title="開啟動畫">
<text>折疊內(nèi)容</text>
</uni-collapse-item>
<uni-collapse-item :show-animation="false" title="不開啟動畫">
<text>折疊內(nèi)容</text>
</uni-collapse-item>
</uni-collapse>
》配置圖片
使用thumb
配置圖片地址,可在面板左側(cè)顯示一個圖片
<uni-collapse>
<uni-collapse-item title="標題文字"
thumb="https://vkceyugu.cdn.bspapp.com/VKCEYUGU-dc-site/460d46d0-4fcc-11eb-8ff1-d5dcf8779628.png">
<view class="content">
<text class="text">折疊內(nèi)容主體,可自定義內(nèi)容及樣式</text>
</view>
</uni-collapse-item>
</uni-collapse>
》自定義插槽
如果需要自定義面板顯示,可以使用title
插槽達成完全自定義
使用uni-list
的列表示例,需引入uni-list
組件
在折疊面板組件中使用list
時,在App-Nvue
下不要使用uni-list-item
,會導致組件無法正常顯示,其他平臺不做限制
在默認插槽里使用uni-list
組件與上方示例一樣,直接寫在默認插槽即可
<uni-collapse>
<!-- 因為list默認帶一條分隔線,所以使用 titleBorder="none" 取消面板的分隔線 -->
<uni-collapse-item title-border="none" :border="false">
<template v-slot:title>
<uni-list>
<uni-list-item title="標題使用自定義標題插槽" :show-extra-icon="true" :extra-icon="extraIcon">
</uni-list-item>
</uni-list>
</template>
<view class="content">
<text class="text">折疊內(nèi)容主體,可自定義內(nèi)容及樣式</text>
</view>
</uni-collapse-item>
</uni-collapse>
resize():更新當前列表高度
- 解決動態(tài)添加數(shù)據(jù),帶動畫的折疊面板高度不更新的問題
- 需要在數(shù)據(jù)渲染完畢之后使用
resize
方法=》推薦在this.$nextTick()
中使用- 當前只有小程序端需要調(diào)用此方法,H5\App端已經(jīng)做了處理,不需要手動更新高度
<view>
<uni-collapse ref="collapse" v-model="value">
<uni-collapse-item title="默認開啟" >
<view class="content">
<text class="text">{{content}}</text>
</view>
</uni-collapse-item>
<uni-collapse-item title="折疊內(nèi)容">
<view class="content">
<text class="text">折疊內(nèi)容主體,這是一段比較長內(nèi)容。默認折疊主要內(nèi)容,只顯示當前項標題。點擊標題展開,才能看到這段文字。再次點擊標題,折疊內(nèi)容。</text>
</view>
</uni-collapse-item>
</uni-collapse>
<button class="button" type="primary" @click="add">動態(tài)修改內(nèi)容</button>
</view>
export default {
data() {
return {
value:['0'],
content: '折疊內(nèi)容主體,可自定義內(nèi)容及樣式,點擊按鈕修改內(nèi)容使高度發(fā)生變化。',
}
},
methods: {
add() {
if (this.content.length > 35) {
this.content = '折疊內(nèi)容主體,可自定義內(nèi)容及樣式,點擊按鈕修改內(nèi)容使高度發(fā)生變化。'
} else {
this.content = '折疊內(nèi)容主體,這是一段比較長內(nèi)容。通過點擊按鈕修改后內(nèi)容后,使組件高度發(fā)生變化,在次點擊按鈕恢復之前的內(nèi)容和高度。'
}
// TODO 小程序中不支持自動更新 ,需要手動resize 更新組件高度
// #ifdef MP
this.$nextTick(() => {
this.$refs.collapse.resize()
})
// #endif
}
}
}
- uni-combox:組合框
- 可以同時選擇和輸入的表單項
<uni-combox label="所在城市" :candidates="candidates" placeholder="請選擇所在城市" v-model="city"></uni-combox>
- uni-countdown:倒計時
<!-- 一般用法 -->
<uni-countdown :day="1" :hour="1" :minute="12" :second="40"></uni-countdown>
<!-- 不顯示天數(shù) -->
<uni-countdown :show-day="false" :hour="12" :minute="12" :second="12"></uni-countdown>
<!-- 修改顏色 -->
<uni-countdown color="#FFFFFF" background-color="#00B26A" border-color="#00B26A" :day="1" :hour="2" :minute="30" :second="0"></uni-countdown>
- uni-data-checkbox:數(shù)據(jù)選擇器
- 數(shù)據(jù)綁定型組件:給本組件綁定一個data,會自動渲染一組候選內(nèi)容
- 自動的表單校驗:組件綁定了data,且符合
uni-forms
組件的表單校驗規(guī)范,搭配使用會自動實現(xiàn)表單校驗 - 本組件合并了單選和多選
- 在
uniCloun
開發(fā)中,DB Schema
中配置了enum
枚舉等類型后,在web控制臺的自動生成表單功能中,會自動生成uni-data-checkbox
組件并綁定好data - 本組件需依賴sass插件,需自行安裝
- 本組件為數(shù)據(jù)驅(qū)動,目的是快速投入使用,只可通過style覆蓋有限樣式,不支持自定義更多樣式
- 組件支持
nvue
,需要在manifest.json>app-plus
節(jié)點下配置"nvueStyleCompiler":"uni-app"
設(shè)置
localdata
屬性后,組件會通過數(shù)據(jù)渲染出對應的內(nèi)容,默認顯示的是單選框
當:multiple="false"
時(單選),value/v-model
的值是String|Number
類型
<template>
<view>
<uni-data-checkbox v-model="value" :localdata="range" @change="change"></uni-data-checkbox>
</view>
</template>
export default {
data() {
return {
value: 0,
range: [{"value": 0,"text": "籃球" },{"value": 1,"text": "足球"},{"value": 2,"text": "游泳"}]
}
},
methods: {
change(e){
console.log('e:',e);
}
}
}
》多選框
- 設(shè)置
multiple
屬性,組件顯示為多選框 - 當
:multiple="true"
時(多選),value/v-model
的值是Array
類型
<template>
<view>
<uni-data-checkbox multiple v-model="value" :localdata="range" @change="change"></uni-data-checkbox>
</view>
</template>
export default {
data() {
return {
value: [0,2],
range: [{"value": 0,"text": "籃球" },{"value": 1,"text": "足球"},{"value": 2,"text": "游泳"}]
}
},
methods: {
change(e){
console.log('e:',e);
}
}
}
》設(shè)置最大最小值
- 當
:multiple="true"
時(多選),可以設(shè)置min
、max
- 如果選中個數(shù)小于
min
屬性設(shè)置的值,則選擇內(nèi)容將不可取消,只有當選中個數(shù)大于等于min
且小于`max``時,才可取消選中 - 如果選中個數(shù)大于等于
max
屬性設(shè)置的值,那么其他未選中內(nèi)容將不可選
<template>
<view>
<uni-data-checkbox min="1" max="2" multiple v-model="value" :localdata="range" @change="change"></uni-data-checkbox>
</view>
</template>
export default {
data() {
return {
value: [0,2],
range: [{"value": 0,"text": "籃球" },{"value": 1,"text": "足球"},{"value": 2,"text": "游泳"}]
}
},
methods: {
change(e){
console.log('e:',e);
}
}
}
》設(shè)置禁用
- 禁用需要在
localdata
屬性的數(shù)據(jù)源中添加disable
屬性
<template>
<view>
<uni-data-checkbox v-model="value" :localdata="range" @change="change"></uni-data-checkbox>
</view>
</template>
export default {
data() {
return {
value: 0,
range: [{
"value": 0,
"text": "籃球"
},
{
"value": 1,
"text": "足球",
// 禁用當前項
"disable":true
},
{
"value": 2,
"text": "游泳"
}
]
}
},
methods: {
change(e){
console.log('e:',e);
}
}
}
》自定義選中顏色
- 設(shè)置
selectedColor
屬性,可以修改組件選中后的圖標及邊框顏色 - 設(shè)置
selectedColor
屬性,可以修改組件選中后的文字顏色,如不填寫默認同selectedColor
屬性,mode
屬性為tag
時,默認為白色
<template>
<view>
<uni-data-checkbox selectedColor="red" selectedTextColor="red" multiple v-model="value" :localdata="range" @change="change"></uni-data-checkbox>
</view>
</template>
export default {
data() {
return {
value: [0,2],
range: [{"value": 0,"text": "籃球" },{"value": 1,"text": "足球"},{"value": 2,"text": "游泳"}]
}
},
methods: {
change(e){
console.log('e:',e);
}
}
}
》更多模式文章來源:http://www.zghlxwxcb.cn/news/detail-401586.html
- 設(shè)置
mode
屬性,可以設(shè)置更多顯示樣式,目前內(nèi)置樣式有四種default/list/button/tag
- 如果需要禁用某項,需要在
localdata
屬性的數(shù)據(jù)源中添加disable
屬性
<template>
<view>
<!-- 默認 default -->
<uni-data-checkbox v-model="value" :localdata="range" @change="change"></uni-data-checkbox>
<!-- 列表 list ,顯示左側(cè)圖標 -->
<uni-data-checkbox mode="list" icon="left" v-model="value" :localdata="range" @change="change"></uni-data-checkbox>
<!-- 列表 list ,顯示右側(cè)圖標 -->
<uni-data-checkbox mode="list" icon="right" v-model="value" :localdata="range" @change="change"></uni-data-checkbox>
<!-- 按鈕 button -->
<uni-data-checkbox mode="button" v-model="value" :localdata="range" @change="change"></uni-data-checkbox>
<!-- 標簽 tag -->
<uni-data-checkbox mode="tag" v-model="value" :localdata="range" @change="change"></uni-data-checkbox>
</view>
</template>
export default {
data() {
return {
value: 0,
range: [{"value": 0,"text": "籃球" },{"value": 1,"text": "足球"},{"value": 2,"text": "游泳"}]
}
},
methods: {
change(e){
console.log('e:',e);
}
}
}
文章來源地址http://www.zghlxwxcb.cn/news/detail-401586.html
到了這里,關(guān)于Uniapp之uni-ui-擴展組件(1)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!