七、Tabbar
7.1 在assets里面创建了img和css文件
在main.js里面可以引入指定的css文件然后在App.vue里面进行引入
<style>
@import url('./assets/css/base.css');
</style>
通过插槽来对属性进行插入
TobarItem.vue
<template>
<div class="tab-bar-item">
<slot name="tab-bar-ico"></slot>
<slot name="tab-bar-item"></slot>
</div>
</template>
<script>
export default {
name: "",
data() {
return {};
},
};
</script>
<style scoped>
.tab-bar-item {
flex: 1;
text-align: center;
height: 49px;
}
.tab-bar-item img {
height: 24px;
width: 24px;
vertical-align: middle;
margin-bottom: 2px;
}
</style>
Tabar.vue
<template>
<div id="tab-bar">
<slot></slot>
</div>
</template>
<script>
export default {
name: "TabBar",
data() {
return {};
},
};
</script>
<style scoped>
#tab-bar {
display: flex;
background-color: #f6f6ff;
position: fixed;
left: 0;
bottom: 0;
right: 0;
box-shadow: 0 -1px 1px rgb(100, 100, 100, 0.8);
/* 垂直方向 向上1px 深1px 透明度0.8 */
}
</style>
App.vue
<template>
<div id="app">
<TabBar>
<TabBarItem>
<img slot="tab-bar-ico" src="../src/assets/img/tabbar/home.svg" alt />
<div slot="tab-bar-item">首页</div>
</TabBarItem>
<TabBarItem>
<img slot="tab-bar-ico" src="../src/assets/img/tabbar/category.svg" alt />
<div slot="tab-bar-item">查询</div>
</TabBarItem>
<TabBarItem>
<img slot="tab-bar-ico" src="../src/assets/img/tabbar/shopcart.svg" alt />
<div slot="tab-bar-item">购物车</div>
</TabBarItem>
<TabBarItem>
<img slot="tab-bar-ico" src="../src/assets/img/tabbar/profile.svg" alt />
<div slot="tab-bar-item">我的</div>
</TabBarItem>
</TabBar>
</div>
</template>
<script>
import TabBar from "./components/tabbar/TabBar";
import TabBarItem from "./components/tabbar/TabBarItem";
export default {
name: "App",
components: {
TabBar,
TabBarItem,
},
};
</script>
<style>
@import url("./assets/css/base.css");
</style>
Components里面一般是用来存放公共组件的,例如几个组件的公共部分存放在components里面使用一个文件夹进行存放,但是如果要是一些具体的组件就要在src下创建一个目录名字是views用来存放一些具体的组件
整体布局:
第一步 导航条里面的图片与内容都是需要进行改变的,所以需要在TabBarItem里面设置插槽来表示需要插入的东西,这个里面设置的是详细的内容,但是又不一定是这一种组件所以要设置一个插槽来表示需要插入的组件最后在App.vue里面进行引用
TabBaritem.vue
<template>
<!--所有的item都展示同一个图片, 同一个文字-->
<div class="tab-bar-item" @click="itemClick">
<div v-if="!isActive"><slot name="item-icon"></slot></div>
<div v-else><slot name="item-icon-active"></slot></div>
<div :style="activeStyle"><slot name="item-text"></slot></div>
</div>
</template>
<script>
export default {
name: "TabBarItem",
props: {
//获取传递的参数
path: String,
activeColor: {
type: String,
default: "red"
}
},
data() {
return {};
},
computed: {
isActive() {
//console.log(this.$route.path + "->");
//console.log(this.path + "===<");
//判断当前点击的这个路径在全部的路径中的位置是否存在,
//只有存在的路径他的地址index就不会是-1,isActive的值就是true
//这样高亮的图片才会进行显示 $route.path是路由的所有路径 this.path是传递过来的路径
return this.$route.path.indexOf(this.path) !== -1;
},
activeStyle() {
return this.isActive ? { color: this.activeColor } : {};
}
},
methods: {
itemClick() {
//进行路由跳转
this.$router.replace(this.path);
}
}
};
</script>
<style scoped>
.tab-bar-item {
flex: 1;
text-align: center;
height: 49px;
font-size: 14px;
}
.tab-bar-item img {
width: 24px;
height: 24px;
margin-top: 3px;
vertical-align: middle;
margin-bottom: 2px;
}
</style>
TabBar.vue
<template>
<div id="tab-bar">
<slot></slot>
</div>
</template>
<script>
export default {
name: "TabBar",
data() {
return {};
},
};
</script>
<style scoped>
#tab-bar {
display: flex;
background-color: #f6f6ff;
position: fixed;
left: 0;
bottom: 0;
right: 0;
box-shadow: 0 -1px 1px rgb(100, 100, 100, 0.8);
/* 垂直方向 向上1px 深1px 透明度0.8 */
}
</style>
MainTabBar.vue
<template>
<tab-bar>
<tab-bar-item path="/home" activeColor="pink">
<img slot="item-icon" src="../../assets/img/tabbar/home.svg" alt="" />
<img
slot="item-icon-active"
src="../../assets/img/tabbar/home_active.svg"
alt=""
/>
<div slot="item-text">首页</div>
</tab-bar-item>
<tab-bar-item path="/category" activeColor="pink">
<img slot="item-icon" src="../../assets/img/tabbar/category.svg" alt="" />
<img
slot="item-icon-active"
src="../../assets/img/tabbar/category_active.svg"
alt=""
/>
<div slot="item-text">分类</div>
</tab-bar-item>
<tab-bar-item path="/cart" activeColor="pink">
<img slot="item-icon" src="../../assets/img/tabbar/shopcart.svg" alt="" />
<img
slot="item-icon-active"
src="../../assets/img/tabbar/shopcart_active.svg"
alt=""
/>
<div slot="item-text">购物车</div>
</tab-bar-item>
<tab-bar-item path="/profile" activeColor="deepPink">
<img slot="item-icon" src="../../assets/img/tabbar/profile.svg" alt="" />
<img
slot="item-icon-active"
src="../../assets/img/tabbar/profile_active.svg"
alt=""
/>
<div slot="item-text">我的</div>
</tab-bar-item>
</tab-bar>
</template>
<script>
import TabBar from "../tabbar/TabBar";
import TabBarItem from "../tabbar/TabBarItem";
export default {
name: "MainTabBar",
components: {
TabBar,
TabBarItem
}
};
</script>
<style scoped></style>
index.js
并在views文件夹里面创建要点击之后显示的内容
import Vue from 'vue'
import VueRouter from 'vue-router'
const Home = () => import('../views/home/Home')
const Category = () => import('../views/category/Category')
const Cart = () => import('../views/cart/Cart')
const Profile = () => import('../views/profile/Profile')
// 1.安装插件
Vue.use(VueRouter)
// 2.创建路由对象
const routes = [{
path: '',
redirect: '/home'
},
{
path: '/home',
component: Home
},
{
path: '/category',
component: Category
},
{
path: '/cart',
component: Cart
},
{
path: '/profile',
component: Profile
}
]
const router = new VueRouter({
routes,
mode: 'history'
})
// 3.导出router
export default router
App.vue
<template>
<div id="app">
<router-view></router-view>
<main-tabbar></main-tabbar>
</div>
</template>
<script>
import MainTabbar from "./components/MainTabbar/MainTabbar";
export default {
name: "App",
components: {
MainTabbar
}
};
</script>
<style>
@import url("./assets/css/base.css");
</style>
s
补充取别名:
在webpack.base.conf.js配置文件里面
alias: {
'@': resolve('src'),
'assets': resolve('src/assets'),
'components': resolve('src/components'),
'views': resolve('src/views')
}
设置别名进行显示
什么时候使用Promise?一般情况下有异步操作的时候使用Promise实现异步操作
7.2 Promise的基本使用
Promise的基本语法
<script>
// 什么情况下会用到Promise?
// 一般情况下是有异步操作时,使用Promise对这个异步操作进行封装
// new -> 构造函数(1.保存了一些状态信息 2.执行传入的函数)
// 在执行传入的回调函数时, 会传入两个参数, resolve, reject.本身又是函数
new Promise((resolve, reject) => {
setTimeout(() => {
// 成功的时候调用resolve
// resolve('Hello World')
// 失败的时候调用reject
reject("error message");
}, 1000);
}).then((data) => {
console.log(data);
console.log(data);
console.log(data);
console.log(data);
console.log(data);
});
</script>
7.2 另外的处理形式
<script>
new Promise((resolve, reject) => {
setTimeout(() => {
// resolve('Hello Vuejs')
reject("error message");
}, 1000);
}).then(
//获取的是成功的数据
(data) => {
console.log(data);
},
//获取的是失败的数据
(err) => {
console.log(err);
}
);
</script>
7.3 链式调用
<script>
// 参数 -> 函数(resolve, reject)
// resolve, reject本身它们又是函数
// 链式编程
// new Promise((resolve, reject) => {
// setTimeout(() => {
// resolve("aaa");
// }, 1000);
// })
// .then((res) => {
// console.log(res, "第一层的处理");
// return new Promise((resolve) => {
// resolve(res + "123");
// });
// })
// .then((res) => {
// console.log(res, "这个是第二层处理");
// return new Promise((resolve) => {
// resolve(res + "222");
// });
// })
// .then((res) => {
// console.log(res, "这个是第三层的应用");
// });
new Promise((resolve, reject) => {
setTimeout(() => {
resolve("aaa");
}, 1000);
})
.then((res) => {
console.log(res, "第一层的处理");
return res + "111";
})
.then((res) => {
console.log(res, "这个是第二层处理");
return res + "222";
})
.then((res) => {
console.log(res, "这个是第三层的应用");
});
</script>
7.4 All方法
<script>
Promise.all([
new Promise((reslove, reject) => {
setTimeout(() => {
reslove({ name: "李加喜", age: "18" });
}, 1000);
}),
new Promise((reslove, reject) => {
setTimeout(() => {
reslove({ name: "lll", age: "20" });
}, 2000);
}),
]).then((reslut) => {
console.log(reslut);
//获取加载的数据直接进行返回 返回一个数组数组里面存放了两个对象
});
</script>