Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

属性里coerce的this指向问题 #2218

Closed
ivan403704409 opened this issue Jan 25, 2016 · 2 comments
Closed

属性里coerce的this指向问题 #2218

ivan403704409 opened this issue Jan 25, 2016 · 2 comments

Comments

@ivan403704409
Copy link

在做分页组件时,我想在设置curIndex的时候对其进行校验,但我不想在watch去处理,因为这样会触发多次,但是this的指向是window,是否有可以将其指向到$vm呢?

props:{
        // 当前页
        curIndex: {
            type: Number,
            default: 0,
            coerce: function (val) {
               //this --> window
               //i want -->  $vm
               if( val < 0 ){
                    val = 0;
                }else if(val>=this.total){
                    val = this.total - 1;
                }
                return val;
            }
        }
    }
@yyx990803
Copy link
Member

不建议在 coerce 函数内做这样的操作(其本意只是用于类型转换)。另外因为 props 是先于组件本身的的 data 被初始化的,所以初始化的时候你并不能获得组件本身的 data。

可以做一个 computed property。

@hughgr
Copy link

hughgr commented Jul 17, 2016

@yyx990803 如果我希望通过props传一些配置项(比如最大值或者最小值),然后通过coerce来校验,这种情况还是有必要把this指向$vm的。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants