Home > Web Front-end > Front-end Q&A > How to implement navigation in Vue

How to implement navigation in Vue

PHPz
Release: 2023-04-12 09:31:41
Original
1328 people have browsed it

Vue is a JavaScript framework suitable for developing single-page applications, so an important part is navigation. In Vue, you can use Vue Router to implement navigation. Here are the steps on how to implement navigation in Vue.

Step One: Install Vue Router

You can use npm or yarn to install Vue Router in your Vue project. In the terminal or command line, enter the following command:

npm install vue-router -save 或
yarn add vue-router
Copy after login

Step 2: Create a Vue Router instance

In the project, you need to create a Vue Router instance. Open the main.js file in the project and enter the following code:

import Vue from 'vue';
import VueRouter from 'vue-router';

Vue.use(VueRouter);

const router = new VueRouter({
  routes: [
    {
      path: '/',
      name: 'home',
      component: Home
    },
    {
      path: '/about',
      name: 'about',
      component: About
    }
  ]
});
Copy after login

What you do is import Vue and Vue Router, and create a Vue Router instance and define routes. Please note that routing is an attribute of the Vue Router instance, and the path attribute is used to specify the routing path, the name attribute is used to specify the route name, and the component attribute is used to specify the component, such as Home, About, etc.

Step 3: Set routing

In the Vue project, you need to set routing. Open the App.vue file and add the following code:

<template>
  <div>
    <nav>
      <router-link to="/">Home</router-link>
      <router-link to="/about">About</router-link>
    </nav>
    <router-view></router-view>
  </div>
</template>
Copy after login

What it does is add a link in the navigation bar using the router-link tag. The path is specified using the to attribute, in this example we have a link to "/" and "/about". In addition, use the router-view component to load the selected routing component.

Step 4: Use routing

Now that you have created a Vue Router instance, set up routing and links, you can use routing in your components. Open the Home.vue file and add the following code:

<template>
  <div>
    <h1>Home Page</h1>
    <p>Welcome to my home page!</p>
  </div>
</template>
Copy after login

Now open About.vue and add the following code:

<template>
  <div>
    <h1>About Page</h1>
    <p>Welcome to my about page!</p>
  </div>
</template>
Copy after login

Now open main.js and add the following code:

new Vue({
  router,
  render: h => h(App)
}).$mount('#app')
Copy after login

Note that we have injected the router instance into the Vue instance and used the $mount() method to mount the Vue instance on the # app element of the application.

Now restart your Vue application and navigate to the "/" and "/about" paths in your browser, you will see the Home and About pages.

In this article, you learned how to implement navigation in a Vue application. The steps include installing Vue Router, creating a Vue Router instance, setting up routing, and using routing. Now you can add navigation in your Vue application to enable users to easily navigate the application and access the required pages.

The above is the detailed content of How to implement navigation in Vue. For more information, please follow other related articles on the PHP Chinese website!

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