乐闻世界logo
搜索文章和话题

How add a href inside nuxt-link ?

1个答案

1

In Nuxt.js, the nuxt-link component is the preferred method for internal routing within Vue applications. It leverages the functionality of but is optimized for Nuxt.js framework's page and routing system. Typically, nuxt-link does not use the href attribute but instead uses the to attribute to specify the target route.

Basic Syntax of nuxt-link:

html
<nuxt-link to="/about">About Us</nuxt-link>

Here, the to attribute specifies the route path of the link target. It works similarly to the href attribute in a standard tag but is better suited for Vue's reactive routing system.

Dynamic Routing:

If you need to create dynamic routes, you can use it as follows:

html
<nuxt-link :to="`/user/${userId}`">User Information</nuxt-link>

Complete Example:

Assume we are building a blog platform where we need to navigate from the blog list to the blog detail page:

html
<template> <div> <h1>Blog List</h1> <ul> <li v-for="post in posts" :key="post.id"> <nuxt-link :to="`/posts/${post.id}`">{{ post.title }}</nuxt-link> </li> </ul> </div> </template> <script> export default { data() { return { posts: [ { id: 1, title: "Fundamentals of Nuxt.js" }, { id: 2, title: "How to Use Vue" }, { id: 3, title: "Importance of Frontend Routing" } ] }; } } </script>

In this example, each nuxt-link uses :to to dynamically bind to the specific route of each article. This ensures that clicking any link navigates users to the corresponding article detail page.

Summary:

By using the above methods, you can effectively implement internal navigation in your Nuxt.js application while maintaining performance and user experience.

2024年7月31日 00:36 回复

你的答案