最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Vue JS 3: How to pass data from one component to another other? - Stack Overflow

programmeradmin3浏览0评论

I'm trying to share data stored in a variable favorite_count within the Favorites ponent in Favorites.vue file. I want to share that data with the App Component in the App.vue file but I'm not able to. I would want that if I change the value of favorite_count in the Favorites ponent, it changes in the App Component. Done quite some research on the web but no success yet. Any ideas on what I could be doing wrong?

Favorites.vue file

<template>
    <div class="row m-5">
        <h3 class="col-8">Your Favorites</h3>
        <div class="col-4">
            <p class="pull-right m-1">Picks 
            <span >{{ favorite_count }}</span>  / 5</p>
        </div>
        <hr>
    </div>
</template>
<script>
export default {
    name: 'favorites',
    data() {
        return {
            favorite_count: 5,
        }
    },
    methods: {
        changeFavoriteCount() { 
            this.favorite_count = this.favorite_count + 2;
        },
        emitToParent (event) {
          this.$emit('childToParent', this.favorite_count)
        }
    }
}
</script>

App.vue file

<template>
    <div class="navbar navbar-expand-md navbar-dark bg-primary">
        <div class="collapse navbar-collapse" id="navbarResponsive">
            <ul class="navbar-nav"> 
                <li class="nav-item">
                    <router-link to="/favorites" class="btn btn-info">
                      Favorites ( {{ favorite_count }} )
                    </router-link>
                </li>
            </ul>
        </div>
    </div> 
</template>
<script> 
import Favorites from './ponents/Favorites.vue'
 
export default {
  name: 'App',
  ponents: {
    Favorites
  },
  data () {
    return { 
      favorite_count: 0, 
    }
  }
}
</script>

I'm trying to share data stored in a variable favorite_count within the Favorites ponent in Favorites.vue file. I want to share that data with the App Component in the App.vue file but I'm not able to. I would want that if I change the value of favorite_count in the Favorites ponent, it changes in the App Component. Done quite some research on the web but no success yet. Any ideas on what I could be doing wrong?

Favorites.vue file

<template>
    <div class="row m-5">
        <h3 class="col-8">Your Favorites</h3>
        <div class="col-4">
            <p class="pull-right m-1">Picks 
            <span >{{ favorite_count }}</span>  / 5</p>
        </div>
        <hr>
    </div>
</template>
<script>
export default {
    name: 'favorites',
    data() {
        return {
            favorite_count: 5,
        }
    },
    methods: {
        changeFavoriteCount() { 
            this.favorite_count = this.favorite_count + 2;
        },
        emitToParent (event) {
          this.$emit('childToParent', this.favorite_count)
        }
    }
}
</script>

App.vue file

<template>
    <div class="navbar navbar-expand-md navbar-dark bg-primary">
        <div class="collapse navbar-collapse" id="navbarResponsive">
            <ul class="navbar-nav"> 
                <li class="nav-item">
                    <router-link to="/favorites" class="btn btn-info">
                      Favorites ( {{ favorite_count }} )
                    </router-link>
                </li>
            </ul>
        </div>
    </div> 
</template>
<script> 
import Favorites from './ponents/Favorites.vue'
 
export default {
  name: 'App',
  ponents: {
    Favorites
  },
  data () {
    return { 
      favorite_count: 0, 
    }
  }
}
</script>
Share Improve this question asked Jul 2, 2021 at 12:43 ValentineValentine 1021 gold badge3 silver badges12 bronze badges 2
  • you should use vuex – Lawrence Cherone Commented Jul 2, 2021 at 13:26
  • 1 see example codesandbox.io/s/nervous-tree-w0lq8 it saves polluting everything with vars – Lawrence Cherone Commented Jul 2, 2021 at 13:47
Add a ment  | 

2 Answers 2

Reset to default 9

If you are going to use <router-view> later in your application, I would suggest this solution

If you were going to include Favorites inside <template> in App.vue, you can use props:

1. Declare your 'shared' variable in the parent ponent (App.vue)

data () {
  return { 
    favorite_count: 0, 
  }
},

2. Define props in your child ponent (Favorites.vue)

export default {
  props: { favorite_count: Number },
  ...
}

3. Pass favorite_count as prop to Favorites

<template>
  ...
    <Favorites :favorite_count="favorite_count" ... />
</template>

If you will need to update favorite_count - emit an event to parent ponent. More about it in Vue docs

Edit: Just to clarify: If you are going to update favorite_count from Favorites.vue, you need to emit an event to App.vue to avoid mutating props.

That also means you need to move your changeFavoriteCount() function to App.vue and apply a listener to your child ponent which will call this function:

// App.vue
<template>
  ...
    <Favorites 
       @your-update-event="changeFavoriteCount" 
       :favorite_count="favorite_count" ...
    />
</template>

...

changeFavoriteCount(newVal) { 
    this.favorite_count = newVal;
},

change your Favourite.vue file like this

<template>
  <div class="row m-5">
    <h3 class="col-8">Your Favorites</h3>
    <div class="col-4">
      <p class="pull-right m-1">
        Picks <span>{{ favorite_count }}</span> / 5

        <button @click="changeFavoriteCount">Click me to change favorite_count</button>
      </p>
    </div>
    <hr />
  </div>
</template>
<script>
export default {
  name: "favorites",
  data() {
    return {
      favorite_count: 5,
    };
  },
  methods: {
    changeFavoriteCount() {
      this.favorite_count = this.favorite_count + 2;
      this.emitToParent();
    },
    emitToParent() {
      this.$emit("childToParent", this.favorite_count);
    },
  },
};
</script>

and the App.vue file like this

<template>
    <div class="navbar navbar-expand-md navbar-dark bg-primary">
        <div class="collapse navbar-collapse" id="navbarResponsive">
            <ul class="navbar-nav"> 
                <li class="nav-item">
                    <router-link to="/favorites" class="btn btn-info">
                      
                      <Favorites @childToParent="updateFavorite" />
                      Favorites ( {{ favorite_count }} )
                    </router-link>
                </li>
            </ul>
        </div>
    </div> 
</template>
<script> 
import Favorites from './ponents/Favorites.vue'
 
export default {
  name: 'App',
  ponents: {
    Favorites
  },
  data () {
    return { 
      favorite_count: 0, 
    }
  },
  methods: {
    updateFavorite(data) {
      this.favorite_count = data;
    },
  },
}
</script>
发布评论

评论列表(0)

  1. 暂无评论