vue实战特定场景写法,部分问题解决

195 阅读3分钟

全局引入字体图标库,动画库

根目录新建一个common目录,把下载的animate.css和阿里的iconfont.css放进去 编辑app.vue,style里全局进入@import './common/iconfont.css'; 需要注意的是,阿里iconfont.css只保留class引用方式,其它删除即可

点击标签更换index,添加active class的标准写法 :class="{'active':tabindex==index}" @tap="changetab(index)" 定义事件,让点击时获取的index赋值给this.tabindex

根据数据自动添加class的写法 :class=' item.sex==0 ? "icon-nan" : "icon-nv" ' 判断sex如果是0,就显示icon-nan的样式,否则就显示icon-nv

选中标签下方出线下划线的做法,不用border-bottom,因为不好控制。而是采用在active下面新增一个view .active view{border-bottom: 4upx solid #FF9619; border-top: 4upx solid #FF9619; border-radius: 4upx; width: 80upx; margin: auto;} 这样写更方便控制,可以不更改滚动条,而单独增加一个下划线

上拉加载更多,实际上是直接在底部写一个带样式的view字,然后通过事件控制这些字的现实和数据的渲染,一般会用到定时器

uni-app顶部搜索框的实现,一般首页的搜索框不能直接点,而是跳转到搜索页的。在page.json配置页增加一个页面生命周期方法,搜索框点击的监听事件onNavigationBarSearchInputClicked,和onload平级,然后再用路由接口跳转

组件的简洁引入方法
1. 在components文件夹下,新建一个index.js,导出所有组件
export const left = ()=>import("@/components/left.vue");
export const right = ()=>import("@/components/right.vue");
2. 到页面内引入,注册,使用
import {left,right} from "@/components"
components:{left,right}
页面内使用
<left></left>
<right></right>
那么,相同道理的,路由里面也可以这样写,简单简洁
Class 与 Style 绑定 数据绑定一个常见需求是操作元素的 class 列表和它的内联样式。我们可以通过v-bind去绑定。此外,在 v-bind 用于 class 和 style 时, 表达式的结果类型除了字符串之外,还可以是对象或数组。
 <!--
    给v-bind:class一个对象,可以动态地切换class;
    此外, 也可以在对象中传入更多属性用来动态切换多个class,
    而且v-bind:class指令可以与普通的 class 属性共存
    -->
    <p :class="{'green': isGreen}">根据数据决定是否加载css的green样式</p>
    <p :class="{'size': isSize, 'bg-color': isBgColor}">同时设置多个css样式</p>
    <p :class="['class-A', 'class-B', 'class-C']">通过数组设置多个样式</p>

    <!--
    给v-bind:style一个对象,可以动态地切换style;
    -->
    <p :style="{backgroundColor: bgColor, width: width, height: height}">设置行内样式</p>
    <p :style="[styleA, styleB, styleC]">可以通过数组设置多个行内样式</p>
</div>
解决vue中的NavigationDuplicated {_name: "NavigationDuplicated", name: "NavigationDuplicated"}
在main.js下添加如下代码:

import Router from 'vue-router'
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}