Vue 路由及路由默认跳转

时间:2023-03-08 21:39:56
Vue 路由及路由默认跳转

路由就是让根组件动态得去挂载其他组件;

步骤:

 //路由配置:

     //.安装 

     npm install vue-router  --save   / cnpm install vue-router  --save

     //、引入并 Vue.use(VueRouter)   (main.js)

         import VueRouter from 'vue-router'

         Vue.use(VueRouter)

    //、配置路由

         、创建组件 引入组件

         、定义路由  (建议复制)

             const routes = [
{ path: '/foo', component: Foo },
{ path: '/bar', component: Bar },
{ path: '*', redirect: '/home' } /*默认跳转路由*/
] // 、实例化VueRouter const router = new VueRouter({
routes // (缩写)相当于 routes: routes
}) // 、挂载 new Vue({
el: '#app',
router,
render: h => h(App)
}) // 、根组件的模板里面放上这句话 <router-view></router-view> //、路由跳转
<router-link to="/foo">Go to Foo</router-link>
<router-link to="/bar">Go to Bar</router-link>