Vue: v-for里面可否添加对 this 的访问 API

Created on 28 Dec 2017  ·  4Comments  ·  Source: vuejs/vue

What problem does this feature solve?

发现v-for里面无法访问 vm 实例,this不存在。
我想进行一个对数字迭代,并按照数字序号进行 vm 上的data数据读取,渲染。可是发现出现没有this的报错。
例如:



  • {{this.listData['value' v]}}


是否有其他办法?
现在解决办法是使用 methods 传参,暂时还不知道副作用,或者 vuex 的 getters 传参。
另外,有些数据是永远不可变的,
是否可以配置一种 不被转化为 get , set 的 data,
以免初始化时候浪费初始化遍历数据的性能开销。暂时用的是this.$options,
可是书写的有点长而且不能表达"这是数据"的意思。

What does the proposed API look like?

我想获得一种可以在v-for里面读取this的方法,
可以取名叫self或其他或叫$this等。
获得一种可以声明"死"的数据的接口,
可以取名叫 slotData,或者 其他。
可以与 data ,computed props 平级待遇,但是不具备双向绑定。

All 4 comments

你跟我问道了同样的问题 你可以看#7338 我的issue被close了

  1. don't use this in templates
  2. The code you shared is not a valid vue template, it contains invalid Javascript.
  3. Your issue is obviously not a feature request, you are abusing it to ask a question, which is against our issue guidelines. If this is meant as a bug report, it's missing the required runnable reproduction

我们的中文支持论坛就在这里: https://forum.vuejs.org/c/chinese

First of all, this variable seems nothing related to your proposal about constant data.

Second, it has been discussed about constant data in https://github.com/vuejs/vue/issues/6004, and you can do it by your self with plugin/mixin very easy.

无绑定的数据一样可以直接读取,不需要 this。整个 template 默认就是以 vm 为 context。

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ShuvoHabib picture ShuvoHabib  ·  40Comments

ecmel picture ecmel  ·  52Comments

smolinari picture smolinari  ·  116Comments

asiFarran picture asiFarran  ·  34Comments

yyx990803 picture yyx990803  ·  36Comments