Vue.js框架路由使用方法实例详解


Posted in Javascript onAugust 25, 2017

Vue.js框架路由使用方法实例详解

html代码:

<!DOCTYPE html>
<html>

  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name='viewport' content='width=device-width,initial-scale=1,maximum-scale=1,user-scalable=no'>
    <link href="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/css/bootstrap.min.css" rel="external nofollow" rel="stylesheet">
    <script src="http://cdn.static.runoob.com/libs/jquery/2.1.1/jquery.min.js"></script>
    <script src="http://cdn.static.runoob.com/libs/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    <link rel="stylesheet" type="text/css" href="../css/css.css" rel="external nofollow" />
    <script src="../lib/vue.min.js" type="text/javascript"></script>
    <script src="../lib/vue-router.js" type="text/javascript" charset="utf-8"></script>
    <title>模仿app页面</title>
  </head>

  <body>
    <div id="app">
      <router-view v-bind:info='info' v-bind:pic='pic' v-bind:icon='icon'></router-view>
      <!--底部导航-->
      <div id="nav">
      <ul class="nav nav-pills">
        <li class="active">
          <a v-on:click="show(0)"><span class="glyphicon glyphicon-home" aria-hidden="true"></span> 首页</a>
        </li>
        <li >
          <a v-on:click="show(1)"><span class="glyphicon glyphicon-comment" aria-hidden="true"></span> 消息</a>
        </li>
        <li>
          <a v-on:click="show(2)"><span class="glyphicon glyphicon-shopping-cart" aria-hidden="true"></span> 购买</a>
        </li>
        <li>
          <a v-on:click="show(3)"><span class="glyphicon glyphicon-user" aria-hidden="true"></span> 我的</a>
        </li>
      </ul>
      </div>
    </div>
    <!--视图页面-->
    <script type="text/x-template" id='home'>
      <div>
        <!--头部-->
        <div id="header">
          <h5>这是什么app</h5>
        </div>
        <!--轮播图-->
        <div id="carousel-example-generic" class="carousel slide" data-ride="carousel">
          <!-- Indicators -->
          <ol class="carousel-indicators">
            <li data-target="#carousel-example-generic" data-slide-to="0" class="active"></li>
            <li data-target="#carousel-example-generic" data-slide-to="1"></li>
            <li data-target="#carousel-example-generic" data-slide-to="2"></li>
          </ol>

          <!-- Wrapper for slides -->
          <div class="carousel-inner" role="listbox">
            <div class="item active">
              <img src="../img/券买买_淘宝天猫优惠券_files/594781cc80d85.jpg" alt="...">
              <div class="carousel-caption">
                ...
              </div>
            </div>
            <div class="item" v-for='y in pic'>
              <img v-bind:src='y.picUrl' alt="...">
              <div class="carousel-caption">
                ...
              </div>
            </div>

          </div>
          <!-- Controls -->
          <a class="left carousel-control" href="#carousel-example-generic" rel="external nofollow" rel="external nofollow" role="button" data-slide="prev">
            <span class="glyphicon glyphicon-chevron-left" aria-hidden="true"></span>
            <span class="sr-only">Previous</span>
          </a>
          <a class="right carousel-control" href="#carousel-example-generic" rel="external nofollow" rel="external nofollow" role="button" data-slide="next">
            <span class="glyphicon glyphicon-chevron-right" aria-hidden="true"></span>
            <span class="sr-only">Next</span>
          </a>
        </div>
        <!--九宫格-->
        <div id="jiugongge">
          <div class="col-xs-3 col-sm-3 col-md-3 col-lg-3" v-for='z in icon'>
            <img v-bind:src="z.iurl" />
            <div>{{z.name}}</div>
          </div>
        </div>
        <!--主页商品展示-->
        <div v-for='x in info'>
          <a v-bind:href='x.toUrl'>
            <div class="col-xs-12 col-sm-12 col-md-6 col-lg-6 production">
                <div class="image"><img v-bind:src='x.imgsrc' /></div>
                <div class="content">{{x.info}}</div>
            </div>
          </a>
        </div>
        <div> </div>
        <div> </div>
      </div>
    </script>
    <script type="text/x-template" id='info'>
      <div>
        这是info界面
      </div>
    </script>
    <script type="text/x-template" id='goumai'>
      <div>
        这是购买界面
      </div>
    </script>
    <script type="text/x-template" id='wode'>
      <div>
        这是我的界面
      </div>
    </script>
    <script type="text/javascript">
      /*定义组件*/
      const home = {
        template: '#home',
        props:{
          info:Array,
          pic:Array,
          icon:Array
        }
      }
      const info = {
        template: '#info'
      }
      const goumai = {
          template: '#goumai'
        }
      const wode = {
          template:'#wode'
      }
      /*定义路由*/
      const myroutes = [{
          path: '/home',
          component: home
        }, {
          path: '/info',
          component: info
        }, {
          path: '/goumai',
          component: goumai
        }, {
          path: '/wode',
          component: wode
        }]
        /*创建VueRouter实例,然后传'routes'配置*/
      const myr = new VueRouter({
          routes: myroutes
        })
        /*创建Vue和挂载根实例*/
      const app = new Vue({
        router: myr,
        data:{
            info:[
              {imgsrc:'http://img.alicdn.com/bao/uploaded/i2/TB16ybkRpXXXXXeXXXXXXXXXXXX_!!0-item_pic.jpg',info:'西麦燕麦片380克/袋装 原味纯麦片早餐 成人即食免煮冲饮无蔗糖',nav:'咖啡/麦片/冲饮',toUrl:'http://item.taobao.com/item.htm?id=534003771051'},
              {imgsrc:'http://img.alicdn.com/bao/uploaded/i2/TB1SLSiRFXXXXaMXVXXXXXXXXXX_!!0-item_pic.jpg',info:'安全裤防走光女夏中高腰冰丝无痕莫代尔平角打底保险裤大码不卷边',nav:'女士内衣/男士内衣/家居服',toUrl:'http://item.taobao.com/item.htm?id=550539721556'},
              {imgsrc:'http://img.alicdn.com/bao/uploaded/i4/TB1cYYHQVXXXXakXXXXXXXXXXXX_!!0-item_pic.jpg',info:'情侣装夏装韩版百搭2017新款体恤宽松半袖学生短袖T恤女潮上衣服',nav:'女装/女士精品',toUrl:'http://item.taobao.com/item.htm?id=549327457300'}
            ],
            pic:[
              {picUrl:'../img/券买买_淘宝天猫优惠券_files/555737bbad955.jpg'},
              {picUrl:'../img/券买买_淘宝天猫优惠券_files/594781cc80d85.jpg'}
            ],
            icon:[
              {iurl:'../img/券买买_淘宝天猫优惠券_files/5695f4aeb8a3f.png',name:'女装'},
              {iurl:'../img/券买买_淘宝天猫优惠券_files/5695ff4d7bd96.png',name:'男装'},
              {iurl:'../img/券买买_淘宝天猫优惠券_files/5695ff6d5b989.png',name:'内衣'},
              {iurl:'../img/券买买_淘宝天猫优惠券_files/5695ff9b2eebd.png',name:'鞋子'},
              {iurl:'../img/券买买_淘宝天猫优惠券_files/5695ffd10d9e3.png',name:'玩具'},
              {iurl:'../img/券买买_淘宝天猫优惠券_files/569600f2bb151.png',name:'盆栽'},
              {iurl:'../img/券买买_淘宝天猫优惠券_files/569600f2bb151.png',name:'首饰'},
              {iurl:'../img/券买买_淘宝天猫优惠券_files/5805f0ba98f4f.png',name:'美食'},
            ]
        },
        methods: {
          show: function(num) {
            if(num == 0) {
              myr.push('home')
            } else if(num == 1) {
              myr.push('info')
            } else if(num == 2) {
              myr.push('goumai')
            }else{
              myr.push('wode')
            }
          }
        }
      }).$mount('#app')
      window.onload = app.show(0)
    </script>
    <script src="../js/js.js" type="text/javascript" charset="utf-8"></script>
  </body>

</html>

css代码:

#header{
  background: white;
  text-align: center;
  margin:0 auto;
}
#jiugongge{
  margin-top:20px;
}
#jiugongge div{
  text-align: center;
}
.production{
  display:flex;
  margin-top:10px;
}
.production .image{
  flex: 1;
}
.production .content{
  flex: 2;
}
.image img {
  width:150px;
}
#nav{
  position: fixed;
  bottom: 0px;
  background: white;
  width:100%;
  margin:0 10px;
}
#nav ul{
  width:100%;
}
#nav li{
  width:24%;
}
#produces{
  height:40px;
  margin-bottom: 40px;
}

js代码:

$('ul').on('click','li',function(){
  $('li').attr('class','')
  $(this).attr('class','active')
})

效果图:

Vue.js框架路由使用方法实例详解

以上就是Vue.js框架路由使用方法,如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

Javascript 相关文章推荐
CCPry JS类库 代码
Oct 30 Javascript
JavaScript入门之基本函数详解
Oct 21 Javascript
JQuery中基础过滤选择器用法实例分析
May 18 Javascript
JS模式之简单的订阅者和发布者模式完整实例
Jun 30 Javascript
jQuery解决input超多的表单提交
Aug 10 Javascript
谈谈我对JavaScript原型和闭包系列理解(随手笔记9)
Dec 24 Javascript
浅析jQuery 3.0中的Data
Jun 14 Javascript
JavaScript实现简单评论功能
Aug 17 Javascript
Node.js + express基本用法教程
Mar 14 Javascript
vue发送websocket请求和http post请求的实例代码
Jul 11 Javascript
JS面向对象编程实现的拖拽功能案例详解
Mar 03 Javascript
实现AJAX异步调用和局部刷新的基本步骤
Mar 17 Javascript
关于Vue.nextTick()的正确使用方法浅析
Aug 25 #Javascript
jQuery图片缩放插件smartZoom使用实例详解
Aug 25 #jQuery
JS中利用swiper实现3d翻转幻灯片实例代码
Aug 25 #Javascript
在Vue组件化中利用axios处理ajax请求的使用方法
Aug 25 #Javascript
JS点击图片弹出文件选择框并覆盖原图功能的实现代码
Aug 25 #Javascript
JavaScript数据类型的存储方法详解
Aug 25 #Javascript
使用JQ完成表格隔行换色的简单实例
Aug 25 #Javascript
You might like
一个经典实用的PHP图像处理类分享
2014/11/18 PHP
服务器上配置PHP运行环境教程
2015/02/12 PHP
php生成与读取excel文件
2016/10/14 PHP
TP5(thinkPHP5)框架基于ajax与后台数据交互操作简单示例
2018/09/03 PHP
php 与 nginx 的处理方式及nginx与php-fpm通信的两种方式
2018/09/28 PHP
javascript radio 联动效果
2009/03/04 Javascript
EXT中xtype的含义分析
2010/01/07 Javascript
Javascript实现仿WebQQ界面的“浮云”兼容 IE7以上版本及FF
2011/04/27 Javascript
使用Math.floor与Math.random取随机整数的方法详解
2013/05/07 Javascript
js返回上一页并刷新的多种实现方法
2014/02/26 Javascript
js仿苹果iwatch外观的计时器代码分享
2015/08/26 Javascript
jQuery过滤特殊字符及JS字符串转为数字
2016/05/26 Javascript
JavaScript生成图形验证码
2020/08/24 Javascript
详解http访问解析流程原理
2017/10/18 Javascript
Js经典案例的实例代码
2018/05/10 Javascript
引入外部js脚本加载慢与页面白屏问题的解决
2018/12/10 Javascript
详解从react转职到vue开发的项目准备
2019/01/14 Javascript
20道JS原理题助你面试一臂之力(必看)
2019/07/22 Javascript
vue滚动插件better-scroll使用详解
2019/10/18 Javascript
[02:35]DOTA2超级联赛专访XB 难忘一年九冠称王
2013/06/20 DOTA
修改Python的pyxmpp2中的主循环使其提高性能
2015/04/24 Python
六个窍门助你提高Python运行效率
2015/06/09 Python
Python中动态检测编码chardet的使用教程
2017/07/06 Python
python实现根据文件关键字进行切分为多个文件的示例
2018/12/10 Python
python烟花效果的代码实例
2020/02/25 Python
keras自定义损失函数并且模型加载的写法介绍
2020/06/15 Python
详解Python流程控制语句
2020/10/28 Python
html5实现多文件的上传示例代码
2014/02/13 HTML / CSS
优秀士兵个人事迹材料
2014/01/19 职场文书
红领巾心向党演讲稿
2014/09/10 职场文书
比赛口号霸气押韵
2015/12/24 职场文书
FP-growth算法发现频繁项集——构建FP树
2021/06/24 Python
浅谈哪个Python库才最适合做数据可视化
2021/06/28 Python
Django中session进行权限管理的使用
2021/07/09 Python
Docker 镜像介绍以及commit相关操作
2022/04/13 Servers
Win11 BitLocker 驱动器加密
2022/04/19 数码科技