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

2023-11-17


一、准备工作

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函数里面得到。
			}
		]
	}

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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【uni-app系列】uni-app从0到1开发实例 的相关文章

随机推荐

  • 面试官偷偷给到45k*16薪,堪称面试风向标!

    前天加完班 回家路上翻了下粉丝群 发现群里最近在疯传一份叫 前端offer收割机养成指南 的资料 本来感觉这个title看起来有点离谱 结果没想到仔细一看 这份资料竟然真的有点东西 内容收纳的很全 而且融合了很多今年的新玩意 据我所知有人靠
  • HTML 整体缩放

    最近用到web 控件加载网页需要缩放问题 由于控件比较旧 所以只能从html 入手 html 页面缩放主要有两种 IE 可使用 CSS body zoom 1 2 或者微软相关的控件支持 包括BCB 其它 浏览器可使用 body trans
  • 指针字符串 与 const char * 即const * char 的详细使用讲解

    指针字符串的使用问题 一 直接定义字符串指针的使用注意事项 定义字符串指针的时候 const char 和字符串本身相同 就不会出现警告 const char char const 作用 const char p 表示的是指针p指向的数值不
  • Kubernetes CoreDNS 状态是 CrashLoopBackOff 报错

    查看状态的时候 遇见coredns出现crashlookbackoff 首先我们来进行排错 不管是什么原因 查看coredns的详细信息 以及logs root k8s master coredns kubectl get pod svc
  • Shell 中 &>/dev/null 和 >/dev/null 2>&1

    下面 咱们一起来看看这个命令操作涉及到的知识点 这其实涉及到三部分的内容 如下图 1 文件描述符 linux shell脚本攻略 的描述 文件描述符是与文件输入 输出关联的整数 它们用来跟踪已打开的文件 最常见的文件描述符是 stidin
  • linux cuda安装目录,Ubuntu 11.10上安装CUDA开发环境的方法及命令

    这篇文章全部内容在我的ThinkPad W520 Ubuntu 11 10 x64位上测试通过 但不代表这篇文章的内容适合你 任何根据这篇文章操作产生的后果 这篇文章作者cheungmine概不负责 英文参考文档 http develope
  • 华为OD机试 C++ [周末爬山]

    题目 小明打算周末去爬山 有一份山的地图 上面用数字表示山的高度 0表示平地 1至9表示不同的山峰高度 小明每次移动只能上下左右移动一格 并且山峰高度差不能超过k 现在他从地图的左上角出发 你能帮他找出他能爬到的最高的山峰是多高吗 还有 他
  • Android数据存储 —— SharedPreferences

    SharedPreferences以键值对的形式存储数据 支持几种基本数据类型 boolean float int long String 一般存储配置信息 它保存的数据时持久化的 即使应用被关掉也不会丢失 存储格式为 xml 一般放在内部
  • 三十二、java版 SpringCloud分布式微服务云架构之Java LinkedList

    Java LinkedList 链表 Linked list 是一种常见的基础数据结构 是一种线性表 但是并不会按线性的顺序存储数据 而是在每一个节点里存到下一个节点的地址 链表可分为单向链表和双向链表 一个单向链表包含两个值 当前节点的值
  • IO流作业

    io 文件操作 in 读取 out 写出 java io File 常用的三个构造方法 File String pathname 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例 public class Demo01 pu
  • 目标检测方法概述(一)

    目标检测的问题 就是在给定的图片中找到物体的位置 并标明物体的类别 通过卷积神经网络CNN 我们可以完成图像识别 即分类任务 然后我们需要通过一些额外的功能完成定位任务 即找到上图中方框在图像中的位置 x y w
  • Webshell不出网方案之正向socks代理reGeorg+Proxifier使用

    参考博客 https blog csdn net God XiangYu article details 100126207 Regeorg地址 https github com sensepost reGeorg Proxifier地址
  • 从 Java 到 Go:构建社交网络平台后端的过渡之旅

    目录 目录 1 项目概述 2 环境准备 2 1 安装依赖 3 从 Java 到 Go 的基础知识
  • 屏幕分辨率dpi解析(adb 调试查看)

    author daisy skye的博客 CSDN博客 嵌入式 Qt Linux领域博主 ro sf lcd density属性指定了这个机型使用的dpi是多少 dpi全称是dots per inch 对角线每英寸的像素点的个数 密度 ld
  • [从零开始学习FPGA编程-21]:进阶篇 - 架构 - VerilogHDL编码规范

    作者主页 文火冰糖的硅基工坊 文火冰糖 王文兵 的博客 文火冰糖的硅基工坊 CSDN博客 本文网址 目录 前言 第1章 标识符的编码规范 1 1 模块名与文件名
  • excel相乘再相加_(excel 两列相乘再相加)excel表格两列数据乘积

    Excel求两列的乘积用什么公式啊 1 Excel 2016电子表格应用程序 所示点击屏幕右侧新建空作簿 2 选中销售额单元格数域 如图所示 选择 公式 函数库 功能区 点击 插入函数 图标 3 唤出 插入函数 对话框 点击 搜索函数或选择
  • 2020年团体程序设计天梯赛-总决赛 L2-2 口罩发放

    L2 2 口罩发放 25分 输入格式 输出格式 输入样例 输出样例 样例解释 题解 L2 2 口罩发放 25分 为了抗击来势汹汹的 COVID19 新型冠状病毒 全国各地均启动了各项措施控制疫情发展 其中一个重要的环节是口罩的发放 某市出于
  • thrift文件服务器,Apache

    The Apache Thrift software framework for scalable cross language services development combines a software stack with a c
  • python机器学习之十一 numpy库之矩阵(matrix)

    本文主要介绍numpy库中的矩阵 矩阵的创建 常见的矩阵运算 矩阵 列表 数组之间的转换 矩阵的创建 语法格式 numpy matrix data dtype data 数据 dtype 数据类型 同数组 如 m2 np matrix 1
  • 【uni-app系列】uni-app从0到1开发实例

    目录 一 准备工作 1 接口 2 代码块 二 创建项目 1 创建默认模板项目 news 2 创建 Hello uni app模板项目 hello uniapp 三 引入样式文件 四 修改入口页 五 创建详情页 六 运行 七 调试小技巧 一