完成入门案例
步骤1.下载编辑器
1.1 创建项目hello-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.默认组件
2.1 修改index.vue
<view class="content">
<view>块1</view>
<view>块2</view>
<view><text>字1</text><text>字2</text></view>
</view>
步骤3.运行程序
- 打开浏览器
- 打开小程序
- 打开app
文档见快速上手-运行uni-app
步骤4.打印日志
onLoad() {
console.log('hello')
},
步骤5.使用扩展组件的插件
- 使用Icons图标
修改
<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调用
<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 创建项目shop
1.2 清空pages/index/index.vue
- template中删除多余的标签,和修改class为container
- script data属性清空.添加onload生命周期
- style 清空内容,添加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 打开参考网站
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 完成导航栏
3.1 打开配置文档 ,打开h5的配置文档,打开导航栏的配置文档
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 打开字体图标
拷贝字体图标到static文件夹
3.5 修改page.json,添加左侧图标
"titleNView": {
"buttons": [{
"float": "left",
"fontSrc": "/static/iconfont/iconfont.ttf",
"text": ""
}]
}
3.6 修改page.json,添加右侧图标
"buttons": [{
"float": "left",
"fontSrc": "/static/iconfont/iconfont.ttf",
"text": ""
},{
"text": "登录",
"fontSize": "12px",
"redDot": true
}]
3.7 导航栏兼容性,app端
- 添加app-plus,复制h5的属性值
- 打开模拟器,运行
"app-plus": {
}
3.8 导航栏兼容性,app端,修改图标内容
"buttons": [{
"float": "left",
"fontSrc": "/static/iconfont/iconfont.ttf",
"text": "\e618;"
}]
3.9 导航栏兼容性,小程序端,引入第三方插件
- 新建cart,user页面
- 搜索插件
NavBar 导航栏
,并安装它 - 修改
index.vue
<template>
<view class="container">
<uni-nav-bar left-icon="back" left-text="返回" right-text="菜单" title="导航栏组件"></uni-nav-bar>
</view>
</template>
<script>
import uniNavBar from "@/components/uni-nav-bar/uni-nav-bar.vue"
export default {
components: {uniNavBar},
}
</script>
3.10 导航栏兼容性,小程序端,美化导航栏
- 导入
navBar的案例
- 修改
index.vue
3.10.1 复制案例代码
<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="输入搜索关键词" @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 导航栏兼容性,调节编译
- 打开文档,条件编译
<!-- #ifdef MP -->
<uni-nav-bar :fixed="false" color="#333333" background-color="#FFFFFF" left-icon="scan" right-text="登录">
</uni-nav-bar>
<!-- #endif -->
步骤4 完成轮播图
4.1获取轮播图的数据
- 打开接口文档
- 打开发起请求文档
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使用轮播组件
- 打开轮播的组件文档
- 修改index.vue
<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增加循环
- 增加.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添加自定义轮播指示器,基础样式
- 修改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轮播事件添加
- 切换轮播,改变指示器的数字显示
<swiper class="carousel" circular @change="swiperChange">
</swiper>
methods: {
swiperChange(e) {
const index = e.detail.current;
this.swiperCurrent = index;
},
}
步骤5 玩分类栏目
5.1基本布局
- 使用emmet写标签
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 美化图标样式
- 添加图标文字,美好
<text>服饰</text>
<text>数码</text>
<text>美妆</text>
<text>手机</text>
<text>旅行</text>
<text>生鲜</text>
<text>超市</text>
<text>物流</text>
<text>进口</text>
<text>缴费</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 实现猜你喜欢
6.1创建guess-like-goods.vue
6.2引入组件guess-like-goods
修改index.vue
<guess-like-goods></guess-like-goods>
6.3发送请求
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写的标签,主要布局
- 编辑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写的标签,其他布局
- 编辑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写标签,加入购物车图标
- 编辑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
}
}
}