【uni-app系列】uni-app从0到1开发实例


一、准备工作

1.接口

本实例实现新闻列表和新闻详情功能,使用到如下接口:
列表接口:
https://unidemo.dcloud.net.cn/api/news
详情接口:
https://unidemo.dcloud.net.cn/api/news/36kr/ + id (id 为新闻id,上个页面传过来的)

2.代码块

开发中使用到 uListMedia 代码块,代码块设置如下:
在这里插入图片描述
自定义代码块:
在这里插入图片描述

"uListMedia": {
    "body": [
		"<view class=\"uni-list\">",
			 "\t<view class=\"uni-list-cell\" hover-class=\"uni-list-cell-hover\" v-for=\"(item,index) in list\" :key=\"index\">",
				  "\t\t<view class=\"uni-media-list\">",
					  "\t\t\t<image class=\"uni-media-list-logo\" :src=\"item.img\"></image>",
					  "\t\t\t<view class=\"uni-media-list-body\">",
						  "\t\t\t\t<view class=\"uni-media-list-text-top\">{{item.title}}</view>",
						  "\t\t\t\t<view class=\"uni-media-list-text-bottom uni-ellipsis\">{{item.content}}</view>",
					  "\t\t\t</view>",
				  "\t\t</view>",
			 "\t</view>",
		 "</view>"
    ],
    "prefix": "ulistmedia",
    "project": "uni-app",
    "scope": "source.vue.html"
}

二、创建项目

1.创建默认模板项目 news

创建新项目 news,选择默认模板,该项目为要开发的实例项目:
在这里插入图片描述

2.创建 Hello uni-app模板项目 hello-uniapp

创建新项目 hello-uniapp,选择默认模板,用于将相关的 js、css、ttf 等文件拷贝至实例项目:
在这里插入图片描述

三、引入样式文件

将 hello-uniapp 的 common 拷贝至 news :
在这里插入图片描述
news 项目的 App.vue 引入 ./common/uni.css:
在这里插入图片描述
将 hello-uniapp 的 static/uni.ttf 拷贝至 news :
在这里插入图片描述

四、修改入口页

修改入口页 news/pages/index/index.vue:
在这里插入图片描述

<template>
	<view class="content">
		<view class="uni-list">
			<view class="uni-list-cell" hover-class="uni-list-cell-hover" v-for="(item,index) in news" :key="index" @tap="openinfo" :data-newsid="item.post_id">
				<view class="uni-media-list">
					<image class="uni-media-list-logo" :src="item.author_avatar"></image>
					<view class="uni-media-list-body">
						<view class="uni-media-list-text-top">{{item.title}}</view>
						<view class="uni-media-list-text-bottom uni-ellipsis">{{item.created_at}}</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				news: []
			}
		},
		onLoad() {
			uni.showLoading({
				title: '加载中...',
				mask: false
			});
			uni.request({
				url: 'https://unidemo.dcloud.net.cn/api/news',
				method: 'GET',
				data: {},
				success: res => {
					console.log(res);
					this.news = res.data;
					uni.hideLoading();
				},
				fail: () => {},
				complete: () => {}
			});

		},
		methods: {
			openinfo(e) {
				console.log(e);
				var newid = e.currentTarget.dataset.newsid;
				console.log(newid);
				uni.navigateTo({
					url: '../info/info?newsid=' + newid
				});
			}
		}
	}
</script>

<style>
.uni-media-list-body{height: auto;}
.uni-media-list-text-top{line-height: 1.6em;}
</style>

五、创建详情页

创建详情页 info/info.vue:

<template>
	<view class="content">
		<view class="title">{{title}}</view>
		<view class="art-content">
			<rich-text class="richText" :nodes="strings"></rich-text>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				title: '',
				strings: ''
			}
		},
		onLoad(e) {
			console.log(e);
			uni.request({
				url: 'https://unidemo.dcloud.net.cn/api/news/36kr/' + e.newsid,
				method: 'GET',
				data: {},
				success: res => {
					console.log(res);
					this.title = res.data.title;
					this.strings = res.data.content;
				},
				fail: () => {},
				complete: () => {}
			});
		},
		methods: {
			
		}
	}
</script>

<style>
.content{padding: 10upx 2%; width: 96%; flex-wrap: wrap;}
.title{line-height: 2em; font-weight: 700; font-size: 38upx;}
.art-content{line-height: 2em;}
</style>

六、运行

运行效果
列表页:
在这里插入图片描述
详情页:
在这里插入图片描述

七、调试小技巧

在 pages.json 加入 condition 配置,可以直达某个页面进行调试:

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

运行:
在这里插入图片描述

  • 0
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

奔跑吧邓邓子

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

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

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

打赏作者

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

抵扣说明:

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

余额充值