[Vue] Vue makes tabbar

Four links and one new star 2020-11-13 03:08:26
vue vue makes tabbar


Use Vue Make tabbar

  1. Demo Address : https://github.com/lpzzzz/vue-tabbar

1. Create project

  1. vue-cli2.0 Use vue init webpack tabbar Command create project .

    vue init webpack tabbar
    

 Insert picture description here

 Insert picture description here

  1. stay style Reference resources in need to use @import reference ;

2. Remove redundant default components and styles from the project

 Insert picture description here

3. Write a simple basic interface

  1. Write a simple interface to start the test .
1. npm install
2. npm run dev

3.1 Clear the default style of the page

  1. stay assets Create in folder img and css Static resource folder .

 Insert picture description here

  1. Clear page default style
body {
margin: 0;
padding: 0;
}
  1. stay App.vue Reference style in
@import "./assets/css/base.css";
  1. Write basic structure code : Quick way => #tabbar>div.tabbar-item*4
<div id="tabbar">
<div class="tabbar-item"> home page </div>
<div class="tabbar-item"> classification </div>
<div class="tabbar-item"> The shopping cart </div>
<div class="tabbar-item"> my </div>
</div>
  1. Write simple styles
#tabbar {

/* Horizontal distribution of words */
display: flex;
position: fixed;
left: 0;
right: 0;
bottom: 0;
height: 49px;
background-color: #f7f7f7;
/* Set shadow box-shadow: x Axis offset ( Positive right offset ) y Axis offset ( Positive values shift downward ) Shadow height Color value */
box-shadow: 0 -3px 5px rgba(100, 100, 100, .1);
}
.tabbar-item {

/* Each item is equally divided */
flex: 1;
text-align: center;
}
  1. The above code has no strong reusability .

3.2 encapsulation ( extract ) Components

3.2.1 extract App Medium Tabbar

  1. take Tabbar The code in the code block is extracted as a new component Tabbar.vue
  2. stay App.vue in import Tabbar from “./components/tabbar/Tabbar”
  3. stay components in Certified components
 <div id="tabbar">
<div class="tabbar-item">
<img src="../../assets/img/tabbar/home.svg" alt="">
<div> home page </div>
</div>
<div class="tabbar-item">
<img src="../../assets/img/tabbar/category.svg" alt="">
<div> classification </div>
</div>
<div class="tabbar-item">
<img src="../../assets/img/tabbar/shopcart.svg" alt="">
<div> The shopping cart </div>
</div>
<div class="tabbar-item">
<img src="../../assets/img/tabbar/profile.svg" alt="">
<div> my </div>
</div>
</div>

3.2.2 extract Tabbar In the component Tabbar-item

  1. extract Tabbar A lot of code in , Use slot Slot to replace .
<template>
<div id="tabbar">
<slot> </slot>
</div>
</template>
<script>
export default {

name: "tabbar"
}
</script>
<style scoped>
#tabbar {

/* Horizontal distribution of words */
display: flex;
position: fixed;
left: 0;
right: 0;
bottom: 0;
height: 49px;
background-color: #f7f7f7;
/* Set shadow box-shadow: x Axis offset ( Positive right offset ) y Axis offset ( Positive values shift downward ) Shadow height Color value */
box-shadow: 0 -3px 5px rgba(100, 100, 100, .1);
}
</style>

3.2.3 take Tabbar-item Part of the Extraction out of the use of named slot replacement, easy to expand later

<template>
<div class="tabbar-item">
<!-- Here you can use the named slot and replace it when you use it -->
<slot name="slot-icon"/>
<slot name="slot-text"/>
</div>
</template>
<script>
export default {

name: "tabbar-item"
}
</script>
<style scoped>
.tabbar-item {

/* Each item is equally divided */
flex: 1;
text-align: center;
vertical-align: middle;
margin-top: 3px;
font-size: 14px;
margin-bottom: 2px;
}
/* Set icon style */
.tabbar-item img {

height: 24px;
width: 24px;
}
</style>

3.2.4 stay App Introduce components to use

  1. stay App Use in script Used in code blocks import … Introduce custom components to use .
import Tabbar from "./components/tabbar/Tabbar"
import TabbarItem from "./components/tabbar/Tabbar-item"
  1. stay App The introduced components are as follows :
<template>
<div id="app">
<Tabbar>
<tabbar-item>
<img slot="slot-icon" src="./assets/img/tabbar/home.svg" alt="">
<div slot="slot-text"> home page </div>
</tabbar-item>
<tabbar-item>
<img slot="slot-icon" src="./assets/img/tabbar/category.svg" alt="">
<div slot="slot-text"> classification </div>
</tabbar-item>
<tabbar-item>
<img slot="slot-icon" src="./assets/img/tabbar/shopcart.svg" alt="">
<div slot="slot-text"> The shopping cart </div>
</tabbar-item>
<tabbar-item>
<img slot="slot-icon" src="./assets/img/tabbar/profile.svg" alt="">
<div slot="slot-text"> my </div>
</tabbar-item>
</Tabbar>
</div>
</template>
<script>
import Tabbar from "./components/tabbar/Tabbar"
import TabbarItem from "./components/tabbar/Tabbar-item"
export default {

name: 'App',
components: {

// After the introduction of components, you need to register 
Tabbar,
TabbarItem
}
}
</script>
<style>
/* The way to introduce external styles style You need to use @import*/
@import "./assets/css/base.css";
</style>

3.3 to Tabbar-item Pass in active picture

  1. Add a new slot and add judgment to the slot. Activate the display according to the condition .
<template>
<div class="tabbar-item">
<!-- Here you can use the named slot and replace it when you use it -->
<div v-if="!isActive">
<slot name="slot-icon"/>
</div>
<!-- Active picture slot -->
<div v-else>
<slot name="slot-icon-active"/>
</div>
<!-- You need to use a div Wrap it up , Otherwise, the contents of the slot will be replaced when using, and the attribute styles inside will be replaced -->
<!--<slot :class="{active:isActive}" name="slot-text"/>-->
<div :class="{active:isActive}">
<slot name="slot-text"/>
</div>
</div>
</template>
<script>
export default {

name: "tabbar-item",
data() {

return {

isActive: false
}
}
}
</script>
<style scoped>
.tabbar-item {

/* Each item is equally divided */
flex: 1;
text-align: center;
vertical-align: middle;
margin-top: 3px;
font-size: 14px;
margin-bottom: 2px;
}
/* Set icon style */
.tabbar-item img {

height: 24px;
width: 24px;
}
.active {

color: red;
}
</style>
  1. Be careful : When using the slot, the contents filled in the slot will replace all the contents in the slot , So go ahead and use a div Wrap the slot around , stay div Write some necessary properties in . Like this :
 <!-- You need to use a div Wrap it up , Otherwise, the contents of the slot will be replaced when using, and the attribute styles inside will be replaced -->
<!--<slot :class="{active:isActive}" name="slot-text"/>-->
<div :class="{active:isActive}">
<slot name="slot-text"/>
</div>

3.4 Tabbar-item Combined with routing

  1. Every Tabbar-item They all correspond to a route jump .

3.4.1 Install routing operations

  1. If you don't have routing installed when you create your project , You need to use the command to install :npm install vue-router -save . establish router Folder . Create... In the folder index.js file . The contents of the document are as follows :
import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)
const Home = () => import('../views/home/Home')
const Category = () => import('../views/category/Category')
const Cart = () => import('../views/cart/Cart')
const Profile = () => import('../views/profile/Profile')
// Create routing objects Notice that this is routes Don't write routers Otherwise, when switching routes router-view It doesn't show 
const routes = [
{

path: '/',
redirect: '/home'
},
{

path: '/home',
component: Home
},
{

path: '/category',
component: Category
},
{

path: '/profile',
component: Profile
},
{

path: '/cart',
component: Cart
}
]
const router = new VueRouter({

routers
})
// export router
export default router

3.4.2 stay main.js Reference import route and use

import Vue from 'vue'
import App from './App'
import router from './router'
Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
el: '#app',
router,
render: h => h(App)
})

3.4.3 Create individual components

  1. Try not to create components to components In the folder , In this folder are some common components .
  2. You can create a new views perhaps pages Store each new single component .

 Insert picture description here

3.4.4 stay Tabbar-item Register listening events on component

  1. Listen to the jump to set the route on the event ,this.$router.push( Destination path );

 Insert picture description here

 Insert picture description here

3.4.5 stay App The path passed to jump from

 Insert picture description here

3.5 Tabbar-item Color dynamic control of

  1. take isActive Set to calculated property , Judge the current selected state according to the currently selected routing path . Return to one Boolean value ;
 // stay TabbarItem Add the following code to
computed: {
isActive() {
/* When the condition is true, it means a certain tabbar-item It's active */
return this.$route.path.indexOf(this.path) !== -1
}
}

3.6 TabbarItem Chinese character color

3.6.1 Ideas

  1. Need to be in props Define a property in . Used to receive from App The color settings passed from . Set up props The property name in is activeColor The type is String The default value is red stay TabbarItem The text in the component is bound with style Calculated attributes of .

 Insert picture description here

The path determines the current selected state . Return to one Boolean value ;

 // stay TabbarItem Add the following code to
computed: {
isActive() {
/* When the condition is true, it means a certain tabbar-item It's active */
return this.$route.path.indexOf(this.path) !== -1
}
}

3.6 TabbarItem Chinese character color

3.6.1 Ideas

  1. Need to be in props Define a property in . Used to receive from App The color settings passed from . Set up props The property name in is activeColor The type is String The default value is red stay TabbarItem The text in the component is bound with style Calculated attributes of .

 Insert picture description here
 Insert picture description here

3.7 extract Tabbar in App A lot of code in to Main.vue In the component

<template>
<Tabbar>
<tabbar-item path="/home" activeColor="red">
<img slot="slot-icon" src="../assets/img/tabbar/home.svg" alt="">
<img slot="slot-icon-active" src="../assets/img/tabbar/home_active.svg" alt="">
<div slot="slot-text"> home page </div>
</tabbar-item>
<tabbar-item path="/category" activeColor="green">
<img slot="slot-icon" src="../assets/img/tabbar/category.svg" alt="">
<img slot="slot-icon-active" src="../assets/img/tabbar/category_active.svg" alt="">
<div slot="slot-text"> classification </div>
</tabbar-item>
<tabbar-item path="/cart" activeColor="blue">
<img slot="slot-icon" src="../assets/img/tabbar/shopcart.svg" alt="">
<img slot="slot-icon-active" src="../assets/img/tabbar/shopcart_active.svg" alt="">
<div slot="slot-text"> The shopping cart </div>
</tabbar-item>
<tabbar-item path="/profile">
<img slot="slot-icon" src="../assets/img/tabbar/profile.svg" alt="">
<img slot="slot-icon-active" src="../assets/img/tabbar/profile_active.svg" alt="">
<div slot="slot-text"> my </div>
</tabbar-item>
</Tabbar>
</template>
<script>
import Tabbar from "../components/tabbar/Tabbar"
import TabbarItem from "../components/tabbar/Tabbar-item"
export default {

name: "main-tabbar",
components: {

// After the introduction of the component, you need to just register 
Tabbar,
TabbarItem
}
}
</script>
<style scoped>
</style>

3.8 File path alias ( File path reference problem )

  1. modify webpack.base.conf.js The configuration file The lower part of :
     Insert picture description here
  2. Use... In attributes : quote src The file path needs to be preceded by ~
     Insert picture description here
  3. Reference external components
     Insert picture description here

4. Final effect

 Insert picture description here

版权声明
本文为[Four links and one new star]所创,转载请带上原文链接,感谢

  1. [front end -- JavaScript] knowledge point (IV) -- memory leakage in the project (I)
  2. This mechanism in JS
  3. Vue 3.0 source code learning 1 --- rendering process of components
  4. Learning the realization of canvas and simple drawing
  5. gin里获取http请求过来的参数
  6. vue3的新特性
  7. Get the parameters from HTTP request in gin
  8. New features of vue3
  9. vue-cli 引入腾讯地图(最新 api,rocketmq原理面试
  10. Vue 学习笔记(3,免费Java高级工程师学习资源
  11. Vue 学习笔记(2,Java编程视频教程
  12. Vue cli introduces Tencent maps (the latest API, rocketmq)
  13. Vue learning notes (3, free Java senior engineer learning resources)
  14. Vue learning notes (2, Java programming video tutorial)
  15. 【Vue】—props属性
  16. 【Vue】—创建组件
  17. [Vue] - props attribute
  18. [Vue] - create component
  19. 浅谈vue响应式原理及发布订阅模式和观察者模式
  20. On Vue responsive principle, publish subscribe mode and observer mode
  21. 浅谈vue响应式原理及发布订阅模式和观察者模式
  22. On Vue responsive principle, publish subscribe mode and observer mode
  23. Xiaobai can understand it. It only takes 4 steps to solve the problem of Vue keep alive cache component
  24. Publish, subscribe and observer of design patterns
  25. Summary of common content added in ES6 + (II)
  26. No.8 Vue element admin learning (III) vuex learning and login method analysis
  27. Write a mini webpack project construction tool
  28. Shopping cart (front-end static page preparation)
  29. Introduction to the fluent platform
  30. Webpack5 cache
  31. The difference between drop-down box select option and datalist
  32. CSS review (III)
  33. Node.js学习笔记【七】
  34. Node.js learning notes [VII]
  35. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  36. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  37. 【JQuery框架,Java编程教程视频下载
  38. [jQuery framework, Java programming tutorial video download
  39. Vue Router根据后台数据加载不同的组件(思考-&gt;实现-&gt;不止于实现)
  40. Vue router loads different components according to background data (thinking - & gt; Implementation - & gt; (more than implementation)
  41. 【Vue,阿里P8大佬亲自教你
  42. 【Vue基础知识总结 5,字节跳动算法工程师面试经验
  43. [Vue, Ali P8 teaches you personally
  44. [Vue basic knowledge summary 5. Interview experience of byte beating Algorithm Engineer
  45. 【问题记录】- 谷歌浏览器 Html生成PDF
  46. [problem record] - PDF generated by Google browser HTML
  47. 【问题记录】- 谷歌浏览器 Html生成PDF
  48. [problem record] - PDF generated by Google browser HTML
  49. 【JavaScript】查漏补缺 —数组中reduce()方法
  50. [JavaScript] leak checking and defect filling - reduce() method in array
  51. 【重识 HTML (3),350道Java面试真题分享
  52. 【重识 HTML (2),Java并发编程必会的多线程你竟然还不会
  53. 【重识 HTML (1),二本Java小菜鸟4面字节跳动被秒成渣渣
  54. [re recognize HTML (3) and share 350 real Java interview questions
  55. [re recognize HTML (2). Multithreading is a must for Java Concurrent Programming. How dare you not
  56. [re recognize HTML (1), two Java rookies' 4-sided bytes beat and become slag in seconds
  57. 【重识 HTML ,nginx面试题阿里
  58. 【重识 HTML (4),ELK原来这么简单
  59. [re recognize HTML, nginx interview questions]
  60. [re recognize HTML (4). Elk is so simple