当前位置: 首页 > news >正文

weex 项目开发(四)项目框架搭建 及 自定义 TabBar 组件

1.安装  路由模块  及  状态管理模块

npm install vue-router --save
npm install vuex --save

2.自定义  TabBar  组件

src / components / TabBar.vue

TabBar.vue

<!-- 底部选项卡 -->
<template>
  <div class="wrapper">
    <div class="bar-item" @click="tabTo('home')">
      <text class="bar-ic iconfont" :style="testCS"></text>
      <text class="bar-txt">首页</text>
    </div>
    <div class="bar-item" @click="tabTo('topic')">
      <text class="bar-ic iconfont"></text>
      <text class="bar-txt">专题</text>
    </div>
    <div class="bar-item act" @click="tabTo('class')">
      <text class="bar-ic iconfont"></text>
      <text class="bar-txt">分类</text>
    </div>
    <div class="bar-item" @click="tabTo('shop')">
      <text class="bar-ic iconfont"></text>
      <text class="bar-txt">购物车</text>
    </div>
    <div class="bar-item" @click="tabTo('my')">
      <text class="bar-ic iconfont"></text>
      <text class="bar-txt">个人</text>
    </div>
  </div>
</template>
 
<script>
  var modal = weex.requireModule('modal');

  export default {
    computed:{
      testCS:function () {
        return this.pIndexKey == 'home'?'color:#b4282d;':''
      }
    },
    data () {
      return {
        pIndexKey:'home'
      }
    },
    mounted(){
    },
    methods: {
      tabTo(_key){
        if(this.pIndexKey == _key) return;
        this.pIndexKey = _key;
        this.$emit('tabTo',{
          status : 'tabTo',
          data : {
            key : _key
          }
        })
      }
    }
  }
</script>
 
<style scoped>
  .iconfont {
    font-family:iconfont;
  }
  .wrapper{
    position: fixed;
    bottom: 0;
    left: 0;right: 0;
    height: 90px;
    flex-wrap: nowrap;
    flex-direction: row;
    justify-content: space-around;
    border-top-width: 1px;
    border-top-color: #d9d9d9;
    background-color: #fafafa;
  }
  .bar-item{
    flex: 1;
  }
  .bar-txt,.bar-ic{
    color:#666;
    text-align: center;
  }
  .bar-active{
    color:#b4282d;
  }
  .bar-ic{
    padding-top: 14px;
    font-size: 38px;
  }
  .bar-txt{
    font-size: 22px;
    padding-top: 2px;
  }
</style>

3.自定义  工具类

src / utils / util.js

util.js

/**
 * 工具类
 */

let utilFunc = {
    initIconFont () {
        let domModule = weex.requireModule('dom');
        domModule.addRule('fontFace', {
            'fontFamily': "iconfont",
            'src': "url('http://at.alicdn.com/t/font_404010_jgmnakd1zizr529.ttf')"
        });
    },
    setBundleUrl(url, jsFile) {
        const bundleUrl = url;
        let host = '';
        let path = '';
        let nativeBase;
        const isAndroidAssets = bundleUrl.indexOf('your_current_IP') >= 0 || bundleUrl.indexOf('file://assets/') >= 0;
        const isiOSAssets = bundleUrl.indexOf('file:///') >= 0 && bundleUrl.indexOf('WeexDemo.app') > 0;
        if (isAndroidAssets) {
            nativeBase = 'file://assets/dist';
        } else if (isiOSAssets) {
            // file:///var/mobile/Containers/Bundle/Application/{id}/WeexDemo.app/
            // file:///Users/{user}/Library/Developer/CoreSimulator/Devices/{id}/data/Containers/Bundle/Application/{id}/WeexDemo.app/
            nativeBase = bundleUrl.substring(0, bundleUrl.lastIndexOf('/') + 1);
        } else {
            const matches = /\/\/([^\/]+?)\//.exec(bundleUrl);
            const matchFirstPath = /\/\/[^\/]+\/([^\s]+)\//.exec(bundleUrl);
            if (matches && matches.length >= 2) {
                host = matches[1];
            }
            if (matchFirstPath && matchFirstPath.length >= 2) {
                path = matchFirstPath[1];
            }
            nativeBase = 'http://' + host + '/';
        }
        const h5Base = './index.html?page=';
        // in Native
        let base = nativeBase;
        if (typeof navigator !== 'undefined' && (navigator.appCodeName === 'Mozilla' || navigator.product === 'Gecko')) {
            // check if in weexpack project
            if (path === 'web' || path === 'dist') {
                base = h5Base + '/dist/';
            } else {
                base = h5Base + '';
            }
        } else {
            base = nativeBase + (!!path? path+'/':'');
        }

        const newUrl = base + jsFile;
        return newUrl;
    },
    getUrlSearch(url,name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = url.slice(url.indexOf('?')+1).match(reg);
        if (r != null) {
            try {
                return decodeURIComponent(r[2]);
            } catch (_e) {
                return null;
            }
        }
        return null;
    }
};

export default utilFunc;  

4.其他页面

src / pages / Home / Home.vue

例如:Home.vue

<!-- 首页 -->
<template>
  <div>
    <text>首页</text>
  </div>
</template>
 
<script>
  export default {
    name: 'Home',
    data: () => ({
      //
    }),
    created () {
      //
    },
    methods: {
      //
    }
  };
</script>
 
<style scoped>
</style>

5.配置 路由

src / router / index.js

index.js

/**
 * 配置路由
 */
import Router from 'vue-router'
// 首页
import Home from '../pages/Home/Home.vue'
// 专题
import Topic from '../pages/Topic/Topic.vue'
// 分类
import Class from '../pages/Class/Class.vue'
// 购物车
import Shop from '../pages/Shop/Shop.vue'
// 个人
import My from '../pages/My/My.vue'

Vue.use(Router)


export default new Router({
    // mode: 'abstract',
    routes: [
        { path: '/', redirect: '/home' },
        { path: '/home', component: Home },
        { path: '/topic', component: Topic },
        { path: '/class', component: Class },
        { path: '/shop', component: Shop },
        { path: '/my', component: My }
    ]
})

6.主页面  引入 工具类  及  TabBar 组件

src / App.vue

App.vue

<!-- 主页面 -->
<template>
  <div class="app-wrapper">
    <router-view class="r-box"></router-view>
    <tab-bar @tabTo="onTabTo"></tab-bar>
  </div>
</template>

<script>
  var modal = weex.requireModule('modal');
  import util from './utils/util.js';
  import tabBar from './components/TabBar.vue';

  export default {
    data () {
      return {
        //
      }
    },
    components: {
      'tab-bar': tabBar
    },
    created () {
      util.initIconFont();
    },
    methods: {
      onTabTo(_result){
        let _key = _result.data.key || '';
        this.$router && this.$router.push('/'+_key)
      }
    }
  }
</script>

<style>
  body{
    margin: 0;
    padding: 0;
    background-color: #f4f4f4;
    color:#333;
  }
</style>

<style scoped>
  .app-wrapper{
    background-color: #f4f4f4;
  }
  .r-box{
    position: absolute;
    top:0;
    left: 0;
    right: 0;
    bottom: 0;
  }
</style>

7.定义  入口文件  entry.js

src / entry.js

/**
 * 入口文件
 */
import App from './App.vue'
import router from './router'

// 创建应用程序实例
new Vue(Vue.util.extend({ el: '#root', router }, App));

router.push('/');

8.在  webpack.config.js 中配置 入口文件

/***************** 配置入口文件 start *****************/
const entry = {index: pathTo.resolve('src', 'entry.js')};
const weexEntry = {index: pathTo.resolve('src', 'entry.js')};
/****************** 配置入口文件 end ******************/

9.项目 结构

10.效果图

注:#root 报错

如果你使用的是 entry.js 作为入口文件,就需要删除 webpack.conf.js 文件中的 getEntryFileContent 和 walk 方法。

相关文章:

  • 项目规划管理 - 1
  • C# DLL资源文件打包(图片、JS、CSS)[WebResource]
  • 阅读摘要
  • 浅谈SQL Server中的事务日志(三)----在简单恢复模式下日志的角色
  • exchange日常管理之十五:报550错误
  • 12. ZooKeeper之Java客户端API使用—创建会话。
  • 安装office2007 1706错误
  • 36.Linux驱动调试-根据oops定位错误代码行
  • 自我激励的20种方法
  • 数据结构C++(1)线性表——数组实现(arrayList)
  • C++拷贝构造函数(深拷贝与浅拷贝)
  • 支付系统接口性能压力测试TPS优化之路
  • 数据挖掘十大经典算法——k-means
  • crontab中执行多条命令
  • 25 JavaScript的幻灯片用于在Web布局的精彩案例
  • 2018以太坊智能合约编程语言solidity的最佳IDEs
  • angular组件开发
  • co模块的前端实现
  • HTML中设置input等文本框为不可操作
  • Java IO学习笔记一
  • JS数组方法汇总
  • linux学习笔记
  • mysql中InnoDB引擎中页的概念
  • PermissionScope Swift4 兼容问题
  • Perseus-BERT——业内性能极致优化的BERT训练方案
  • React-Native - 收藏集 - 掘金
  • ⭐ Unity 开发bug —— 打包后shader失效或者bug (我这里用Shader做两张图片的合并发现了问题)
  • vue的全局变量和全局拦截请求器
  • 关于springcloud Gateway中的限流
  • 基于OpenResty的Lua Web框架lor0.0.2预览版发布
  • 通信类
  • 项目实战-Api的解决方案
  • 要让cordova项目适配iphoneX + ios11.4,总共要几步?三步
  • Java性能优化之JVM GC(垃圾回收机制)
  • 如何在 Intellij IDEA 更高效地将应用部署到容器服务 Kubernetes ...
  • #中国IT界的第一本漂流日记 传递IT正能量# 【分享得“IT漂友”勋章】
  • (pojstep1.3.1)1017(构造法模拟)
  • (补)B+树一些思想
  • (二十四)Flask之flask-session组件
  • (二十五)admin-boot项目之集成消息队列Rabbitmq
  • (附源码)springboot 基于HTML5的个人网页的网站设计与实现 毕业设计 031623
  • (转)linux下的时间函数使用
  • (转)linux自定义开机启动服务和chkconfig使用方法
  • (转载)从 Java 代码到 Java 堆
  • *Algs4-1.5.25随机网格的倍率测试-(未读懂题)
  • .apk文件,IIS不支持下载解决
  • .describe() python_Python-Win32com-Excel
  • .NET Core日志内容详解,详解不同日志级别的区别和有关日志记录的实用工具和第三方库详解与示例
  • .Net Memory Profiler的使用举例
  • .NET 解决重复提交问题
  • .NET基础篇——反射的奥妙
  • .NET企业级应用架构设计系列之结尾篇
  • /usr/local/nginx/logs/nginx.pid failed (2: No such file or directory)
  • [100天算法】-不同路径 III(day 73)
  • [Android]RecyclerView添加HeaderView出现宽度问题