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

GraphQL学习第八篇 -Vue中使用Vue-apollo进行新增操作

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

在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="doAdd()">提交数据</button>
        </div>
    </div>
</template>

<script>
// 引入模块
import gql from "graphql-tag";
// 添加的语句,注意参数要指定类型
var navMutationAddGql = gql`
    mutation($title: String!, $url: String!) {
        addNav(title: $title, url: $url) {
            title
        }
    }
`;
export default {
    name: "app",
    data() {
        return {
            navJson: {
                title: "",
                url: ""
            }
        };
    },
    methods: {
        // 添加的方法
        doAdd() {
            // $apollo.mutate为默认的更新方法
            this.$apollo
                .mutate({
                    // 更新的语句
                    mutation: navMutationAddGql,
                    // 实参列表
                    variables: {
                        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 归档