vue.js - 计算属性的“评估期间错误”

标签 vue.js vuejs2 vue-component

我遇到一个问题,即计算属性仅有时有效。有时它有值/错误 templateComponent:"(error during evaluation)"

是什么导致了这个问题?如果有人能指出正确的方向,我可以进一步调查,但我不知道从哪里开始。

问题计算属性:

// 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
}

页面.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>

最佳答案

问题可能与 this.wp.template 有关。您确定它始终是可以调用 lowerCase 的字符串吗?如果计算属性将返回字符串以外的其他内容,则可能会导致问题。

此外,Vue 在处理 numbers 之前的破折号时有问题.

关于vue.js - 计算属性的“评估期间错误”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50016501/

相关文章:

javascript - 在加载组件之前等待 VueX 值加载

javascript - Vuejs 2 : debounce not working on a watch option

vue.js - Vue js 组件中的 jQuery Accordion

vue.js - vue3 ssr 不返回纯 html

Vue.js——v-model 和 v-bind 的区别

javascript - 如何从 Vue.js 中的组件生命周期方法访问 mixin 方法内部的函数

javascript - 在组件内嵌套 Vue 组件时出现问题

javascript - 如何组合两个组件 Vue.js

html - vuetify v-col xs ="12"只填充一半的宽度

javascript - 如何在 Vue js 中解析 json 以在模板中使用它