简体   繁体   English

Vue.js路由器链接动态更改

[英]Vuejs router-link changes dynamically

I have multiple templates in my project, with a fairly simple url structure. 我的项目中有多个模板,URL结构非常简单。

/startpage/

/startpage/test_1

/startpage/test_2

In my 'App.vue' I made a template, which is shown on every page in my project. 在我的“ App.vue”中,我制作了一个模板,该模板显示在项目的每个页面上。 That template includes a button, which is supposed to act like a 'Home' button, named 'Projects'. 该模板包括一个按钮,该按钮的作用类似于“主页”按钮,名为“项目”。

App.vue App.vue

<template>
  <div>
    <div>
      <router-link :to="/startpage/"><button class="Project">Projects</button></router-link>
    </div>
    <router-view/>
  </div>
</template>

When I'm on the startpage ( localhost:4545/#/startpage/ ), the button has the target localhost:4545/#/startpage/ . 当我进入起始页( localhost:4545/#/startpage/ )时,按钮具有目标localhost:4545/#/startpage/

But when I'm on another page, for example localhost:4545/#/startpage/test_1 , the button suddenly has the same url as the page I'm on. 但是当我在另一个页面上时,例如localhost:4545/#/startpage/test_1 ,按钮突然具有与我所在页面相同的URL。

Why does the router change the link dynamically and not keep the target /startpage/ ? 为什么路由器动态更改链接而不保留目标/startpage/

As described in the documentation , you either need to use binding or not: 文档所述 ,您是否需要使用绑定:

<!-- literal string -->
<router-link to="home">Home</router-link>
<!-- renders to -->
<a href="home">Home</a>

<!-- javascript expression using `v-bind` -->
<router-link v-bind:to="'home'">Home</router-link>

<!-- Omitting `v-bind` is fine, just as binding any other prop -->
<router-link :to="'home'">Home</router-link>

So it should be that you don't need to use : , or use a string literal. 所以应该是您不需要使用: 使用字符串文字。 Currently it tries to use it as a variable, which of course it not present. 当前,它尝试将其用作变量,当然它不存在。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM