飞翔的鱼 飞翔的鱼
首页
  • Http
  • Ajax
  • Node
  • MongoDB
  • Axios
  • Git
  • Webpack
  • React
  • Vue
  • Uni-app
  • 性能优化
  • 移动端
  • HTML
  • CSS
  • stylus
  • 常用
  • 实战
  • 实用网站
  • 资源库
  • Vue专区
关于
  • css效果
  • js效果
  • 拥抱生活
  • 生活知道
GitHub (opens new window)

Wang

飞翔的鱼
首页
  • Http
  • Ajax
  • Node
  • MongoDB
  • Axios
  • Git
  • Webpack
  • React
  • Vue
  • Uni-app
  • 性能优化
  • 移动端
  • HTML
  • CSS
  • stylus
  • 常用
  • 实战
  • 实用网站
  • 资源库
  • Vue专区
关于
  • css效果
  • js效果
  • 拥抱生活
  • 生活知道
GitHub (opens new window)
  • 常用

  • 实战

    • Markdown使用教程
    • 阿里字体图标使用
    • Vue实战技巧
      • Vue实战技巧
        • 1.导航栏跟随鼠标点击同步(变色)
        • 2.封装公共组件(插槽和prop)
        • 3.meta传参
  • 技术
  • 实战
wang jiaqi
2021-04-19

Vue实战技巧

# Vue实战技巧

# 1.导航栏跟随鼠标点击同步(变色)

利用route.path方法

<div
      @click="goPath('/home')"
      :class="{active:$route.path=='/home'}"
>
      <span>首页</span>
      <i class="iconfont icon-dingdan"></i>
</div>
<style>
       active {
           color:red
       }   
</style>         
1
2
3
4
5
6
7
8
9
10
11
12

# 2.封装公共组件(插槽和prop)

  1. 插槽和props的区别

​ props一般是用来传递数据的

​ 插槽一般是某个区域(功能块)

  1. props的使用

    //公共组件
    <div>{{title}}</div>
    <script>
      export default {
    	name: "gshopHeader",
        props:{
          title:{
            request:true,
            type:String,
            default:'默认值'
          }
        }
      }
    </script>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    //子组建
    <gshop-header :title="'订单'"></gshop-header>
    
    1
    2
  2. 插槽的使用

    //公共组件
    <slot name="left"></slot>
    <div>{{title}}</div>
    <slot name="right"></slot>
    
    1
    2
    3
    4
    //子组件
    <gshop-header :title="'标题'">
        <template v-slot:left>   //vue推荐用法
            <span class="header_search">
                <i class="iconfont icon-search1"></i>
    		</span>
    	</template>
    	<template v-slot:right>
            <span class="header_search">
                <i class="iconfont icon-search1"></i>
            </span>
    	</template>
    </gshop-header>
    
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13

# 3.meta传参

公共组件在某一个组件中不需要,则通过route.meta来判断

router/index.js

{
      path: '/home',//需要公共组件
      component: () => import('../pages/home/home'),
      meta:{//通过meta传参
        isFooterContainer:true
      }
    },
    {
      path: '/login',//不需要公共组件
      component: () => import('../pages/login/login'),
      

1
2
3
4
5
6
7
8
9
10
11
12

App.vue

<footer-guide v-show="$route.meta.isFooterContainer"/>   //通过$route.meta来判断
1
编辑 (opens new window)
上次更新: 2021/07/01, 20:33:02
阿里字体图标使用

← 阿里字体图标使用

最近更新
01
数组常用方法
05-21
02
边框
04-30
03
js1
04-29
更多文章>
Theme by Vdoing | Copyright © 2021-2021 本博客仅仅作为自己日常学习记录笔记使用
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×