微信小程序在一个页面设置一个加号图标,点击它可以写标题,内容和添加图片或视频,有两个按钮一个是保存,点保存后内容以列表保存在该页面,点击它,内容将完整展现,另一个按钮会把内容发布到另一个页面且具有保存按钮的功能。请问代码怎么写
时间: 2024-02-16 09:04:48 浏览: 111
首先,在wxml文件中可以这么写:
```html
<view class="container">
<view class="header">
<view class="icon-box" bindtap="addNote">
<image class="icon" src="/images/add.png"></image>
</view>
<view class="title">笔记列表</view>
</view>
<scroll-view class="note-list" scroll-y="true">
<view wx:for="{{noteList}}" wx:key="noteId" class="note-item" bindtap="viewNote">
<view class="note-title">{{item.title}}</view>
<view class="note-content">{{item.content}}</view>
</view>
</scroll-view>
</view>
```
在js文件中,可以这样写:
```javascript
Page({
data: {
noteList: [],
},
// 点击加号图标添加笔记
addNote: function() {
wx.navigateTo({
url: '/pages/note-edit/note-edit'
})
},
// 点击笔记查看详情
viewNote: function(e) {
wx.navigateTo({
url: '/pages/note-detail/note-detail?noteId=' + e.currentTarget.dataset.noteId
})
},
// 生命周期函数--监听页面显示
onShow: function () {
// 从本地存储中获取笔记列表
var noteList = wx.getStorageSync('noteList') || [];
this.setData({
noteList: noteList
});
},
})
```
在wxml文件中,可以这样写:
```html
<view class="container">
<view class="header">
<view class="title">编辑笔记</view>
<view class="save-btn" bindtap="saveNote">保存</view>
</view>
<view class="form">
<input type="text" class="title-input" placeholder="请输入标题" bindinput="inputTitle" value="{{noteTitle}}"></input>
<textarea class="content-input" placeholder="请输入内容" bindinput="inputContent" value="{{noteContent}}"></textarea>
</view>
</view>
```
在js文件中,可以这样写:
```javascript
Page({
data: {
noteTitle: '',
noteContent: '',
},
// 输入笔记标题
inputTitle: function(e) {
this.setData({
noteTitle: e.detail.value
})
},
// 输入笔记内容
inputContent: function(e) {
this.setData({
noteContent: e.detail.value
})
},
// 点击保存按钮保存笔记
saveNote: function() {
var noteList = wx.getStorageSync('noteList') || [];
var noteId = noteList.length + 1;
var noteTitle = this.data.noteTitle.trim();
var noteContent = this.data.noteContent.trim();
if (noteTitle === '' || noteContent === '') {
wx.showToast({
title: '标题和内容不能为空',
icon: 'none'
})
return;
}
var note = {
noteId: noteId,
title: noteTitle,
content: noteContent
}
noteList.unshift(note);
wx.setStorageSync('noteList', noteList);
wx.navigateBack();
},
})
```
在wxml文件中,可以这样写:
```html
<view class="container">
<view class="header">
<view class="title">{{note.title}}</view>
<view class="edit-btn" bindtap="editNote">编辑</view>
</view>
<view class="content">{{note.content}}</view>
</view>
```
在js文件中,可以这样写:
```javascript
Page({
data: {
note: {}
},
// 点击编辑按钮进入编辑笔记页面
editNote: function() {
wx.navigateTo({
url: '/pages/note-edit/note-edit?noteId=' + this.data.note.noteId
})
},
// 生命周期函数--监听页面加载
onLoad: function (options) {
var noteId = options.noteId;
var noteList = wx.getStorageSync('noteList') || [];
var note = noteList.find(function(item) {
return item.noteId == noteId;
})
this.setData({
note: note
})
},
})
```
这是一个简单的代码示例,仅供参考。需要注意的是,此代码示例仅包含基本功能,实际使用时还需要根据需求进行修改和完善。