Home > Web Front-end > JS Tutorial > body text

How to use router in vue-cli

小云云
Release: 2018-01-15 13:32:08
Original
1926 people have browsed it

This article mainly introduces the basic usage of vue-cli router in detail. It has certain reference value. Interested friends can refer to it. I hope it can help everyone.

1. Create a new router directory in the src directory, and then create index.js


import Vue from 'vue';
import VueRouter from 'vue-router';
import goods from '@/components/goods/goods';

Vue.use(VueRouter);

export default new VueRouter({
 routes: [
  {
   path: '/',
   redirect: {name: 'goods'}
  }
});
Copy after login

The @ in the code is in webpack.base.conf.js The purpose of the modified configuration is that it is too troublesome to write a relative path every time a file such as a component is introduced. Just use @ instead. The configuration modification is as follows


 resolve: {
  extensions: ['.js', '.vue', '.json'],
  alias: {
   'vue$': 'vue/dist/vue.esm.js',
   '@': resolve('src'),
  }
 }
Copy after login

2, Introduce it into the entry file main.js and mount it on the node


import router from './router';

/* eslint-disable no-new */
new Vue({
 el: '#app',
 router,
 template: &#39;<App/>&#39;,
 components: { App }
});
Copy after login

3. Use it in the App.vue file, for example. Click to switch the route, and the content will be presented in In the router-view container


<template>
 <p id="app">
  <p class="tab">
    <router-link to="/goods" >商品</router-link>
  </p>
  <router-view></router-view>
 </p>
</template>
Copy after login

If there are three clicks to switch routes, such as products, merchants, and comments, if you want to set the style of a node currently clicked, you can set

.router-link-active {color: rgb(139,0,0);}

##Related recommendations:


vue-router routing basic instance sharing

Router solves page jumps under cross-modules

vue router imitates the bottom of Tmall Navigation bar example sharing

The above is the detailed content of How to use router in vue-cli. 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!