0

I want to pass a parameter to Vue component. please help

Blade

@extends("./layouts.app")
@section("title")
 {{$shop->shop_name}}
@endsection


@section("content")
 <addToCart></addToCart>
@endsection

Vue Js

<template>
  <div class="button-container">
   <button @click="addToCart(product_id)">Add To Cart</button>
  </div>
</template>
6
  • Does the addToCart component have any properties? Commented Nov 27, 2019 at 7:36
  • Does this answer your question? Passing data to components in vue.js Commented Nov 27, 2019 at 7:39
  • @Jerodev no addToCart component doest have any properties Commented Nov 27, 2019 at 7:42
  • @Francois_Borgies NO I want to pass a parameter to addToCart method from the blade... addToCart("parameter") Commented Nov 27, 2019 at 7:45
  • @Anas do you have the product_id in laravel side. Commented Nov 27, 2019 at 7:49

2 Answers 2

0

In your Vue Js add new prop array

<template>
      <div class="button-container">
       <button @click="addToCart(product_id)">Add To Cart</button>
      </div>
    </template>
    <script>
    export default {
      props: ["product_id"],
      methods: {
       addToCart(product_id) 
       {
        //code
       }
    }
    </script>

in your Blade add :product_id ="product id", in addToCart component.

@extends("./layouts.app")
@section("title")
 {{$shop->shop_name}}
@endsection


@section("content")
 <addToCart :product_id="***product id goes here***"></addToCart>
@endsection
Sign up to request clarification or add additional context in comments.

Comments

0

You should follow these steps to access product_id

  1. inside your addToCart component add product_id like this:

    @section("content") <addToCart :product_id="$product_id"></addToCart> @endsection

  2. in your component you should get the :product_id with props array like below :

    <template> <div class="button-container"> <button @click="addToCart(product_id)">Add To Cart</button> </div> </template> <script> export default { props: ["product_id"], methods: { addToCart(product_id) { //code } } </script>

Note: I suppose that you have the product_id and you do the other part of component correct.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.