我的观点是这样的:
<div class="favorite" style="margin-bottom:5px;">
@if (Auth::user())
<add-favorite-store :id-store="{{ $store->id }}"></add-favorite-store>
@else
<a href="javascript:" class="btn btn-block btn-success">
<span class="fa fa-heart"></span> Favorite
</a>
@endif
</div>
我的组件是这样的:
<template>
<a href="javascript:" class="btn btn-block btn-success" @click="addFavoriteStore($event)">
<span class="fa fa-heart"></span> <label id="favoriteId">{{ store_id == 'responseFound' ? 'Un-Favorite' : 'Favorite' }}</label>
</a>
</template>
<script>
export default{
props:['idStore'],
mounted(){
this.checkFavoriteStore()
},
methods:{
addFavoriteStore(event){
var label = $('#favoriteId')
var text = label.text()
event.target.disabled = true
const payload= {id_store: this.idStore}
if(text == "Favorite") {
this.$store.dispatch('addFavoriteStore', payload)
}
else {
this.$store.dispatch('deleteFavoriteStore', payload)
}
setTimeout(function () {
location.reload(true)
}, 1500)
},
checkFavoriteStore(){
const payload= {id_store: this.idStore}
var data = this.$store.dispatch('checkFavoriteStore', payload)
data.then((res) => this.store_id = res)
}
},
data() {
return {
store_id: ''
}
}
}
</script>
在我上面的代码中,我使用
location.reload(真)
更新页面上的数据。但它正在重新加载页面。
我想在更新页面时,不会重新加载页面
我该怎么做?
答案 0 :(得分:2)
vue-router doc中解释的另一种解决方案。只需使用应用于watch
原生属性的$route
系统即可。即使他们使用相同的组件并获取结果,您也能够检测到新的路由。
答案 1 :(得分:1)
您需要的是 vm-forceUpdate 。
强制Vue实例重新渲染。请注意,它不会影响所有子组件,只影响实例本身和插入插槽内容的子组件。
我自己没有尝试过使用它,只是在处理项目时遇到过它。
vue forceUpdate或者您可以在setTimeout
方法中调用该函数。
setTimeout(function () {
this.checkFavoriteStore();
}, 1500)
你在这里使用了很多javascript,你可以使用vue来完成那里的大部分工作。
答案 2 :(得分:1)
Ok这是一个简单的用例,但不像你的那样复杂,并且应该使用vuejs。 (http://codepen.io/simondavies/pen/MJOQEW)
好吧,让laravel / php代码获取商店ID,以及它是否已被收藏。这样你的脚本就不会先检查商店然后决定做什么。
这样做是通过组件发送document.cookie = "gameData = 0";
console.log(document.cookie);
和store-id
,如:
is-favorited
然后Vue组件将更新按钮以显示其已经喜欢(红色)或不喜欢(灰色),然后还处理点击事件并相应地更新。
当您使用Laravel告诉组件它是否已经被收藏时,您可以摆脱您的检查功能和少一个http请求。然后,您只需在用户单击“收藏夹”按钮时更新商店。
正如演示中所见,它更新,无需刷新。
我希望这可以帮助你重写你的内容,以便得到你想要的东西。
PS我遗漏了你所拥有的Logged IN支票 <favorite :store-id="{{$store->id}}" :is-favorited="{{$store->isFavorited}}"></favorite>
所以你可以把它放回去等等
@if (Auth::user())
Vue.component('favorite', {
template: '<button type="button" @click.prevent="updateFaviteOption" :class="{ \'is-favorited\': isFavorited }"><span class="fa fa-heart"></span></button>',
props: [
'storeId',
'isFavorited'
],
data: function() {
return {}
},
methods: {
updateFaviteOption: function() {
this.isFavorited = !this.isFavorited;
///-- DO YOU AJAX HERE i use axios
///-- so you can updte the store the the this.isFavorited
}
}
});
new Vue({
el: '#app',
});
.favorite-wrapper {
margin: 20px auto;
padding: 0;
text-align: center;
width: 500px;
height: 100px;
}
a:link,
a:active,
a:visited {
text-decoration: none;
text-transform: uppercase;
color: #444;
transition: color 800ms;
font-size: 18px;
}
a:hover,
a:hover:visited {
color: purple;
}
button {
margin: 10px auto;
padding: 8px 10px;
background: transparent;
width: auto;
color: white;
text-transform: uppercase;
transition: color 800ms;
border-radius: 4px;
border: none;
outline: none;
}
button:hover {
cursor: pointer;
color: #058A29;
}
.fa {
color: #d9d9d9;
font-size: 20px;
transition: color 400ms, transform 400ms;
opacity: 1;
}
.is-favorited .fa {
opacity: 1;
color: red;
transform: scale(1.4);
}
答案 3 :(得分:0)
这里没有提到很多,以帮助尽可能好地回答问题,它需要有问题的HTML,需要更新的内容以及等等。
我认为你最好看一下这个https://laracasts.com/series/learn-vue-2-step-by-step,以便更好地理解Vuejs如何运作以及如何做这样简单的事情。
您使用深入的商店/ vuex类型编码,如果您不了解基础知识,那么很难为您找到答案。
抱歉有点啰嗦,但是。