admin管理员组文章数量:1430811
My view is like this :
<div class="panel panel-default panel-store-info">
...
<div id="app">
<AddFavoriteProduct></AddFavoriteProduct>
</div>
....
</div>
My ponent 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 './ponents/AddFavoriteProduct.vue';
...
ponents: {
...
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 ponent correctly? For recursive ponents, make sure to provide the "name" option. (found in root instance)
Whereas I had register it
My view is like this :
<div class="panel panel-default panel-store-info">
...
<div id="app">
<AddFavoriteProduct></AddFavoriteProduct>
</div>
....
</div>
My ponent 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 './ponents/AddFavoriteProduct.vue';
...
ponents: {
...
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 ponent correctly? For recursive ponents, make sure to provide the "name" option. (found in root instance)
Whereas I had register it
Share Improve this question edited Jan 25, 2017 at 6:42 Saurabh 73.7k44 gold badges191 silver badges251 bronze badges asked Jan 25, 2017 at 5:09 moses tohmoses toh 13.2k81 gold badges265 silver badges459 bronze badges 2- Are you using any other ponent or this is first ponent you are trying, also can you add whole of App.js if possible. which version of vue you are using – Saurabh Commented Jan 25, 2017 at 6:43
- @Saurabh, Yes, I have another ponent. Another ponent works well – moses toh Commented Jan 25, 2017 at 6:46
4 Answers
Reset to default 3Three fixes that I can see...
First, omit name: 'AddFavoriteProduct'
in your ponent (it's not required for single-file ponents) and use kebab-case for the ponent in your template. Second, you appear to be missing the id-product
prop
<add-favorite-product :id-product="someProductIdFromSomewhere"></<add-favorite-product>
Third, you don't use interpolation in bound properties and you don't even need to pass anything other than the $event
to your addFavoriteProduct
method
@click="addFavoriteProduct($event)"
HTML is case-insensitive, so your custom button element <AddFavoriteProduct></AddFavoriteProduct>
is being interpreted as your Vue warning reports: <addfavoriteproduct>
When you name your ponent 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".
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 ponent - you can try the below
<template>
<a href="javascript:" class="btn btn-block btn-success" @click="addFavoriteProduct({{ $product->id }}, $event)">
<span class="fa fa-heart"></span> Favorite
</a>
</template>
<script>
export default{
props:['idProduct'],
vuex: {
actions: {
setFavoriteProduct: setFavoriteProduct
// assuming you named the action as setFavoriteProduct in vuex
// avoid duplicate naming for easy debugging
}
}
methods:{
addFavoriteProduct(productId, event){
event.target.disabled = true
const payload= {id_product: this.idProduct}
// this.$store.dispatch('addFavoriteProduct', payload)
this.setFavoriteProduct(payload);
setTimeout(function () {
location.reload(true)
}, 1500);
}
},
}
Note: 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 like
store.dispatch('setSplitData',[data[0], data [1]])
// in the action:
setSplitData (context, [data1, data2]) { // uses ES6 argument destructuring
OR
//... with an object:
store.dispatch('setSplitData',{
data1: data[0],
data2: data [1],
})
// in the action:
setSplitData (context, { data1, data2 }) { // uses ES6 argument destructuring
//source: LinusBorg's ment at https://github./vuejs/vuex/issues/366
Are 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
<template>
<a href="javascript:" class="btn btn-block btn-success" @click="addFavoriteProduct({{ $product->id }}, $event)">
<span class="fa fa-heart"></span> Favorite
</a>
</template>
<script>
export default{
name: 'AddFavoriteProduct', // I am not sure if name is needed, don't remember seeing it in docs
props:['idProduct'],
methods:{
addFavoriteProduct(productId, event){
event.target.disabled = true
const payload= {id_product: this.idProduct}
this.$store.dispatch('addFavoriteProduct', payload)
setTimeout(function () {
location.reload(true)
}, 1500);
}
}
}
</script>
Another problem is you are expecting a prop idProduct
, which is not being passed to ponent, You have to pass it like this in kebab-case:
<div class="panel panel-default panel-store-info">
...
<div id="app">
<AddFavoriteProduct id-product="4"></AddFavoriteProduct>
</div>
....
</div>
本文标签: javascriptHow to display button link with vuejsStack Overflow
版权声明:本文标题:javascript - How to display button link with vue.js? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745534904a2662227.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论