My view is like this :
<div class="panel panel-default panel-store-info">
...
<div id="app">
<AddFavoriteProduct></AddFavoriteProduct>
</div>
....
</div>
My component is like this :
<template>
<a href="javascript:" class="btn btn-block btn-success" @click="addFavoriteProduct({{ $product->id }})">
<span class="fa fa-heart"></span> Favorite
</a>
</template>
<script>
export default{
name: 'AddFavoriteProduct',
props:['idProduct'],
methods:{
addFavoriteProduct(event){
event.target.disabled = true
const payload= {id_product: this.idProduct}
this.$store.dispatch('addFavoriteProduct', payload)
setTimeout(function () {
location.reload(true)
}, 1500);
}
}
}
</script>
When click button favorite, it will call controller on the laravel
I had register on the app.js like this :
...
import AddFavoriteProduct from './components/AddFavoriteProduct.vue';
...
components: {
...
AddFavoriteProduct
},
...
When executed, the button favorite does not appear.
Please help.
UPDATE
There exist error like this :
[Vue warn]: Unknown custom element: - did you register the component correctly? For recursive components, make sure to provide the "name" option. (found in root instance)
Whereas I had register it
I am not used to
vuex
, as I generally implement my own store as a POJO object - not graduated with the learning curve of vuex. So can't provide a working solution with vuex, however to my knowledge, you need to kind of import the action(s) in your component - you can try the belowNote: From Vuex2 the
store.dispatch()
accepts only one argument (not a problem with your current use case), however if you need to pass multiple arguments to your action you can work around likeThree fixes that I can see...
First, omit
name: 'AddFavoriteProduct'
in your component (it's not required for single-file components) and use kebab-case for the component in your template. Second, you appear to be missing theid-product
propThird, you don't use interpolation in bound properties and you don't even need to pass anything other than the
$event
to youraddFavoriteProduct
methodAre you getting any error?
One mistake I see in your code is, you are taking one parameter: event, while you are trying to pass
{{ $product->id }}
to it, which seems a laravel variable. (Sorry, I don't know laravel)If you want both event and
product->id
in method, you have to pass both parameters from HTML, like it is in docs with help of$event
Another problem is you are expecting a prop
idProduct
, which is not being passed to component, You have to pass it like this in kebab-case:HTML is case-insensitive, so your custom button element
<AddFavoriteProduct></AddFavoriteProduct>
is being interpreted as your Vue warning reports:<addfavoriteproduct>
When you name your component with camelCase or PascalCase, the corresponding tag name you should be using in your markup should be kebab-cased like so:
<add-favorite-product></add-favorite-product>
Vue knows to do the conversion from "dash-letter" to "uppercase-letter".