Discussion icon indicating copy to clipboard operation
Discussion copied to clipboard

Ho To give dynamic href in html when using vuejs

Open Anshul-Batra opened this issue 10 years ago • 6 comments

Hi have a v-repeat on big object


<div v-repeat="obj : mainObj">
<a href="#{{obj.name}}"></a>
</div>

I want to give href value dynamically based on the iteration of object in vuejs. The obj has a property name but name is ofcource more than one word. When i give href as name it also takes space. like if name is "john clark" the href value will be "#john clark". This stops my functionality to work.

How can i trim spaces from name while giving it in href?

Please suggest

Anshul-Batra avatar Apr 15 '15 14:04 Anshul-Batra

You can make a filter:

<a href="#{{obj.name | trimSpaces}}"></a>

yyx990803 avatar Apr 17 '15 02:04 yyx990803

Hi @yyx990803 evan, I try to make filter inside href attribute in vue 2.0 but it doesnt work. I'm using <a href="#{{product.id | formatParam(product.category,product.count)}}"></a> but it doesnt work. So I change my code into this

<a :href="product.id | formatParam(product.category,product.count)"></a>

And still doesnt work, why?. The error message is

formatParam is not defined on the instance but referenced during render

Any solutions? Thanks

raisrahman avatar Oct 26 '16 10:10 raisrahman

Filters have been replaced with computed properties in Vue 2.0. Take a look here: http://vuejs.org/guide/list.html

humanismusic avatar Oct 26 '16 22:10 humanismusic

@raisrahman You can refer to:

        <el-table-column label="Path" min-width="195">
          <template scope="props">
            <a v-bind:href="''+props.row.path+''">{{ props.row.path }}</a>
          </template>
        </el-table-column>

it ′ s work

zibuyu1995 avatar Aug 14 '17 02:08 zibuyu1995

@zibuyu1995 Thx! That's what I looking for~

lluma avatar Feb 17 '18 11:02 lluma

Any way to do this without string concatenation? Preferably something along the lines of template literal

sschadwick avatar Feb 20 '18 20:02 sschadwick