9
votes

I have an issue where a computed property only sometimes works. Sometimes it has the value/error templateComponent:"(error during evaluation)"

What could be causing this issue? If someone can point me in the correct direction I can investigate further but I dont know where to start.

The problem computed property:

// Error in the below computed property
templateComponent() {
  let template = 'default' // assign default template

  if (!_.isNull(this.wp.template) && this.wp.template.length)
    template = this.wp.template.replace('.php','').toLowerCase()

  return template
}

Page.vue

<template>
    <div v-if="wp">
      <component :is="templateComponent" v-bind:wp="wp"></component>
    </div>
    <p v-else>Loading...</p>
</template>

<script type="text/javascript">

import { mapGetters } from 'vuex'
import * as Templates from './templates'

// Map template components
let templateCmps = {}
_.each(Templates, cmp => {
    templateCmps[cmp.name] = cmp
})

export default {

  props: ["slug"],

  components: {
    ...templateCmps

    // Example of templateCmps is below
    // 'default': Templates.Default,
    // 'agency': Templates.Agency,
    // 'home': Templates.Home,
  },

  computed: {
    ...mapGetters(['pageBySlug']),

    wp() {
      return this.pageBySlug(this.slug);
    },

    // Error in the below computed property
    templateComponent() {
      let template = 'default' // assign default template

      if (!_.isNull(this.wp.template) && this.wp.template.length)
        template = this.wp.template.replace('.php','').toLowerCase()

      return template
    }
  },

  created() {
    // Get page title, content, etc. via rest request
    this.$store.dispatch('getPageBySlug', { slug: this.slug })
  }
}
</script>
2
Always string of undefined? Why even bother checking for null?Estradiaz

2 Answers

1
votes

The problem might relate to this.wp.template. Are you certain that it is always a string upon which lowerCase can be called? If the computed property would return something else than a string, it could cause the problem.

Also, Vue has problems handling dashes before numbers.

0
votes
**> must be added:
> --> import store from "./store/store";
> --> new Vue({ .... store ....**


import store from "./store/store";

Vue.config.productionTip = false;

new Vue({
  router,
  store,
  render: (h) => h(App),
}).$mount("#app");