请选择 进入手机版 | 继续访问电脑版
帖子
帖子
用户
博客
课程
12下一页
返回列表 发新帖
显示全部楼层
92
帖子
2
勋章
717
Y币

[App引擎] 遍历出图片,点击图片删除自己

[复制链接]
发表于 2022-6-11 21:43:27
apicloud3 不太会用,请教个小白问题,v-for遍历出图片,我想点击图片删除当前图片,delete方法应该怎么写呢?希望高手指点一下,感激不尽<img class="images" v-for="img in images" src={img} @click="delete" />

delete() {

}




380
帖子
4
勋章
6
Y币
改变数据源,即for循环的data

https://www.**.****.**/itweb666/articles/15637119.html
直接数组  images.splice(_index,1);    index为当前删除角标  1为删除后一个
1
帖子
1
勋章
1万+
Y币
v-for 里加上index 然后delete方法里传index 然后images里移除对应的就好
92
帖子
2
勋章
717
Y币
<img class="images" v-for="(img,_index) in images" src={img} @click="delete" />


del(_index){


}
勇可可 · 2022-6-12 10:32直接数组  images.splice(_index,1);    index为当前删除角标  1为删除后一个
92
帖子
2
勋章
717
Y币
<img class="images" v-for="(img,_index) in images" src={img} @click="delete" />


del(_index){
                this.data.images.splice(_index,1);
                },

图片可以正常循环出来,但是点击删除提示images未定义。
[ERROR] dispatch event failed. TypeError: Cannot read property 'images' of undefined
这个是什么原因呢?
勇可可 · 2022-6-12 10:32直接数组  images.splice(_index,1);    index为当前删除角标  1为删除后一个
完整的代码发一下
yhbhpu · 2022-6-15 19:35del(_index){
                this.data.images.splice(_index,1);
                },
92
帖子
2
勋章
717
Y币
就是仿小红书的帖子发布页代码,我增加了一个删除函数

<template>
        <view class="page">
                <a-nav-bar title="" left-arrow hide-line>
                        <template _slot="right">
                                <a-icon name="prompt" size="18" />
                        </template>
                </a-nav-bar>

                <scroll-view style="flex:1;">
                        <scroll-view scroll-x class="publish__images">

                                <img class="publish__images-img" mode="aspectFill" v-for="(img,_index) in postData.images" src={img} @click="del" />
                                <img class="publish__images-img" src="../../image/icon_add_pic.png" @click="choosePic" />
                        </scroll-view>

                        <view class="publish__fields">

                                <view class="publish__cell">
                                        <input v-model="postData.title" type="text" class="publish__title" placeholder="填写标题会有更多的赞哦~" />
                                </view>

                                <view class="publish__cell">
                                        <textarea v-model="postData.content" class="publish__content" placeholder="添加正文"></textarea>
                                </view>


                                <view class="publish__cell" style="flex-flow:row;" @click="chooseTopic">
                                        <a-icon name="topic-filling" size="12" color="#000" />
                                        <text class="publish__label-text">
                                                参与话题
                                        </text>

                                        <text class="publish__label-value" v-if="postData.topic.id">
                                                {postData.topic.name}
                                        </text>

                                        <view style="justify-content:center;" v-else>
                                                <a-tag class="publish__tag" round>✨选择合适的话题让更多人看到</a-tag>
                                        </view>
                                        <a-icon name="arrow-right" color="#ccc" size="14" />
                                </view>

                                <view class="publish__cell">

                                        <view style="flex-flow:row;" @click="choosePois">
                                                <a-icon name="map-filling" size="12" color="#000" />
                                                <text class="publish__label-text">
                                                        添加地点
                                                </text>

                                                <text class="publish__label-value" v-if="postData.location.name">
                                                        {postData.location.name}
                                                </text>

                                                <a-icon name="arrow-right" color="#ccc" size="14" />
                                        </view>

                                        <scroll-view scroll-x style="margin-bottom:15px;" v-if="!postData.location.name">
                                                <a-tag v-for="(poi,i) in pois" class="publish__tag" round @click="selectPoi(i)">{poi.name}
                                                </a-tag>
                                        </scroll-view>
                                </view>


                        </view>


                </scroll-view>


                <safe-area class="publish__footer">

                        <view class="publish__footer-left">
                                <view class="publish__draft-bg">
                                        <a-icon name="inbox" size="18" color="#666" />
                                </view>
                                <text class="publish__draft-text">
                                        存草稿
                                </text>
                        </view>

                        <text class="publish__button" @click="publish">
                                发布笔记
                        </text>

                </safe-area>




        </view>
</template>
<script>
import '../../components/act/a-nav-bar.stml';
import '../../components/act/a-tag.stml';
import { chooseImages, req } from '../../script/util';
export default {
        name: 'publish',
        apiready() {
                token = api.getPrefs({
                                sync: true,
                                key: 'token'
                          });
                this.loadTopics();
                this.getPois();
                this.listenPoi();
               
        },
        data() {
                return {
                        postData: {
                                images: api.pageParam.images || [],
                                imgx: [],
                                title: '',
                                content: '',
                                token: '',
                                topic: {
                                        id: 0,
                                        name: ''
                                },
                                location: {
                                        name: '',
                                        pos: null
                                }
                        },
                        topics: [],
                        pois: []
                }
        },
        methods: {
                choosePic() {

                        const max = 9 - this.data.postData.images.length;

                        chooseImages(max).then(images => this.data.postData.images.push(...images));
                        console.log(JSON.stringify(this.data.postData.images));
                },
                del(_index){
                        this.data.postdata.images.splice(_index,1);
                },
                yiChu(_index) {
                        console.log(JSON.stringify(index));
                        api.confirm({
                        msg: '你确定要删除这张图片吗?',
                        buttons: ['确定', '取消']
                }, function(ret, err) {
                        this.data.postData.images.splice(index,1)
                });

                },
                loadTopics() {
                        req('topics/list').then(data => this.data.topics = data)
                },
                chooseTopic() {
                        api.actionSheet({
                                title: '选择话题',
                                cancelTitle: '取消',
                                destructiveTitle: '不使用话题',
                                buttons: this.data.topics.map(item => item.name)
                        }, ({ buttonIndex }) => {
                                const index = buttonIndex - 2;

                                if (index === -1) {
                                        this.data.postData.topic = {
                                                id: 0,
                                                name: ''
                                        }
                                } else if (index === this.data.topics.length) {
                                        console.log('取消')
                                } else {
                                        this.data.postData.topic = this.data.topics[index];
                                }
                        });
                },
                getPois() {
                        var map = api.require('bMap');
                        var that = this;
                        map.getLocation({
                                accuracy: '100m',
                                autoStop: true,
                                filter: 1
                        }, function(ret, err) {
                                if (ret.status) {
                                        console.log(JSON.stringify(ret));
                                        map.searchNearby({
                                                keyword: '景点',
                                                lon: ret.lon,
                                                lat: ret.lat,
                                                radius: 2000
                                        }, function(ret, err) {
                                                if (ret.status) {
                                                       
                                                        that.data.pois = ret.results;
                                                        console.log(JSON.stringify(that.data.pois));
                                                } else {
                                                        console.log(JSON.stringify(err));
                                                }
                                        });
                                } else {
                                        console.log('没返回');
                                }
                        });

                },
                selectPoi(i) {
                        const poi = this.data.pois;
                        this.setPoi(poi);
                },
                choosePois() {
                        api.openWin({
                                name: 'pois',
                                url: '../post/pois.stml',
                                pageParam: {
                                        pois: this.data.pois
                                }
                        })
                },
                listenPoi() {
                        api.addEventListener({
                                name: 'sendPoi'
                        }, ({ value }) => {
                                this.setPoi(value);
                                api.closeWin({
                                        name: 'pois'
                                })
                        })
                },
                setPoi(poi) {

                        if (poi) {
                                this.data.postData.location = {
                                        pos: {
                                                lat: poi.lat,
                                                lon: poi.lon
                                        },
                                        name: poi.name
                                }
                        } else {
                                this.data.postData.location = {
                                        pos: null,
                                        name: ''
                                }
                        }
                },
                publish() {
                        // console.log(JSON.stringify(this.data.postData))

                        const postData = this.data.postData;

                        if (postData.images.length === 0) {
                                return api.toast({
                                        msg: '请选择至少一张图片'
                                })
                        }

                        if (!postData.title) {
                                return api.toast({
                                        msg: '请输入标题'
                                })
                        }
                        if (!postData.content) {
                                return api.toast({
                                        msg: '请输入内容'
                                })
                        }

                        const uploadList = postData.images.map(image => req('posts/upload', {}, image));

                        Promise.all(uploadList)
                                .then(data => {
                                        postData.images = data.map(item => item.url);

                                        // console.log(JSON.stringify(postData));
                                        req('posts/publish', postData)
                                                .then(data => {
                                                        api.sendEvent({
                                                                name: 'publish-success',
                                                                extra: data
                                                        })
                                                })
                                                .catch(err => console.log(JSON.stringify(err)))

                                })



                }
        }
}
</script>
勇可可 · 2022-6-15 20:45完整的代码发一下
<img class="publish__images-img" mode="aspectFill" v-for="(img,_index) in postData.images" src={img} @click="del" />

你要把参数带上啊 你不带上 他知道个鬼 @click="del(_index)"
yhbhpu · 2022-6-18 15:39就是仿小红书的帖子发布页代码,我增加了一个删除函数


92
帖子
2
勋章
717
Y币
<img class="publish__images-img" mode="aspectFill" v-for="(img,_index) in postData.images" src={img} @click="del(_index)" />

似乎不是这里的问题,刚开始我是加上的参数的,发现有问题又去掉了,现在重新加上了,但是还是那个问题:[ERROR] dispatch event failed. TypeError: Cannot read property 'images' of undefined
勇可可 · 2022-6-18 17:55你要把参数带上啊 你不带上 他知道个鬼 @click="del(_index)"
12下一页
您需要登录后才可以回帖 登录

本版积分规则