uni-app入門和電商首頁實(shí)現(xiàn)

完成入門案例

步驟1.下載編輯器

hbuilder

1.1 創(chuàng)建項(xiàng)目hello-uni-app

文檔見快速上手-創(chuàng)建uni-app

1.2 清空pages/index/index.vue

<template>
    <view class="content">
    </view>
</template>
<script>
export default {
    data() {
        return {
        }
    },
    onLoad() {
    },
    methods: {
    }
}
</script>
<style>
</style>

步驟2.默認(rèn)組件

2.1 修改index.vue

<view class="content">
    <view>塊1</view>
    <view>塊2</view>
    <view><text>字1</text><text>字2</text></view>
</view>

步驟3.運(yùn)行程序

步驟4.打印日志

onLoad() {
    console.log('hello')
},

步驟5.使用擴(kuò)展組件的插件

    <uni-icons type="contact" size="30"></uni-icons>
import uniIcons from "@/components/uni-icons/uni-icons.vue"
components: {uniIcons},

步驟6.自定義組件

6.1定義button-counter組件

組件注冊

  • 新增button-counter.vue
<template>
    <view>
        <button @click="handleClick">{{num}}</button>
    </view>
</template>
<script>
    export default {
        data() {
            return {
                num: 1
            };
        },
        methods:{
            handleClick(){
                this.num++
            }
        }
    }
</script>
<style>
</style>
  • 修改index.vue
<template>
    <button-counter></button-counter>
</template>
<script>
    import buttonCounter from "@/components/button-counter.vue"
    components: {uniIcons, buttonCounter},
</script>

步驟7.API調(diào)用

    <image v-for="item in carouselList" :key="item.id" :src="item.goodsPic" mode="aspectFit" style="width: 100%;"></image>
data() {
    return {
        carouselList:[]
    }
},
onLoad() {
    this.getAdBanners()
},
methods: {
    async getAdBanners(){
        var [error, res] = await uni.request({
            url: 'https://a0e83064-1d70-4acb-8b07-43b6d3ef7b23.bspapp.com/http/adBanners'
        })
        this.carouselList = res.data.data.list
        console.log('this.carouselList', this.carouselList)
    }
}

完成shop案例

步驟1.整理頁面

1.1 創(chuàng)建項(xiàng)目shop

1.2 清空pages/index/index.vue

  • template中刪除多余的標(biāo)簽,和修改class為container
  • script data屬性清空.添加onload生命周期
  • style 清空內(nèi)容,添加lang="less" scoped屬性
<template>
    <view class="container">
    </view>
</template>
<script>
    export default {
        data() {
            return {
            }
        },
        onLoad() {
        },
        methods: {

        }
    }
</script>

<style lang="less" scoped>
</style>

1.3 添加頁面背景顏色

  • 打開文檔
page {
    background: #f5f5f5;
}

步驟2 完成tabBar完成

2.1 打開參考網(wǎng)站

2.2 打開素材/圖片拷貝本地圖片到static文件夾

2.3 打開配置文檔,并查看代碼示例

2.4 修改page.json,新增tabBar屬性

    "tabBar":{
        "color": "#C0C4CC",
        "selectedColor":"#FF713F",
        "list": [{
            "pagePath": "pages/index/index",
            "iconPath": "static/tab-home.png",
            "selectedIconPath": "static/tab-home-current.png",
            "text": "首頁"
        },
        {
            "pagePath": "pages/category/category",
            "iconPath": "static/tab-cate.png",
            "selectedIconPath": "static/tab-cate-current.png",
            "text": "分類"
        },
        {
            "pagePath": "pages/cart/cart",
            "iconPath": "static/tab-cart.png",
            "selectedIconPath": "static/tab-cart-current.png",
            "text": "購物車"
        },
        {
            "pagePath": "pages/user/user",
            "iconPath": "static/tab-my.png",
            "selectedIconPath": "static/tab-my-current.png",
            "text": "我的"
        }]
    }

2.5 新建其他頁面

頁面名,填category,cart,user

步驟3 完成導(dǎo)航欄

3.1 打開配置文檔 ,打開h5的配置文檔,打開導(dǎo)航欄的配置文檔

3.2 修改page.json,添加搜索框

"style": {
    "navigationBarTitleText": "首頁",
    "h5": {
        "titleNView": {
            "searchInput": {
                
            }
        }
    }
}

3.3 修改page.json,美好搜索框

"searchInput": {
    "backgroundColor": "rgba(231, 231, 231,.7)",
    "borderRadius": "16px",
    "placeholder": "搜索",
    "align": "left",
    "fontSize": "12px",
    "placeholderColor": "#aaa"
}

3.4 打開字體圖標(biāo)

拷貝字體圖標(biāo)到static文件夾

3.5 修改page.json,添加左側(cè)圖標(biāo)

"titleNView": {
    "buttons": [{
        "float": "left",
        "fontSrc": "/static/iconfont/iconfont.ttf",
        "text": "&#xe618;"
    }]
}

3.6 修改page.json,添加右側(cè)圖標(biāo)

    "buttons": [{
        "float": "left",
        "fontSrc": "/static/iconfont/iconfont.ttf",
        "text": "&#xe618;"
    },{ 
        "text": "登錄",
        "fontSize": "12px",
        "redDot": true
    }]

3.7 導(dǎo)航欄兼容性,app端

  • 添加app-plus,復(fù)制h5的屬性值
  • 打開模擬器,運(yùn)行
"app-plus": {
    
}

3.8 導(dǎo)航欄兼容性,app端,修改圖標(biāo)內(nèi)容

"buttons": [{
    "float": "left",
    "fontSrc": "/static/iconfont/iconfont.ttf",
    "text": "\e618;"
}]

3.9 導(dǎo)航欄兼容性,小程序端,引入第三方插件

  • 新建cart,user頁面
  • 搜索插件NavBar 導(dǎo)航欄,并安裝它
  • 修改index.vue
<template>
    <view class="container">
        <uni-nav-bar left-icon="back" left-text="返回" right-text="菜單" title="導(dǎo)航欄組件"></uni-nav-bar>
    </view>
</template>
<script>
    import uniNavBar from "@/components/uni-nav-bar/uni-nav-bar.vue"
    export default {
        components: {uniNavBar},
    }
</script>

3.10 導(dǎo)航欄兼容性,小程序端,美化導(dǎo)航欄

  • 導(dǎo)入navBar的案例
  • 修改index.vue

3.10.1 復(fù)制案例代碼

<uni-nav-bar :fixed="false" color="#333333" background-color="#FFFFFF" right-icon="scan" @clickLeft="showCity" @clickRight="scan">
    <block slot="left">
        <view class="city">
            <view><text class="uni-nav-bar-text">{{ city }}</text></view>
            <uni-icons type="arrowdown" color="#333333" size="22" />
        </view>
    </block>
    <view class="input-view">
        <uni-icons class="input-uni-icon" type="search" size="22" color="#666666" />
        <input confirm-type="search" class="nav-bar-input" type="text" placeholder="輸入搜索關(guān)鍵詞" @confirm="confirm">
    </view>
</uni-nav-bar>
.input-view {
    /* #ifndef APP-PLUS-NVUE */
    display: flex;
    /* #endif */
    flex-direction: row;
    /* width: 500rpx;*/
    flex: 1;
    background-color: #f8f8f8;
    height: 30px;
    border-radius: 15px;
    padding: 0 15px;
    flex-wrap: nowrap;
    margin: 7px 0;
    line-height: 30px;
}

.input-uni-icon {
    line-height: 30px;
}
.nav-bar-input {
    height: 30px;
    line-height: 30px;
    /* #ifdef APP-PLUS-NVUE */
    width: 370rpx;
    /* #endif */
    padding: 0 5px;
    font-size: 28rpx;
    background-color: #f8f8f8;
}

3.10.2 修改成案例效果

<view class="input-view">
    <uni-icons class="input-uni-icon" type="search" size="22" color="#666666" />
    <input confirm-type="search" disabled class="nav-bar-input" type="text" placeholder="搜索">
</view>

3.11 導(dǎo)航欄兼容性,調(diào)節(jié)編譯

<!-- #ifdef MP -->
<uni-nav-bar :fixed="false" color="#333333" background-color="#FFFFFF" left-icon="scan" right-text="登錄">
</uni-nav-bar>
<!-- #endif -->

步驟4 完成輪播圖

4.1獲取輪播圖的數(shù)據(jù)

data(){
    return {
        carouselList: []
    }
},
loadData() {
    this.getAdBanners()
},
methods: {
    async getAdBanners(){
        var [error, res] = await uni.request({
            url: 'https://a0e83064-1d70-4acb-8b07-43b6d3ef7b23.bspapp.com/http/adBanners'
        })
        console.log('res', res)
        this.carouselList = res.data.data.list
    }
}

4.2使用輪播組件

<view class="carousel-section">
    <swiper>
        <swiper-item v-for="(item, index) in carouselList" :key="index">
            <image :src="item.goodsPic" />
        </swiper-item>
    </swiper>
</view>

4.3完善輪播組件

  • circular增加循環(huán)
  • 增加.carousel樣式
<view class="carousel-section">
    <swiper class="carousel" circular>
        <swiper-item v-for="(item, index) in carouselList" :key="index" class="carousel-item">
            <image :src="item.goodsPic"/>
        </swiper-item>
    </swiper>
</view>
.carousel {
    width: 100%;
    height: 373rpx;

    image {
        width: 100%;
        height: 100%;
    }
}

4.4添加自定義輪播指示器,基礎(chǔ)樣式

  • 修改index.vue
<view class="carousel-section">
    <swiper class="carousel" circular>
        <swiper-item v-for="(item, index) in carouselList" :key="index" class="carousel-item">
            <image :src="item.goodsPic"/>
        </swiper-item>
    </swiper>
    <view class="swiper-dots">
        <text class="num">{{ swiperCurrent + 1 }}</text>
        <text class="sign">/</text>
        <text class="num">{{ swiperLength }}</text>
    </view>
</view>
data() {
    return {
        swiperCurrent: 0,
        swiperLength: 0,
    }
},
methods: {
    async getAdBanners(){
        var [error, res] = await uni.request({
            url: 'https://a0e83064-1d70-4acb-8b07-43b6d3ef7b23.bspapp.com/http/adBanners'
        })
        console.log('res', res)
        this.carouselList = res.data.data.list
        this.swiperLength = this.carouselList.length
    }
}
.carousel-section{
    position: relative;
}   
.swiper-dots {
    display: flex;
    position: absolute;
    left: 45rpx;
    bottom: 40rpx;
    width: 72rpx;
    height: 36rpx;
    background-image: url(https://itcast-1255651667.cos.ap-chengdu.myqcloud.com/20200421213325.png);
    background-size: 100% 100%;

    .num {
        width: 36rpx;
        height: 36rpx;
        border-radius: 50px;
        font-size: 24rpx;
        color: #fff;
        text-align: center;
        line-height: 36rpx;
    }

    .sign {
        position: absolute;
        top: 0;
        left: 50%;
        line-height: 36rpx;
        font-size: 12rpx;
        color: #fff;
        transform: translateX(-50%);
    }
}

4.5輪播事件添加

  • 切換輪播,改變指示器的數(shù)字顯示
<swiper class="carousel" circular @change="swiperChange">
</swiper>
methods: {
    swiperChange(e) {
        const index = e.detail.current;
        this.swiperCurrent = index;
    },
}

步驟5 玩分類欄目

5.1基本布局

  • 使用emmet寫標(biāo)簽
    view.cate-section>(view.cate-item>image[src=""]+text)*10
  • 打開素材/圖片/圖片.md
<view class="cate-section">
    <view class="cate-item">
        <image src="https://itcast-1255651667.cos.ap-chengdu.myqcloud.com/20200409224144.png"></image>
        <text></text>
    </view>
    <view class="cate-item">
        <image src="https://itcast-1255651667.cos.ap-chengdu.myqcloud.com/20200409224613.png"></image>
        <text></text>
    </view>
    <view class="cate-item">
        <image src="https://itcast-1255651667.cos.ap-chengdu.myqcloud.com/20200409224724.png"></image>
        <text></text>
    </view>
    <view class="cate-item">
        <image src="https://itcast-1255651667.cos.ap-chengdu.myqcloud.com/20200409224803.png"></image>
        <text></text>
    </view>
    <view class="cate-item">
        <image src="https://itcast-1255651667.cos.ap-chengdu.myqcloud.com/20200409224954.png"></image>
        <text></text>
    </view>
    <!-- 第二行 -->
    <view class="cate-item">
        <image src="https://itcast-1255651667.cos.ap-chengdu.myqcloud.com/20200409225107.png"></image>
        <text></text>
    </view>
    <view class="cate-item">
        <image src="https://itcast-1255651667.cos.ap-chengdu.myqcloud.com/20200409225233.png"></image>
        <text></text>
    </view>
    <view class="cate-item">
        <image src="https://itcast-1255651667.cos.ap-chengdu.myqcloud.com/20200409225318.png"></image>
        <text></text>
    </view>
    <view class="cate-item">
        <image src="https://itcast-1255651667.cos.ap-chengdu.myqcloud.com/20200409225348.png"></image>
        <text></text>
    </view>
    <view class="cate-item">
        <image src="https://itcast-1255651667.cos.ap-chengdu.myqcloud.com/20200409225437.png"></image>
        <text></text>
    </view>
</view>
/* 分類 */
.cate-section {
    display: flex;
    justify-content: space-around;
    align-items: center;
    flex-wrap: wrap;

    .cate-item {
        width: 20%;
    }

    image {
        width: 85rpx;
        height: 85rpx;
        border-radius: 50%;
    }
}

5.2 美化圖標(biāo)樣式

  • 添加圖標(biāo)文字,美好
<text>服飾</text>
<text>數(shù)碼</text>
<text>美妝</text>
<text>手機(jī)</text>
<text>旅行</text>
<text>生鮮</text>
<text>超市</text>
<text>物流</text>
<text>進(jìn)口</text>
<text>繳費(fèi)</text>
.cate-section {
    margin: 18rpx 17rpx;
    padding-top: 30rpx;
    background: #fff;
    border-radius: 10rpx;   
    .cate-item {
        display: flex;
        flex-direction: column;
        align-items: center;
        font-size: 26rpx;
        margin-bottom: 30rpx;
    }
    image {
        margin-bottom: 10rpx;
    }
}

步驟6 實(shí)現(xiàn)猜你喜歡

6.1創(chuàng)建guess-like-goods.vue

6.2引入組件guess-like-goods

修改index.vue

<guess-like-goods></guess-like-goods>

6.3發(fā)送請求

    import guessLikeGoods from '@/components/guess-like-goods.vue'
export default {    
    components: {
        guessLikeGoods
    },
    data() {
        return {
            guessLikeGoodsList: []
        };
    },
    created(){
        this.getGuessLikeGoods()
    },
    methods: {
        async getGuessLikeGoods(){
            var [error, res] = await uni.request({
                url:'https://a0e83064-1d70-4acb-8b07-43b6d3ef7b23.bspapp.com/http/guessLike'
            })
            this.guessLikeGoodsList = res.data.data
        }
    }
}

6.4寫的標(biāo)簽,主要布局

  • 編輯guess-like-goods.vue
<!-- 猜你喜歡 -->
<view class="f-header">
    <h3>─<span>猜你喜歡</span>─</h3>
    <view class="guess-section">
        <view v-for="(item, index) in guessLikeGoodsList" :key="index" class="guess-item">
            <view class="image-wrapper">
                <image :src="item.goodsPic" mode="aspectFill"></image>
            </view>
        </view>
    </view>
</view>
/* 猜你喜歡 */
.f-header {
    margin: 18rpx 17rpx;
    h3 {
        font-weight: bold;
        padding: 6rpx 0 24rpx;
        font-size: 30rpx;
        text-align: center;
        span {
            color: #FF7443;
        }
    }
}
.guess-section {
    display: flex;
    flex-wrap: wrap;
    .guess-item {
        display: flex;
        position: relative;
        flex-direction: column;
        width: 48%;
        margin-bottom: 20rpx;
        background-color: #fff;
        border-radius: 10rpx;
        padding-bottom: 20rpx;
        &:nth-child(2n + 1) {
            margin-right: 4%;
        }
    }
    .image-wrapper {
        width: 100%;
        height: 330rpx;
        border-radius: 3px;
        image {
            width: 100%;
            height: 100%;
        }
    }
}

6.5寫的標(biāo)簽,其他布局

  • 編輯guess-like-goods.vue
<view v-for="(item, index) in guessLikeGoodsList" :key="index" class="guess-item" @click="navToDetailPage(item)">
    <view class="image-wrapper">
        <image :src="item.goodsPic" mode="aspectFill"></image>
    </view>
    <text class="title">{{item.goodsTitle}}</text>
    <text class="price">¥{{item.goodsPrice}}</text>
    
    <!-- <image class="cart" src="../static/tab-cart.png"></image> -->
</view>
.guess-section {
    .title {
        padding-left: 10rpx;
        margin: 20rpx 0;
        font-size: 25rpx;
        color: #262626;
        
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        display: block;
    }
    .price {
        padding-left: 10rpx;
        font-size: 24rpx;
        color: #DC332A;
        line-height: 1;
    }   
}

6.6寫標(biāo)簽,加入購物車圖標(biāo)

  • 編輯App.vue
<style>
    @import url("static/iconfont/iconfont.css");
</style>
  • 編輯guess-like-goods.vue
<view v-for="(item, index) in guessLikeGoodsList" :key="index" class="guess-item" @click="navToDetailPage(item)">
    <view class="cart iconfont icon-gouwuche"></view>
</view>
.guess-section {
    .guess-item {
        .cart {
            position: absolute;
            bottom: 10rpx;
            right: 10rpx;
            font-size: 40rpx
        }   
    }
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市多柑,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 221,576評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件嫂易,死亡現(xiàn)場離奇詭異统翩,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)匙姜,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,515評論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來冯痢,“玉大人氮昧,你說我怎么就攤上這事或详。” “怎么了郭计?”我有些...
    開封第一講書人閱讀 168,017評論 0 360
  • 文/不壞的土叔 我叫張陵霸琴,是天一觀的道長。 經(jīng)常有香客問我昭伸,道長梧乘,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 59,626評論 1 296
  • 正文 為了忘掉前任庐杨,我火速辦了婚禮选调,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘灵份。我一直安慰自己仁堪,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 68,625評論 6 397
  • 文/花漫 我一把揭開白布填渠。 她就那樣靜靜地躺著弦聂,像睡著了一般。 火紅的嫁衣襯著肌膚如雪氛什。 梳的紋絲不亂的頭發(fā)上莺葫,一...
    開封第一講書人閱讀 52,255評論 1 308
  • 那天,我揣著相機(jī)與錄音枪眉,去河邊找鬼捺檬。 笑死,一個(gè)胖子當(dāng)著我的面吹牛贸铜,可吹牛的內(nèi)容都是我干的堡纬。 我是一名探鬼主播,決...
    沈念sama閱讀 40,825評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼蒿秦,長吁一口氣:“原來是場噩夢啊……” “哼烤镐!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起渤早,我...
    開封第一講書人閱讀 39,729評論 0 276
  • 序言:老撾萬榮一對情侶失蹤职车,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后鹊杖,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,271評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡扛芽,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,363評論 3 340
  • 正文 我和宋清朗相戀三年骂蓖,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片川尖。...
    茶點(diǎn)故事閱讀 40,498評論 1 352
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡登下,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情被芳,我是刑警寧澤缰贝,帶...
    沈念sama閱讀 36,183評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站畔濒,受9級特大地震影響剩晴,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜侵状,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,867評論 3 333
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧预侯,春花似錦翻具、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,338評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至蹋凝,卻和暖如春闯团,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背仙粱。 一陣腳步聲響...
    開封第一講書人閱讀 33,458評論 1 272
  • 我被黑心中介騙來泰國打工房交, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人伐割。 一個(gè)月前我還...
    沈念sama閱讀 48,906評論 3 376
  • 正文 我出身青樓候味,卻偏偏與公主長得像,于是被迫代替她去往敵國和親隔心。 傳聞我的和親對象是個(gè)殘疾皇子白群,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,507評論 2 359

推薦閱讀更多精彩內(nèi)容