Vue.js框架--路由模块化(二十六)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/hlx20080808/article/details/83507209

主要操作技能:

  1>创建文件夹\router.js文件

  
  2>写入相关路由配置 router.js

import Vue from 'vue'

//0. 使用路由
import VueRouter from 'vue-router'
Vue.use(VueRouter)

//1. 定义 (路由) 组件。
import Home from '../components/Home.vue'
import News from '../components/News.vue'
import Buy from '../components/Buy.vue'

//2. 定义路由
const routes = [{
		path: '/home',
		component: Home
	},
	{
		path: '/news',
		component: News
	},
	{
		path: '/buy',
		component: Buy
	},
	{ //默认首页不能有参数
		path: '*',
		redirect: '/home'
	}
]

//3. 创建 router 实例,然后传 `routes` 配置
const router = new VueRouter({
	routes // (缩写) 相当于 routes: routes
})

//4.导出路由
export default router;


  3>在main.js中导入   import router from './router/router.js'

猜你喜欢

转载自blog.csdn.net/hlx20080808/article/details/83507209