0

Slall question:

I have a 2 parent components nesting the same child component inside of them. I use props so the parents can tell the child what title to show.

this child component is a photo gallery. It makes a query to the database, download photos and show them up. classic.

I need the parents to tell the child where to get the photos from: Get the photos from All users, for the home page or get only the photos from a specific user for a user's page.

I'm wondering if I can pass this information through a prop. Is that possible? Can we use the info from a prop as a varialble inside of the setup() function? Is there a better way to do this?

6
  • 1
    why does one not simply show us the code? boromir meme Commented Apr 10, 2021 at 16:45
  • 1
    Show us some code please. How to Ask Commented Apr 10, 2021 at 16:46
  • My code doesn't make any sense. I don't even know if it is possible. I just want to know if it is possible for a child component tu use a prop as a value in function. Commented Apr 10, 2021 at 17:07
  • Basicly, I just want to know if there is a way to console.log a prop in the child component. I'm working with the composition api and the setup() function. Commented Apr 10, 2021 at 17:34
  • v3.vuejs.org/guide/composition-api-setup.html#props Commented Apr 10, 2021 at 17:46

1 Answer 1

2

Passing objects from one component to a child component is the purpose of props.

You can pass many items through props. VueJS has the following types built-in:

  • String
  • Number
  • Boolean
  • Array
  • Object
  • Function
  • Promise

In the V3 VueJS guide it gives the following example of a prop being passed into a component and then being logged to the console inside the setup() method.

export default {
  props: {
    title: String
  },
  setup(props) {
    console.log(props.title)
  }
}

However, when using props, you should always mark whether the is required, what its type is and what the default is if it is not required.

For example:

export default {
  props: {
    title: String, // This is the type
    required: false, // If the prop is required
    default: () => 'defaultText' // Default needed if required false
  },
  setup(props) {
    console.log(props.title)
  }
}

It's important to note that when using default, the value must be returned from a function. You cannot pass a default value directly.

Sign up to request clarification or add additional context in comments.

2 Comments

!! setup(props) !! I think THIS is the only thing I was missing... I can't believe it was so simple. Thank you so much for your message!!! I wish I had asked the question straight away instead of searching on my own. I don't think I have seen this anywhere though. And I searched all day!! thanks again!!
No worries, @Posoroko! I'm glad this answer helped you. You can see the exact example I used and more examples on the page I linked in my answer. Please my answer as correct and upvote it if possible so it can have better visibility for any future visitors. Happy learning! :)

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.