简体   繁体   English

Vue.js如何将数据作为道具传递给子组件

[英]Vuejs how to pass data as a prop to a child component

I build following component: 我构建以下组件:

var Modal = Vue.component('modal', {

    template: `
        <div id="modal" class="modal">
            <div class="modal-content">
                <p>{{ link }}</p>
            </div>
        </div>
    `,

    props: [
        'link'
    ],

});

And I would like to change the link data dynamically after I sent successfully an axios post. 在成功发送axios帖子后,我想动态更改链接数据。

My vue instance 我的Vue实例

new Vue({

    el: '#form',

    components: {
        'modal': Modal
    },

    data: {
        userId: '',
        title: '',
        body: '',
        snippetLink: '',
    },

    methods: {

        publish (e) {
            var self = this;
            axios.post('/snippets',  {
                title: this.title,
                body: this.content,
            })
            .then(function (response) {
                console.log("success");
                self.link = response.data.hash; // Here I tried to add the reponse content to the vue component's p
            })
            .catch(function (error) {
              console.log(error);
            })
        },

My Html Markup: 我的HTML标记:

        <modal link=""></modal>
       ...
        <button type="button"                      
                   v-bind:class="{ 'modal-trigger': !isActiveModal }"
                   @click="publish">Publish
                   <i class="material-icons right">send</i>
        </button>

So I am sending an axios post to my server successfully and get the data, I would like to open a modal window and put the data in ap tag, so far the modal pops up after my post but I am not sure my it does not change the content of the p tag. 因此,我将axios帖子成功发送到服务器并获取数据,我想打开一个模式窗口并将数据放在ap标签中,到目前为止,该模式在我的帖子之后弹出,但是我不确定我没有更改p标签的内容。

As per my understanding , Snippetlink property can be used to hold data from server. 据我了解,Snippetlink属性可用于保存服务器中的数据。

self.Snippetlink = response.data.hash;

and Pass Snippetlink to link attribute of the snippet-model 并将Snippetlink传递到snippet-model的链接属性

 <snippet-modal :link="Snippetlink"></snippet-modal>

rupesh_padhye's answer is correct. rupesh_padhye的答案是正确的。 This is just a further explanation. 这只是进一步的解释。

First of all, to store the response data to a Vue component, you need to define a key in data for that purpose first. 首先,要将响应数据存储到Vue组件,首先需要为此目的在data定义一个键。 So to make this line work: self.link = response.data.hash; 因此,使此行有效: self.link = response.data.hash; , you need to add link as a key for the Vue component's data: ,您需要添加link作为Vue组件数据的键:

data: {
        userId: '',
        title: '',
        body: '',
        snippetLink: '',
        link: ''
    },

If you mean snippetLink , change the line to self.snippetLink = response.data.hash; 如果您的意思是snippetLink ,则将行更改为self.snippetLink = response.data.hash;

Secondly, to pass the data as a prop to a child component, you have to specify the prop name, as well as the data key being passed. 其次,要将数据作为道具传递给子组件,您必须指定道具名称以及要传递的数据键。 For instance, to pass your component's link as a prop with the same name to the snippet-modal component, you need: <snippet-modal :link="link"></snippet-modal> 例如,要将组件的link作为具有相同名称的prop传递到snippet-modal组件,您需要: <snippet-modal :link="link"></snippet-modal>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM