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

Using filters inside attributes with Vue 2.0 #4080

Closed
callumacrae opened this issue Nov 1, 2016 · 12 comments
Closed

Using filters inside attributes with Vue 2.0 #4080

callumacrae opened this issue Nov 1, 2016 · 12 comments

Comments

@callumacrae
Copy link

Both of the following work in Vue 1:

<a href="{{ 'something' | linkHandler }}">link</a>
<a :href="'something' | linkHandler">link</a>

Neither of them work in Vue 2.0, because you can only use filters inside interpolations, and you can't use interpolations as attributes anymore.

Any way around this?

@happyDemon
Copy link

You could use a computed property

@callumacrae
Copy link
Author

Kinda defeats the point in using a filter, though. I don't want to declare 30 computed properties, and I don't want to declare duplicate methods across multiple components.

@trailskr
Copy link
Contributor

trailskr commented Nov 1, 2016

@callumacrae declare method in mixin (and use it) or in Vue.prototype like Vue.prototype.$linkHandler = function (value) {....} and use <a :href="$linkHandler('something')">link</a>

@sqal
Copy link
Contributor

sqal commented Nov 1, 2016

#2756 (comment)

@callumacrae
Copy link
Author

@iagafonov: How is that not a workaround? What are filters for?

I'd suggest that you should either be able to be able to use filters everywhere—which would mean allowing interpolations as attributes when there is a filter—or filters should be removed. The current behaviour is confusing.

@yyx990803
Copy link
Member

yyx990803 commented Nov 1, 2016

This has been discussed extensively during the 2.0 API design thread - please read that thread, most of the arguments have been raised already. Filters were originally to be removed but added back for the most common use case due to community request. It's not going to change in the foreseeable future.

@nghiepdev
Copy link

It work with <a :href="$options.filters.linkHandler('somthings')">link</a>

@callumacrae
Copy link
Author

Thank you for changing your mind on this 🙌 🙌 🙌

@vijaypws
Copy link

vijaypws commented Mar 12, 2019

Its not working for me.
my code is :

<p v-html="getHtml('status')"></p>

I want to apply caps helper.

i have columns like body, title and status, and i want to apply only on status column.

i tried this :

<p v-html="$options.filters.caps(getHtml('status'))"></p>

any idea ?

@adamreisnz
Copy link

If the $options.filters.xxxxx workaround works, then why on earth make us jump through hoops and not just support filters directly in attributes?

@michtio
Copy link

michtio commented Apr 23, 2020

Also I see a lot of use for filters in attributes ... certain words going to lowercase as css class for example

@posva
Copy link
Member

posva commented Apr 23, 2020

This is quite old but filters can be used in v-bind directives as well as text interpolation: https://vuejs.org/v2/guide/filters.html

@vuejs vuejs locked as resolved and limited conversation to collaborators Apr 23, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

10 participants