ninestep
V2EX  ›  Vue.js

vue 组件数据错误

  •  
  •   ninestep · Aug 26, 2017 · 2954 views
    This topic created in 3207 days ago, the information mentioned may be changed or developed.

    目前刚刚开始学习 vue,编写了一个小组件,但是这个组件再一个地方可以用,但是再其他地方会发生赋值错误

    ```
    
    ```
    var num = {
    props: ['index', 'num', 'min', 'max', 'step'],
    template: '<div><span class="jia" @click="change(-1,min,max)">-</span>\n' +
    '<input class="num" v-model="count" :value="count">\n' +
    '<span class="jian" @click="change(1,min,max)">+</span></div>',
    data: function () {
        return {
            count: this.num,
            cindex: this.index,
        }
    },
    methods: {
        change: function (type, min, max) {
            var num1 = this.count + type * this.step;
            if (num1 >= min && num1 <= max) {
                this.count = num1;
                this.$emit('change', [this.index, num1]);
            }
        },
    }
    

    };

    ```
    
    ```
    其中正确的地方我是这么调用的
    
    <num :index="index" v-on:change="change" :num="item.num" :min="item.minimum" :max="item.goods_stock" step=1></num>
    
    

    错误的地方

    <num  v-on:change="change" :index="1" :num="goodsInfo.minimum" :min="goodsInfo.minimum" :max="goodsInfo.goods_stock" step=1></num>
    
    

    其中 index 因为没有直接赋值一个固定的数

    这是为啥了,一个可以成功赋值,另一个 vue 组件内部的 count 是 undefined 状态

    3 replies    2017-09-10 02:18:10 +08:00
    cephei
        1
    cephei  
       Aug 26, 2017
    props 要传的是变量
    stiekel
        2
    stiekel  
       Aug 26, 2017
    问题在这里:
    :index="1"
    改为
    index="1"
    SilentDepth
        3
    SilentDepth  
       Sep 10, 2017
    如果没有其他报错的话,这段代码看起来并没有问题。组件的 count 只与属性 num 有关,第 2 个例子里属性 num 被传入 goodsInfo.minimum,确定这个变量是有值的吗?
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   941 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 18:30 · PVG 02:30 · LAX 11:30 · JFK 14:30
    ♥ Do have faith in what you're doing.