写一个微信小程序新闻系统
时间: 2024-04-29 13:20:40 浏览: 74
好的,以下是一个简单的微信小程序新闻系统的代码示例:
1. 在app.json中配置tabBar
```
"tabBar": {
"color": "#999",
"selectedColor": "#07c160",
"backgroundColor": "#fff",
"list": [
{
"pagePath": "pages/newsList/newsList",
"text": "新闻",
"iconPath": "images/news.png",
"selectedIconPath": "images/news-active.png"
},
{
"pagePath": "pages/mine/mine",
"text": "我的",
"iconPath": "images/mine.png",
"selectedIconPath": "images/mine-active.png"
}
]
}
```
2. 在pages目录下创建newsList和newsDetail两个页面
newsList页面代码示例:
```
<view class="news-list">
<view wx:for="{{newsList}}" wx:key="index" class="news-item" bindtap="goToDetail">
<image src="{{item.image}}" class="news-image"></image>
<view class="news-info">
<view class="news-title">{{item.title}}</view>
<view class="news-time">{{item.time}}</view>
</view>
</view>
</view>
```
newsDetail页面代码示例:
```
<view class="news-detail">
<image src="{{newsDetail.image}}" class="news-image"></image>
<view class="news-title">{{newsDetail.title}}</view>
<view class="news-content">{{newsDetail.content}}</view>
</view>
```
3. 在pages目录下创建newsList和newsDetail两个页面的js文件
newsList.js文件代码示例:
```
Page({
data: {
newsList: [
{
title: '新闻标题1',
image: 'http://image.com/news1.jpg',
time: '2022-01-01',
content: '新闻内容1'
},
{
title: '新闻标题2',
image: 'http://image.com/news2.jpg',
time: '2022-01-02',
content: '新闻内容2'
}
]
},
goToDetail: function(event) {
var index = event.currentTarget.dataset.index;
var newsItem = JSON.stringify(this.data.newsList[index]);
wx.navigateTo({
url: '../newsDetail/newsDetail?newsItem=' + newsItem
})
}
})
```
newsDetail.js文件代码示例:
```
Page({
data: {
newsDetail: {}
},
onLoad: function(options) {
var newsItem = JSON.parse(options.newsItem);
this.setData({
newsDetail: newsItem
})
}
})
```
4. 在app.js中配置全局样式
```
App({
onLaunch: function () {
// ...
},
globalData: {
// ...
}
})
// app.wxss
/* 全局样式 */
.news-list {
padding: 10px;
}
.news-item {
display: flex;
margin-bottom: 10px;
background-color: #fff;
box-shadow: 0 1px 5px rgba(0, 0, 0, 0.1);
border-radius: 5px;
overflow: hidden;
}
.news-image {
width: 120px;
height: 80px;
object-fit: cover;
}
.news-info {
flex: 1;
display: flex;
flex-direction: column;
justify-content: center;
padding: 10px;
}
.news-title {
font-size: 16px;
font-weight: bold;
margin-bottom: 5px;
}
.news-time {
font-size: 14px;
color: #999;
}
.news-detail {
padding: 10px;
}
.news-title {
font-size: 18px;
font-weight: bold;
margin-bottom: 10px;
}
.news-content {
font-size: 16px;
line-height: 1.5;
text-indent: 2em;
}
```
这样就完成了一个简单的微信小程序新闻系统,用户可以在newsList页面看到新闻列表,点击某个新闻后会跳转到newsDetail页面查看新闻详情。
阅读全文
相关推荐
















