1

Having an issue passing two data members from my parent's data method to the child component's props. Basically, I'm setting the data values inside my parent component, binding them to my child component invocation inside the HTML template. Then I'm attempting to reference this passed data inside the child'd 'props'.

But I keep getting this inside my Vue dev tools:

 props
   date: undefined
   title: undefined

Here's my code:

App.vue (showing for reference only. i don't expect my issue is with this code)

<template>
  <div id="app">
    <div id="nav">
      <Header />
    </div>
  </div>
</template>

<script>
  import Header from '@/components/Header.vue'

  export default {
    components: {
      Header
    }
  }
</script>

Home.vue (Parent component)

<template>
  <div>
    <Header :title="title" :date="date" />
  </div>
</template>

<script>
// @ is an alias to /src
import Header from "@/components/Header.vue";
import moment from "moment";

export default {
  data () {
    return {
      title: "SOME NEWS",
      date: moment().format("dddd, MMMM D, YYYY") 
    }
  },
  components: {
    Header
  }
};
</script>

Header.vue (Child component)

<template>
  <div class="hello">
    <h1>{{ getTitle() }}</h1>
    <h3>{{ getDate() }}</h3>
  </div>
</template>

<script>
  export default {
    props: {
      title: {
        type: String,
        required: true
      },
      date: {
        type: String,
        required: true
      }
    },
    method: {
      getTitle() {
        return this.title
      },
      getDate() {
        return this.date
      }
    }
  };
</script>
10
  • Can you share a working example? Commented Apr 21, 2020 at 21:26
  • If I had a working example, I would not have posted this question. Commented Apr 21, 2020 at 21:33
  • But I keep getting this inside my Vue dev tools: This happens without a working example? Commented Apr 21, 2020 at 21:33
  • The code I provided is what I have, and it's not working and the Vue dev tools (for Chrome) shows that the two properties, title and date, are undefined. Commented Apr 21, 2020 at 21:39
  • Do you understand the meaning of a working example? It is the code which shows your error, working in an environment, like a code snippet/code sandbox. Commented Apr 21, 2020 at 21:41

1 Answer 1

2

If I understand you correct in your App component code you should use <Home /> component, but you use <Header /> instead. Example:

<template>
  <div id="app">
    <div id="nav">
      <Home />
    </div>
  </div>
</template>

<script>
  import Home from '@/components/Home.vue'
  export default {
    components: {
      Home
    }
  }
</script>
Sign up to request clarification or add additional context in comments.

4 Comments

The Home component is inside the 'views' directory, and does not need to be registered inside App.vue. My Header component, however, needs to be displayed on every page, so included it inside App.vue and registered as a component.
Ok, but you included your Header component inside Home. And you passed props from Home to Header, but your Home component not used anywhere. It will never showed.
I see what you mean now. So App.vue should be my Parent, not Home.vue. Is that accurate?
Yes, it is correct. Please try to replace Header with Home in you App component, and you will see how it works.

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.