Vue学习之路(八) --- vue-router使用

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

1.首先安装vue-router ,在项目的文件夹中使用npm命令即npm install vue-router –save(提示:如果你在vue-cli脚手架工具中已经选择安装路由可以不用执行这一步)

这里写图片描述

2.在实战中,一般路由放在src/router/index.js中具体操作如下

这里写图片描述这里写图片描述

路由的配置,需要一个实例一个路由对象,然后在路由中配置中数组,具体如下,其中component组件需要自己import引入

new Router({
  linkActiveClass: 'active',
  routes: [
    {
      path: '/goods',
      name: 'goods',
      component: goods
    },
    {
      path: '/comments',
      name: 'comments',
      component: comments
    },
    {
      path: '/shops',
      name: 'shops',
      component: shops
    }
  ]
})

3.然后在main.js中引入router文件夹下的index.js文件。

这里写图片描述

4.最后一步一定写,就是在app.vue咧写路由插座router-view

这里写图片描述

然后你就可以在router/index.js文件里随意配置你的路由了

猜你喜欢

转载自blog.csdn.net/qq_29329037/article/details/79439390
今日推荐