针对您提到的uni-app个人全职工作,并拥有线上项目案例的背景,以下是一个简要的代码案例展示,旨在体现uni-app在实际开发中的应用能力和实践成果。请注意,由于篇幅限制,这里仅提供一个基础且简化的示例,以展示uni-app的核心功能和组件使用。
项目背景
假设我们正在开发一个简易的“新闻阅读”应用,该应用允许用户浏览新闻列表、查看新闻详情,并具备基本的导航功能。
代码示例
1. 项目结构
uni-news-app/
├── pages/
│ ├── index/
│ │ ├── index.vue
│ ├── news-detail/
│ ├── news-detail.vue
├── App.vue
├── main.js
├── pages.json
├── manifest.json
└── uni.scss
2. pages/index/index.vue
- 新闻列表页
<template>
<view class="container">
<list>
<list-item v-for="(news, index) in newsList" :key="index" @click="navigateToDetail(news.id)">
{{ news.title }}
</list-item>
</list>
</view>
</template>
<script>
export default {
data() {
return {
newsList: [
{ id: 1, title: '新闻标题1' },
{ id: 2, title: '新闻标题2' },
// 更多新闻...
]
};
},
methods: {
navigateToDetail(id) {
uni.navigateTo({
url: `/pages/news-detail/news-detail?id=${id}`
});
}
}
};
</script>
<style>
/* 样式省略 */
</style>
3. pages/news-detail/news-detail.vue
- 新闻详情页
<template>
<view class="container">
<text>{{ news.title }}</text>
<text>{{ news.content }}</text>
</view>
</template>
<script>
export default {
data() {
return {
news: {}
};
},
onLoad(options) {
const newsId = parseInt(options.id);
// 模拟从服务器获取新闻详情(实际开发中应为API请求)
this.news = this.getNewsById(newsId);
},
methods: {
getNewsById(id) {
const mockNews = [
{ id: 1, title: '新闻标题1', content: '新闻内容1' },
{ id: 2, title: '新闻标题2', content: '新闻内容2' },
// 更多新闻...
];
return mockNews.find(news => news.id === id);
}
}
};
</script>
<style>
/* 样式省略 */
</style>
以上代码展示了一个简单的新闻阅读应用的基本框架,包括新闻列表页和新闻详情页。通过uni-app
的页面跳转和组件绑定功能,实现了基本的导航和数据展示。此示例仅为基础演示,实际项目中还需考虑数据持久化、网络请求、用户交互优化等多方面内容。