V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
ninestep
V2EX  ›  Vue.js

vue 组件数据错误

  •  
  •   ninestep · 2017-08-26 14:56:32 +08:00 · 2216 次点击
    这是一个创建于 2406 天前的主题,其中的信息可能已经有所发展或是发生改变。

    目前刚刚开始学习 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 条回复    2017-09-10 02:18:10 +08:00
    cephei
        1
    cephei  
       2017-08-26 17:07:03 +08:00
    props 要传的是变量
    stiekel
        2
    stiekel  
       2017-08-26 18:39:41 +08:00
    问题在这里:
    :index="1"
    改为
    index="1"
    SilentDepth
        3
    SilentDepth  
       2017-09-10 02:18:10 +08:00
    如果没有其他报错的话,这段代码看起来并没有问题。组件的 count 只与属性 num 有关,第 2 个例子里属性 num 被传入 goodsInfo.minimum,确定这个变量是有值的吗?
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5388 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 01:31 · PVG 09:31 · LAX 18:31 · JFK 21:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.