Home > Web Front-end > Vue.js > How to add a link in vuejs

How to add a link in vuejs

藏色散人
Release: 2021-10-20 15:26:28
Original
3532 people have browsed it

How to add links in vuejs: 1. Create html code "

"; 2. Through "navigation :function(){...}" just add the link.

How to add a link in vuejs

The operating environment of this article: Windows 7 system, Vue2.9.6, Dell G3 computer.

How to add a link in vuejs?

How to add a link in vue.js:

js code is:

navigation:function(){
            new Vue({
                el: '#navUl',
                data: {
                    menuData:{
                        '个人首页':'personal-home.html',
                        '人才分析':'personal-analysis.html',
                        '基本信息':'personal-info-base.html',
                        '技能态度':'skill-attitude.html',
                        '参与项目':'involved-project.html',
                        '学习':'learn.html',
                        '考勤':'work-attend.html',
                        '生活福利':'welfare.html'
                    }
                },
                computed:{
                    curIdx:function(){
                        var curIdx = 0;
                        $.each(this.menuData,function(k,v){
                            if(location.pathname.indexOf(v)!=-1){//说明包括(也就是当前页面)
                                return false;
                            }else{//==-1说明不包括(不是当前页面)
                                curIdx++;
                            }
                        });
                        console.log(curIdx);
                        return curIdx;
                    }
                }
            });
        }
Copy after login

html code is:

<ul class="nav-ul" id="navUl">
          <template v-for="(link,name,index) in menuData">
          <li class="nav-li" v-bind:class="index==curIdx?&#39;curr&#39;:&#39;&#39;"><a :href="link">{{ name+&#39;--&#39;+index }}</a></li>
           </template>
</ul>
Copy after login

Description: The idea is that each page corresponds to an index value. For example: when switching to the basic information page, index=2, this If curIdx is also equal to 2, then index==curIdx, add the curr class, the text turns red, and the page jump is caused by adding a link to the text, not the click event;

So when switching to the personal homepage , curIdx==0; when switching to the talent analysis page, curIdx==1; when switching to the basic information page, curIdx==2; and so on;

For the basic information page: in the js file, loop this.menuData, first of all, the current link does not include the content of the first link personal-home.html, so location.pathname.indexOf(v)==-1, at this time curIdx increases from 0 to 1;

Then in the second loop, the current link does not include the content of the second link personal-analysis.html, so location.pathname.indexOf(v)==-1, at this time curIdx increases from 1 to 2;

Then the third loop, the current link includes the content of the third link personal-info-base.html, so location.pathname.indexOf(v)!=-1, at this time it returns false, curIdx==2;

That is to say, the curIdx of the basic information page is 2; at this time, index==curIdx, add the curr class name to the current one;

Recommendation: "vue tutorial"

The above is the detailed content of How to add a link in vuejs. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template