vue 创建路由

瘦欲@ 提交于 2020-02-17 01:29:53
路由
	1、安装
		cnpm install vue-router -S

	2、在创建路由的js中引入
		import Vue from 'vue'
		import VueRouter from 'vue-router'
		Vue.use(VueRouter)

	3、创建路由
		需要创建new 路由实例对象
		const router =/export default new VueRouter({
			routes:[  //不是routers
				{
					path:'',
					name:"",
					component:  //使用前先引入,直接组件名,不加引号

				}
			]
		})

	4、在主入口组件中(App.vue)使用

		<router-view />

	5、在main.js中
		(1)引入路由文件
			import router from './router'
			
		(2)在vue的实例化对象中添加route
		
			new Vue({
			  el: '#app',
			  router,
			  components: { App },
			  template: '<App/>'
			})

代码示例:
main.js:

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import Axios from 'axios'
import VueRouter from 'vue-router'
import HelloWorld from './components/HelloWorld'

Vue.use(VueRouter)
Vue.config.productionTip = false


const router=new VueRouter({
	routes:[
		{
			path:'/hello',
			name:"HelloWorld",
			component:HelloWorld

		}
	]
})

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


App.vue:

<template>
  <div id="app">
    <img src="./assets/logo.png">
    <router-view />
  </div>
</template>

<script>

export default {
  name: 'App',
  data()
  {
    return{
      
    }
  }
</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>

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!