Home > Web Front-end > JS Tutorial > How to set up routing in Vue.js

How to set up routing in Vue.js

php中世界最好的语言
Release: 2018-03-13 14:36:25
Original
2399 people have browsed it

This time I will show you how to set up routing in Vue.js, and what are the notes for setting up routing in Vue.js. Here is a practical case, let’s take a look.

① Routing map

Import vue-router in main.js

import VRouter from 'vue-router'

Set global routing

Vue.use(VRouter)

Instantiate router

let router = new VRouter({ // 如果mode设为history, 那么地址就可以不使用哈希(# 哈希)了,就可以直接访问. http://localhost:8080/#/apple ==>> http://localhost:8080/apple
  mode: 'history',  routes: [    //  做一个映射表
    {      path: '/apple',      component: Apple
    },
    {      path: '/banana',      component: Banana
    }
  ]
})
/* eslint-disable no-new */new Vue({  el: '#app',
  router,
  template: '<app></app>',
  components: { App }
})
Copy after login

②RoutingView

Embed

<template>  <div>
    ![](./assets/logo.png)    <!--
    访问apple的时候,将apple的视图塞到这个位置
    访问banana的时候,将banana的视图塞到这个位置
    -->
    <router-view></router-view>
  </div></template>
Copy after login

Achieve effect

How to set up routing in Vue.js

③ RouteNavigation

In In the app.vue file, embed the router-link tag, which can achieve the effect of a tag

to apple-link>

Specific use:

<template>
  <div id="app">
    ![](./assets/logo.png)    <!--
    访问apple的时候,将apple的视图塞到这个位置
    访问banana的时候,将banana的视图塞到这个位置
    -->
    <router-view></router-view>
    <router-link :to="{path:&#39;apple&#39;}">to apple</router-link>
    <router-link :to="{path:&#39;banana&#39;}">to banana</router-link>
  </div></template>
Copy after login

Effect:

How to set up routing in Vue.js

I believe you have mastered the method after reading the case in this article, For more exciting content, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Vue tag attributes and conditional rendering of Vue.js

List rendering v of Vue.js -for array object subcomponent

Text rendering of Vue.js

The above is the detailed content of How to set up routing in Vue.js. 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