【问题标题】:Props not working in my Vue.js application道具在我的 Vue.js 应用程序中不起作用
【发布时间】:2019-02-14 04:26:51
【问题描述】:

我正在开发一个原型,所以有些字段是硬编码的。 我可以知道为什么下面会抛出错误吗?

vue.runtime.esm.js:587 [Vue warn]: Property or method "A" is not defined on the instance but referenced during render. Make sure that this property is reactive, either in the data option, or for class-based components, by initializing the property. See: https://vuejs.org/v2/guide/reactivity.html#Declaring-Reactive-Properties.

found in

---> <Recommendation> at components/Recommendations.vue
       <HomePage> at pages/profile.vue
         <Nuxt>
           <Default> at layouts/default.vue
             <Root>

Recommendations.vue

<template>
    <div class="recommendations">
        <div class="recommendations__content">
            <AppOption :selected="A"></AppOption>
            <AppOption :selected="B"></AppOption>
            <AppOption :selected="C"></AppOption>
        </div>
    </div>
</template>

<script>
import AppOption from '@/components/Option.vue'

export default {
    name: 'Recommendation',
    components: {
        AppOption
    },
    data() {
        return {
        }
    }
}
</script>

Option.vue

<template>
    <div>
        <b-field>
            <b-select
                placeholder="Select skill"
                v-model="current"
                size="is-medium"
                expanded>

                <template v-for="option in options">
                    <option :value="option.value" v-bind:key="option.value">{{ option.title }} </option>
                </template>
            </b-select>
        </b-field>
        <div class="recommendations__content__row">
            <div class="fieldset">
                <label>Current:</label>
                **<input type="text" value="6.0" disabled>**
            </div>
            <div class="fieldset">
                <label>Goal:</label>
                <input type="text" value="8.0">
            </div>
        </div>
    </div>
</template>

<script>
export default {
    props: ['selected'],
    data() {
        return {
            current: this.selected,
            options: [
                { title: 'Skill A', value: 'A', points: 6},
                { title: 'Skill B', value: 'B', points: 5},
                { title: 'Skill C', value: 'C', points: 4}
            ]
        }
    }
}
</script>

另外,如何根据父页面选择的内容将 Option.vue 中以“**”突出显示的部分更新为 JSON 中的“点”?

【问题讨论】:

    标签: javascript json node.js vue.js nuxt.js


    【解决方案1】:

    关于这部分

            <AppOption :selected="A"></AppOption>
            <AppOption :selected="B"></AppOption>
            <AppOption :selected="C"></AppOption>
    

    您必须定义 A、B、C 属性或数据。例如,添加

    data() {
        return {
          A: '',
          B: '',
          C: '',
        }
    }
    

    对于第二部分,最好的方法是添加一个计算属性。

    computed: {
            selectedPoints() {
                return this.current.points 
            }
        }
    

    并添加

    **<input type="text" :value="selectedPoints" disabled>**
    

    在第二部分,如果您发现 v-model 更适合您的用例,您也可以使用它。


    @yeeen 更新: 我使用了一个 for 循环来获得我想要的点。 cmets中的解释

    computed: {
        selectedPoints() {
            for(let i=0; i<this.options.length; i++) {
                console.log(this.options[i]);
                if (this.options[i].value == this.current)
                    return this.options[i].points
            }
            return 0;
        }
    }
    

    【讨论】:

    • 我只希望 A、B、C 作为字符串,而不是变量...以对应 JSON 选项中的 A、B、C。所以我可以根据传入的字符串选项将默认设置为选项之一: [ { title: 'Skill A', value: 'A', points: 6}, { title: 'Skill B', value: 'B ',分:5},{标题:'技能C',值:'C',分:4}]
    • 如果您只想传递一个字符串,请删除“:”。所以,它只是
    • 很高兴能为您提供帮助。
    • 也感谢第二部分。我不能使用 this.current.points 因为存储在 this.options 中的点 r。但我明白了!我会更新你的答案并奖励你的积分!
    【解决方案2】:

    这是因为在您的 Recommendations.vue 中您引用了 A、B 和 C 变量,但您没有在数据部分中声明它们。

    因此,如果您希望它们成为变量,则需要声明它们:

    export default {
        name: 'Recommendation',
        components: {
            AppOption
        },
        data() {
            return {
               A: 'A',  
               B: 'B',
               C: 'C',      
            }
        }
    }
    

    或者,如果您只想使用 A、B 和 C 作为值,那么您不需要绑定:。 Docs

    <AppOption selected="A"></AppOption>
    <AppOption selected="B"></AppOption>
    <AppOption selected="C"></AppOption>
    

    【讨论】:

    • 我只希望 A、B、C 作为字符串,而不是变量...以对应 JSON 选项中的 A、B、C。所以我可以根据传入的字符串选项将默认设置为选项之一: [ { title: 'Skill A', value: 'A', points: 6}, { title: 'Skill B', value: 'B ',分:5},{标题:'技能C',值:'C',分:4}]
    • @yeeen 在我的回答中看到第二段代码,它可以满足您的需求。你不应该使用:如果你想要值,而不是变量。 : 表示变量绑定
    猜你喜欢
    • 1970-01-01
    • 2020-02-16
    • 2021-03-05
    • 2020-06-01
    • 2020-01-13
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多