当前位置: 移动技术网 > IT编程>脚本编程>vue.js > 详解在vue-cli中使用路由

详解在vue-cli中使用路由

2017年12月12日  | 移动技术网IT编程  | 我要评论

便便狼,迅雷云点播账号,无主之地天龙座代码

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>

 <div 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>//页面渲染放置的部分

 </div>

 

</template>

 

<script>

export default {

 name: 'app'

}

</script>

 

<style>

#app {

 font-family: 'avenir', 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 'vue'

import app from './app'

import router from './router'

 

vue.config.productiontip = false

 

/* eslint-disable no-new */

new vue({

 el: '#app',

 router,

 template: '<app/>',

 components: { app }

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

两个页面的组件

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

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

具体的路由内容可以查看:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网