Rumah  >  Artikel  >  hujung hadapan web  >  在vue-cli中使用路由的方法

在vue-cli中使用路由的方法

小云云
小云云asal
2018-05-15 10:25:401931semak imbas

本文主要和大家介绍在vue-cli中使用路由的方法,给大家做个参考,希望能帮助大家正确在vue-cli中使用路由。

1.首先npm中是否有vue-router

一般在vue-cli的时候就已经下载好了依赖包了

2.使用vue的话正常的需要涉及这几个文件

demo/src/router/index.js

import Vue from 'vue'

import Router from 'vue-router'

import Hello from '../components/Hello'//首页

import Test from '../components/test'//需要跳转的页面 给组件重新命名

 

Vue.use(Router)

 

export default new Router({

 routes: [

  {//首页

   path: '/',

   name: 'Hello',

   component: Hello

  },

  {//需要跳转的页面

    path:'/test',

    name:'test',

    component:Test//组件名字

  }

 ]

})

demo/src/app.vue

<template>

 <p id="app">

  <img src="./assets/logo.png">

  <p>

  <router-link to="/home">home</router-link>//跳转首页

  <router-link to="/test">test</router-link>//跳转新页面

  </p>

  <router-view></router-view>//页面渲染放置的部分

 </p>

 

</template>

 

<script>

export default {

 name: &#39;app&#39;

}

</script>

 

<style>

#app {

 font-family: &#39;Avenir&#39;, Helvetica, Arial, sans-serif;

 -webkit-font-smoothing: antialiased;

 -moz-osx-font-smoothing: grayscale;

 text-align: center;

 color: #2c3e50;

 margin-top: 60px;

}

</style>

demo/src/main.js

import Vue from &#39;vue&#39;

import App from &#39;./App&#39;

import router from &#39;./router&#39;

 

Vue.config.productionTip = false

 

/* eslint-disable no-new */

new Vue({

 el: &#39;#app&#39;,

 router,

 template: &#39;<App/>&#39;,

 components: { App }

}).$mount(&#39;#app&#39;)//实例挂载到元素中

两个页面的组件

这样的话,基本的路由设置就好了,可以按照正常的npm run dev运行这个项目了

另外还有嵌套 自定义多种路由

具体的路由内容可以查看:https://router.vuejs.org/zh-cn/installation.html

相关推荐:

使用路由延迟加载Angular模块实例详解

angular中使用路由和$location切换视图_AngularJS

Vue-Router2实现路由功能实例讲解

Atas ialah kandungan terperinci 在vue-cli中使用路由的方法. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn