0

I'm having trouble getting a route param to pass directly into a component. I followed multiple sets of directions in the docs (including using the Composition API as in the following code), but I'm still getting undefined when the CourseModule.vue first renders.

Route Definition

  {
    path: '/module/:id',
    name: 'Course Module',
    props: true,
    component: () => import('../views/CourseModule.vue'),
  },

CourseModule.vue:

<template>
    <div class="AppHome">
        <CustomerItem />
        <CourseModuleItem :coursemodule-id="this.CoursemoduleId"/>
    </div>
</template>
<script>
import { useRoute } from 'vue-router';
import CustomerItem from '../components/customers/customer-item.vue';
import CourseModuleItem from '../components/coursemodules/coursemodule-item.vue';

export default {
  setup() {
    const route = useRoute();
    alert(`CourseModule.vue setup: ${route.params.id}`);
    return {
      CoursemoduleId: route.params.id,
    };
  },
  components: {
    CustomerItem,
    CourseModuleItem,
  },
  mounted() {
    alert(`CourseModule.vue mounted: ${this.CoursemoduleId}`);
  },
};
</script>

coursemodule-item.vue:

<template>
  <div id="module">
    <div v-if="module.data">
      <h2>Course: {{module.data.ModuleName}}</h2>
    </div>
    <div v-else-if="module.error" class="alert alert-danger">
      {{module.error}}
    </div>
    <Loader v-else-if="module.loading" />
  </div>
</template>

<script>
import Loader from '../APILoader.vue';

export default {
  props: {
    CoursemoduleId: String,
  },
  components: {
    Loader,
  },
  computed: {
    module() {
      return this.$store.getters.getModuleById(this.CoursemoduleId);
    },
  },
  mounted() {
    alert(`coursemodule-item.vue: ${this.CoursemoduleId}`);
    this.$store.dispatch('setModule', this.CoursemoduleId);
  },
};
</script>

The output from my alerts are as follows:

CourseModule.vue setup: zzyClJDQ3QAKuQ2R52AC35k3Hc0yIgft

coursemodule-item.vue: undefined

CourseModule.vue mounted: zzyClJDQ3QAKuQ2R52AC35k3Hc0yIgft

As you can see, the path parameter works fine in the top level Vue, but not it's still not getting passed into the component.

1 Answer 1

1

your kebab-cased :coursemodule-id props that you're passing to the CourseModuleItem component becomes a camelCased coursemoduleId props

Prop Casing (camelCase vs kebab-case)

try this

// coursemodule-item.vue
...
props: {
  coursemoduleId: String,
},
...
mounted() {
  alert(`coursemodule-item.vue: ${this.coursemoduleId}`);
  this.$store.dispatch('setModule', this.coursemoduleId);
},
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you thank you thank you! That would have taken all day to find. I thought I was doing something funny with 'this'.

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.