我正在尝试共享存储在文件favorite_count中收藏夹组件内的变量中的数据Favorites.vue。我想与文件中的应用程序组件共享该数据App.vue,但我无法这样做。我希望如果我更改favorite_count收藏夹组件中的值,它会在应用程序组件中发生更改。在网上做了很多研究,但还没有成功。关于我可能做错了什么有什么想法吗?
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>
Run Code Online (Sandbox Code Playgroud)
App.vue文件
<template>
<div class="navbar navbar-expand-md navbar-dark bg-primary">
<div class="collapse navbar-collapse" id="navbarResponsive">
<ul class="navbar-nav"> …Run Code Online (Sandbox Code Playgroud)