1.创建一个路由配置数组
const routes = [
{ path: '/home', component: Home },
{ path: '/about', component: About },
{ path: '/contact', component: Contact }
];
2.使用循环遍历创建路由
const router = new VueRouter({
routes: routes.map(route => ({
path: route.path,
component: route.component,
})),
});
3.注册路由
new Vue({
router,
}).$mount('#app');
注:还可以根据实际需求添加更多的配置项,如路由别名(name)、路由重定向(redirect)等