美文网首页
VUE----脚手架2.0下路由的使用

VUE----脚手架2.0下路由的使用

作者: JuMinggniMuJ | 来源:发表于2020-09-21 21:53 被阅读0次

vue-router是vue.js官方的路由管理器
(我测试使用脚手架版本是Vue 2,环境是win10)

1.下载vue-router:
npm install vue-router --save
2.准备工作:

1.在src目录下新建view文件夹

2.在src/view目录下创建4个组件,以供路由跳转测试使用 组件结构 3.组件代码示例:
<template>
    <div>
        <h1>我是首页组件</h1>
    </div>
</template>

<script>
    export default {
        name:'Index'
    }
</script>

<style scoped></style>
3.创建路由文件:

1.在src目录下新建router文件夹
2.在router文件夹下新建index.js文件:

//1.引入vue和路由..
        import Vue from 'vue'
        import VueRouter from 'vue-router'
//2.注册路由..
        Vue.use(VueRouter)
//3.使用懒加载引入组件..
        const Index   = () =>import('../view/tabbar/Index')
        const Type    = () =>import('../view/tabbar/Type')
        const Cart    = () =>import('../view/tabbar/Cart')
        const Profile = () =>import('../view/tabbar/Profile')
//4.定义路由映射..
        const routes = [
            {
                path:'',
                redirect:'/index'        //初始页重定向到首页
            },
            {
                path:'/index',
                component:Index
            },
            {
                path:'/type',
                component:Type
            },
            {
              path:'/cart',
               component:Cart
            },
            {
                path:'/profile',
                component:Profile
            }
        ]
//5.实例化路由..
        const router = new VueRouter({
            routes,
            mode:'history'        //默认hash,将其修改成history
      })
//6.导出路由..
        export default router
4.引用路由:
在main.js中引用路由: 引用截图
5.使用路由:

vue的路由并不是a标签,而是router-link,并且需要设置router-view容器,示例代码:

<div>
        <router-link to="/index">首页|</router-link>
        <router-link to="/type">分类|</router-link>
        <router-link to="/cart">购物车|</router-link>
        <router-link to="/profile">我的</router-link>
        <router-view></router-view>
</div>
样式: 样式截图
6.至此一个最基本的路由使用就完成了

相关文章

  • VUE----脚手架2.0下路由的使用

    vue-router是vue.js官方的路由管理器(我测试使用脚手架版本是Vue 2,环境是win10) 1.下载...

  • React(脚手架)

    第一次使用脚手架,先学习路由,回头我们用脚手架针对api有时间我再撸一遍 脚手架的安装,会使用yarn的建议使用y...

  • react脚手架

    第一次使用脚手架,先学习路由,回头我们用脚手架针对api有时间我再撸一遍 脚手架的安装,会使用yarn的建议使用y...

  • Router路由

    基本用法: 详细用法 在脚手架组件中使用路由: main.js:

  • VUE----脚手架3使用axios

    Axios 是一个基于 promise 的 HTTP 库,也是vue推荐使用的网络请求框架 1.下载axios: ...

  • 03.vue-router路由(一级路由、二级路由、路由声明式导

    路由使用的module是vue-router,在脚手架创建项目的时候,已经下载好了 一级路由配置 1.配置路由文件...

  • uni-app项目开发笔记

    开发工具:使用HBuilderX开发;使用脚手架快速搭建;pages.json 页面路由配置。标题,全局窗口样式;...

  • VUE----使用脚手架创建vue项目

    学习了vue的基础之后尝试使用脚手架创建一个vue项目 1.全局安装vue脚手架: 安装完成之后,vue脚手架文件...

  • Vue路由

    Vue作为单页面应用的框架,路由是必不可少的。现在也很多引入vuejs去使用,今天只说脚手架创建的项目的路由。 我...

  • vue2.0+router路由

    1、搭建vue脚手架2、通过vue脚手架,我们搭建好了基础的vue,接下来就开始使用vue-cli来构建基础路由项...

网友评论

      本文标题:VUE----脚手架2.0下路由的使用

      本文链接:https://www.haomeiwen.com/subject/ozpbyktx.html