Home Web Front-end Front-end Q&A What is vue's navigation link component?

What is vue's navigation link component?

Dec 15, 2022 pm 12:15 PM
vue components

Vue’s navigation link component is “router-link”. The "" component supports users to click to navigate in applications with routing functions and specify the target address through the to attribute. The syntax is "...-link>"; the default rendering is the "" tag with the correct connection, and other tags can be generated by configuring the tag attribute.

What is vue's navigation link component?

The operating environment of this tutorial: windows7 system, vue3 version, DELL G3 computer.

Vue’s navigation link component is “router-link”.

vue component router-link introduction

component supports users to click in applications with routing functions navigation. Specify the target address through the to attribute. The default rendering is the tag with the correct connection. You can generate other tags by configuring the tag attribute. In addition, when the target route is successfully activated , the link element automatically sets a css class name indicating activation

Where is router-link?

  • The router-link in the main page shown here

  • So it is in App.vue (why is the main page written in In App, shouldn't it be written in index.html? No no no, here is to render the content obtained from this page to the main page) [Related recommendations: vuejs introductory tutorial, web front-end

  • Write it wherever you like in App.vue

What is vues navigation link component?

##How to use router-link

  • You need to create the corresponding components

  • You need to Write the corresponding routing address of your component

  • Write the corresponding (link) on the App.vue page

Write the routing and import the component The detailed process is in the article above. Here is a brief introduction

1. You need to create the corresponding components

What is vues navigation link component?

2. You need to Write the corresponding routing address for your component

What is vues navigation link component?

3. Write the corresponding (link)

What is vues navigation link component? on the App.vue page

Result display:

What is vues navigation link component?

Introduction to the content inside it

<router-link to="/">Home</router-link>
Copy after login

The to attribute here specifies the path to jump the route (the address in the address bar ).

  • The to in this must be consistent with the path you specify, and is not case-sensitive

  • Additional: a tag is not allowed, because It reopens a tab, and it can also achieve that effect, but it doesn’t feel as friendly as router-link

Extended knowledge: router- Attributes of link

The attributes of the component are:

to, replace, append, tag, active-class, exact, event, exact- active-class

1, to (required parameter): type string/location

represents the link of the target route. The value can be a string or It is a dynamically bound object that describes the target location

The following examples

//下面是字符串的形式
<router-link to="home">Home</router-link>

//下面几种为动态绑定,v-bind: 可以简写为:


<router-link :to="&#39;index&#39;">Home</router-link>

/*但注意这个组件的导出需要有类似下面的代码
export default {
  name: &#39;App&#39;,
  data(){
    return {
      index:&#39;/&#39;
    }
  }
}
*/

<router-link :to="{ path: &#39;/home&#39; }">Home</router-link>
/*
这个路径就是路由中配置的路径
*/
<router-link :to="{ name: &#39;User&#39;}">User</router-link>
/*
  在路由的配置的时候,添加一个name属性,例如:
 routes: [
    {
      path:&#39;/home&#39;,
      name:&#39;User&#39;,
      component:home
    }
]
*/
Copy after login

2, tag

Type: string

Default value: "a"

If you want to be rendered into some kind of label, such as
  • . So we use the tag prop class to specify what kind of tags, and it will still listen for clicks and trigger navigation.

          <router-link :to="&#39;index&#39;"
                       tag="li"
                       event="mouseover">Home
          </router-link>
    Copy after login

    If we want to add an a tag to this li tag at this time, as shown below, we can not add the href attribute to the a tag.

            <router-link :to="{name:&#39;Home&#39;}" tag="li">
            <a>Home</a>
            </router-link>
    Copy after login

    In this case, will be used as the real link (it will get the correct href), and the "activation CSS class name" will be set to the outer
  • .

    3, active-class

    Type: string

    Default value: "router-link-active"

    Set the link The CSS class name to use when activating. The default value can be configured globally via the route's construction option linkActiveClass.

    <router-link :to="{path:&#39;/about&#39;}"
                       active-class="activeClass"                  
          >about</router-link>
    Copy after login

    The default value is configured globally through the route's construction option linkActiveClass, as shown in the following example:

    export default new Router({
      mode:&#39;history&#39;,
      linkActiveClass:&#39;is-active&#39;,
      routes: [
        {
          path:&#39;/about&#39;,
          component:about
        }
    ]
    })
    Copy after login

    4, exact-active-class

    type: string

    Default value: "router-link-exact-active"

    配置当链接被精确匹配的时候应该激活的 class。注意默认值也是可以通过路由构造函数选项 linkExactActiveClass 进行全局配置的。

    5、exact

    类型: boolean

    默认值: false

    "是否激活" 默认类名的依据是 inclusive match (全包含匹配)。 举个例子,如果当前的路径是 /a 开头的,那么 也会被设置 CSS 类名。

    按照这个规则,每个路由都会激活!想要链接使用 "exact 匹配模式",则使用 exact 属性:

            <li><router-link to="/">全局匹配</router-link></li>
            <li><router-link to="/" exact>严格匹配</router-link></li>
    Copy after login

    简单点说,第一个的话,如果地址是/aa,或/aa/bb,……都会匹配成功,

    但加上exact,只有当地址是/时被匹配,其他都不会匹配成功

    6、event

    类型: string | Array

    默认值: 'click'

    声明可以用来触发导航的事件。可以是一个字符串。

    <router-link to="/document" event="mouseover">document</router-link>
    Copy after login

    如果我们不加event,那么默认情况下是当我们点击document的时候,跳转到相应的页面,但当我们加上event的时候,就可以改变触发导航的事件,比如鼠标移入事件

    7、replace

    类型: boolean

    默认值: false

    设置 replace 属性的话,当点击时,会调用 router.replace() 而不是 router.push(),于是导航后不会留下 history 记录。

    8、append

    类型: boolean

    默认值: false

    设置 append 属性后,则在当前 (相对) 路径前添加基路径

    【相关推荐:vuejs视频教程

    The above is the detailed content of What is vue's navigation link component?. For more information, please follow other related articles on the PHP Chinese website!

  • 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

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Article

    Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
    Nordhold: Fusion System, Explained
    3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1664
    14
    PHP Tutorial
    1268
    29
    C# Tutorial
    1248
    24
    How to use bootstrap in vue How to use bootstrap in vue Apr 07, 2025 pm 11:33 PM

    Using Bootstrap in Vue.js is divided into five steps: Install Bootstrap. Import Bootstrap in main.js. Use the Bootstrap component directly in the template. Optional: Custom style. Optional: Use plug-ins.

    How to add functions to buttons for vue How to add functions to buttons for vue Apr 08, 2025 am 08:51 AM

    You can add a function to the Vue button by binding the button in the HTML template to a method. Define the method and write function logic in the Vue instance.

    How to use watch in vue How to use watch in vue Apr 07, 2025 pm 11:36 PM

    The watch option in Vue.js allows developers to listen for changes in specific data. When the data changes, watch triggers a callback function to perform update views or other tasks. Its configuration options include immediate, which specifies whether to execute a callback immediately, and deep, which specifies whether to recursively listen to changes to objects or arrays.

    How to return to previous page by vue How to return to previous page by vue Apr 07, 2025 pm 11:30 PM

    Vue.js has four methods to return to the previous page: $router.go(-1)$router.back() uses &lt;router-link to=&quot;/&quot; component window.history.back(), and the method selection depends on the scene.

    What does vue multi-page development mean? What does vue multi-page development mean? Apr 07, 2025 pm 11:57 PM

    Vue multi-page development is a way to build applications using the Vue.js framework, where the application is divided into separate pages: Code Maintenance: Splitting the application into multiple pages can make the code easier to manage and maintain. Modularity: Each page can be used as a separate module for easy reuse and replacement. Simple routing: Navigation between pages can be managed through simple routing configuration. SEO Optimization: Each page has its own URL, which helps SEO.

    React vs. Vue: Which Framework Does Netflix Use? React vs. Vue: Which Framework Does Netflix Use? Apr 14, 2025 am 12:19 AM

    Netflixusesacustomframeworkcalled"Gibbon"builtonReact,notReactorVuedirectly.1)TeamExperience:Choosebasedonfamiliarity.2)ProjectComplexity:Vueforsimplerprojects,Reactforcomplexones.3)CustomizationNeeds:Reactoffersmoreflexibility.4)Ecosystema

    How to reference js file with vue.js How to reference js file with vue.js Apr 07, 2025 pm 11:27 PM

    There are three ways to refer to JS files in Vue.js: directly specify the path using the &lt;script&gt; tag;; dynamic import using the mounted() lifecycle hook; and importing through the Vuex state management library.

    How to use vue traversal How to use vue traversal Apr 07, 2025 pm 11:48 PM

    There are three common methods for Vue.js to traverse arrays and objects: the v-for directive is used to traverse each element and render templates; the v-bind directive can be used with v-for to dynamically set attribute values ​​for each element; and the .map method can convert array elements into new arrays.

    See all articles