前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >GraphQL学习第九篇 -Vue中使用Vue-apollo进行编辑操作

GraphQL学习第九篇 -Vue中使用Vue-apollo进行编辑操作

作者头像
越陌度阡
发布2020-11-26 12:28:24
4410
发布2020-11-26 12:28:24
举报

在Vue中集中Vue-apollo以后(如何集成请查看本专栏第六篇),就可以使用它进行编辑数据了。

代码语言:javascript
复制
<template>
    <div class="news">
        <div class="navForm">
            导航名称:
            <input v-model="navJson.title" type="text"/>
            导航链接:
            <input v-model="navJson.url" type="text"/>
            <button @click="doEdit()">修改数据</button>
        </div>
    </div>
</template>

<script>
// 引入模块
import gql from "graphql-tag";

// 修改的语句,注意参数要指定类型
var navMutationEditGql = gql`
    mutation($id: String!, $title: String!, $url: String!) {
        editNav(_id: $id, title: $title, url: $url) {
            title
        }
    }
`;

export default {
    name: "app",
    data() {
        return {
            navJson: {
                title: "",
                url: ""
            }
        };
    },
    methods: {
        // 修改的方法
        doEdit() {
            // $apollo.mutate为默认的更新方法
            this.$apollo
                .mutate({
                    // 修改的语句
                    mutation: navMutationEditGql,
                    // 实参列表
                    variables: {
                        _id: "5c7a1eeaa445b5090c70a021",
                        title: this.navJson.title,
                        url: this.navJson.url
                    }
                })
                .then(response => {
                    // 输出获取的数据集
                    console.log(response);
                })
                .catch(err => {
                    // 捕获错误
                    console.log(err);
                });
        }
    }
};
</script>

<style scoped>
</style>
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2020/03/12 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档