-
-
Notifications
You must be signed in to change notification settings - Fork 33.7k
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
hope to have a way to set scope of variable #6913
Comments
其实我很希望是这样的API: div(v-scope="some.deep.props") {{a}}, {{b}} 如果不指定要导出的变量,那么就把 |
You should really translate this one. I think, but I'm not sure, that this is exactly what I want in Vue as well. A way to declare a scope on a container. |
Translation updated. @Solander |
What about changing
into
|
… set local variable in template fix vuejs#6913
hope to get more feedback about this feature :XD |
I also would like to be able to use the mentioned syntax:
To be able to access deep properties without having to use long names like:
|
How about some sort of <template>
<div>
<!-- Object-style syntax, similar to `v-bind` -->
<template v-let="{ props: some.deep.props }">
<span>{{ props.a }}</span>
<span>{{ props.a }}</span>
</template>
</div>
</template> <template>
<div>
<!-- Single-value syntax, similar to `v-bind:arg` -->
<template v-let:props="some.deep.props">
<span>{{ props.a }}</span>
<span>{{ props.a }}</span>
</template>
</div>
</template> JavaScript's |
If you declare a |
@AlexandreBonneau No, that doesn't make sense. |
My thoughts are that you would still need a computed property or method in most cases for this where the data is coming from some api b/c there will be possibility of null/undefined. Keeping a separation between the template and presenting the data will be marginally more verbose but the gains are clarity and also reduction in size of vue core. |
Userland solution which works, but I highly recommend not to use it "as is". (May be useful when you can move some logic into separate component which will make template more cleaner) |
@OEvgeny This is a nice workaround, thanks. However, a simpler |
note that xsl has the concept of const variable. |
个人习惯和风格可能都不太一样,清晰明朗,简洁易用,就是好框架,感觉用法还是不错的,很符合现在的代码编程习惯 |
Any updates on this? Just ran into this as well... |
I was looking for a solution too... While I found only this:
|
Using the export default {
//...
//fake
localVariables: {
myvar: {}
}
}; However, having to name very local var in the component seems a bit too verbose.
|
Is it works now? Can you show a complete example with local variable in template? |
No, it does not work now, as we are discussing options to implement (or not) local variables! The point of my previous comment was to explain how I think having to declare the local variables in the |
I agree with you. |
Another way:
|
A v-let or v-local or something similar would be pretty awesome. Some constellations force me to write a myriad of components with only a very small template and maybe one computed getter, introducing lots of boilerplate that could be avoided with a v-let. |
Our new RFC repo https://github.com/vuejs/rfcs is dedicated to such feature request. |
Sorry, Chinease only. I'll try to translate later.What problem does this feature solve?
有时我希望在组件某区域渲染数据,它们都来自于data上比较深层的某一个属性,当数据量比较多时,直接使用文本插值的写法会比较繁琐,比如:
(以下为了精简代码,采用了pug、es6语法,并忽略Vue组件data必须为函数的要求)
The mustache interpolations of data in some deep props make the code cumbersome:
data.js
my-page.vue
我想到几种变通的做法:
I tried some solutions:
使用计算属性:Use computed properties:
my-page.vue
这样做相对简单,但是总要为此去定义很多计算属性似乎也有些繁琐。
It is easy to use, but the computed properties become cumbersome.
利用
v-for
指令来缩小作用域:Usev-for
directive to define a scoped variable:my-page.vue
这是一个取巧的做法,但也有副作用:用
$refs
访问v-for
范围内的ref
引用会变成数组。It almost works well, but have side-effect:
$refs
in thev-for
scope will become an array.将数据渲染区域定义为子组件,子组件接受简化后的数据:Use child component:
data-view.vue
my-page.vue
子组件内部的写法能简化许多,但是整体花销仍然不小。一个额外的问题是,子组件多引入了一个可能原本并不必要的包装根元素。
Interpolations in child component become simple, but more children is also cumbersome. In addition, the child need a wrapper element as the component root, which was not needed before.
利用子组件作用域插槽来封装一个通用的辅助组件:Use the scoped slot to make a reusable component:
sub-scope.vue
my-page.vue
这也是取巧利用了子组件来改变访问作用域。可以复用看上去好像是一个优点,但实际使用还是比较繁琐,甚至代码看上去有些更乱了。与上一个方法同样存在的一个问题是,子组件引入了可能原本并不必要的包装根元素。
Only the interpolations look like simpler, but other codes become more complex.
What does the proposed API look like?
我感觉利用
v-for
指令的办法很巧妙,就是多了一些副作用,虽然也不是非常严重的影响。因此我希望能有一个指令,能够如同
v-for
一样定义一个局部作用域变量,但又不会影响$refs
引用的表现。I hope there is a directive can define scoped variables like
v-for
, but will not affect the performance of$refs
.或者可以提供两种
ref
元素访问接口:Or the
ref
API should be optimized:$refs
仅用于访问指定引用名的第一项,类似于querySelector()
。$refs
should only be used for reference the first of elements with the sameref
value, likequerySelector()
.$groupedRefs
(我想也许还有更好一点的名字)则用于访问包含相同引用名的全部元素数组,类似于querySelectorAll()
。$groupedRefs
is used for reference the array of the elements with the sameref
value, likequerySelectorAll()
.The text was updated successfully, but these errors were encountered: