weex踩坑之旅第二弹 ~ 在weex中集成vue-router

vue-router的集成

1. 安装vue-router

$ npm install vue-router --save

2. 创建路由组件页面

<template>
    <div class="one">
        <text>
            {{msg}}
        </text>
    </div>
</template>
<script>
    export default {
        data:()=>({
            msg:'this is one'
        })
    }
</script>

代码结构如下

image.png

3. 集成

在src目录创建router目录,用于存放路由相关信息,然后在router中新建index.js。进行路由的配置以及与Router的集成,以下是src/router/index.js的代码

import Router from 'vue-router'
//组件导入
import ViewOne from '../pages/one/index.vue'
import ViewTwo from '../pages/two/index.vue'
import ViewThree from '../pages/three/index.vue'
//将Vue-router继承到Vue中
Vue.use(Router);
//提供默认对外接口
export default new Router({
  // mode: 'abstract',
  routes: [
    { path: '/one', component: ViewOne },
    { path: '/two', component: ViewTwo },
    { path: '/three', component: ViewThree }
  ]
});

然后在entry.js中导入router的配置

import App from './App.vue'
//引入路由配置
import router from './router'
new Vue(Vue.util.extend({
    el:'#root',    
    //将vue集成到vue中
    router,
},App))

4. 路由编程

在App.vue中提供<router-view>指令,用于显示路由信息

<template>
    <div class='container'>
        <!-- 标题 -->
        <div class="panel titlePanel">
            <text class='title'>{{msg}}</text>
        </div>
        <!-- 导航区 -->
      <div class="panel">
          <text class='link' @click='linkTo("/one")'>one</text>
          <text class='link' @click='linkTo("/two")'>two</text>
          <text class='link' @click='linkTo("/three")'>three</text>
      </div>
      <!-- 视图区 -->
    <router-view></router-view>
    </div>
</template>
<script>
    export default{
        data(){
            return {
                msg:'你好,weex'
            }
        },
        methods:{
            linkTo(path){
                //点击后改变路由
                this.$router.push(path);
            }
        }
    }
</script>
<style>
.container {
    background-color:#f4f4f4;
}

.panel {
    flex-direction: row;
    height: 100px;
    border-bottom-width: 1px;
    justify-content: space-between;
}
.titlePanel {
    justify-content: center;
    background-color: #ededed;
}
.title {
    height: 100px;
    line-height: 100px;
}
.link{
    line-height: 100px;
    text-align: center;
    flex: 1
}
</style>

运行效果

image.png
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,890评论 18 139
  • 备忘录模式 又名快照模式,目的是在不破坏封装性的情况下将一个实例的数据进行捕捉,然后外部化保存起来.在未来的某个时...
    tinysnake阅读 354评论 0 1
  • 之所以会出现多列等高布局,是因为在网页设计中可能会出现以下状况。 出现这种情况的原因也很简单,就是各个 div 标...
    ghwaphon阅读 4,396评论 0 11