vue 修改引入组件的样式_vue子组件的子组件布局

vue 修改引入组件的样式_vue子组件的子组件布局意义vue被广大前端推崇很重要一点就是组件封装,但是在组件封装的时候,组件可能在各处都要用到,但是在各处的样式可能不太一样,例如:按钮组件,这时怎么办,难道不同样式但是结构相同的组件进行多次封装么?很明显是很不合算的。用代码说话父组件:<template><el-containerclass=”layout_container”><el-headerheight=”auto”><header-top></header-top&

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE使用 1年只要46元 售后保障 童叟无欺

意义

vue被广大前端推崇很重要一点就是组件封装,但是在组件封装的时候,组件可能在各处都要用到,但是在各处的样式可能不太一样,例如:按钮组件,这时怎么办,难道不同样式但是结构相同的组件进行多次封装么?很明显是很不合算的。

用代码说话

父组件:

<template>
  <el-container class="layout_container">
    <el-header height="auto"><header-top></header-top></el-header>
    <el-container>
      <el-aside width="auto"><aside-left></aside-left></el-aside>
      <el-main>
        <zonghe-nengli></zonghe-nengli>
       /重点看此处:
        <my-button
          :title="biaoti"
          :color="activeColor"
          :size="fontSize"
        ></my-button>
        ///
        <skill-hot></skill-hot>
        <learning-path></learning-path>
        <bar-chart></bar-chart>
        <radar></radar>
        <tupu-fenxi></tupu-fenxi>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
import HeaderTop from "../../components/layout/header";

import MyButton from "../../components/common/button";

import AsideLeft from "../../components/layout/aside";
import ZongheNengli from "../../components/common/zonghenengli";
import Radar from "../../components/common/radar";
import TupuFenxi from "../../components/common/tupufenxi";
import SkillHot from "../../components/putong/skillhot";
import LearningPath from "../../components/putong/learningpath";
import BarChart from "../../components/common/barchart";
export default { 
   
  components: { 
   
    HeaderTop,
    AsideLeft,
    ZongheNengli,
    Radar,
    TupuFenxi,
    SkillHot,
    LearningPath,
    BarChart,
    MyButton,
  },
  data() { 
   
    return { 
   
    ///
      biaoti: 20,
      activeColor: "black",
      fontSize: 30,
   ///
    };
  },
  created() { 
   },
  methods: { 
   },
  computed: { 
   },
};
</script>
<style scoped>
.layout_container { 
   
  height: 100%;
}
.el-aside { 
   
  margin-top: 21px;
  background: #ffffff;
  box-shadow: 0px 1px 13px 0px rgba(0, 0, 0, 0.35);
}
.el-main { 
   
  margin-top: 40px;
  margin-left: 37px;
  background-color: burlywood;
}
</style>

子组件(重点看看):

<template>
  <div class="button_container" :style="{color:activeColor,fontSize:fontSize + 'px'}">
    { 
   { 
    title }}
  </div>
</template>
<script>
export default { 
   
  /接受传过来的参数
  props: ["title","color","size"],
  data() { 
   
    return { 
   
      activeColor: this.color,
      fontSize: this.size,
    };
  },
  created() { 
   
    
  },
  methods: { 
   },
  computed: { 
   },
};
</script>
<style scoped>
.button_container { 
   
  width: 207px;
  height: 60px;
  margin: 35px;
  line-height: 60px;
  text-align: center;
  background: #2e5afb;
  box-shadow: 3px 8px 17px 1px rgba(46, 90, 251, 0.6);
  border-radius: 6px;
}
</style>

效果图:
在这里插入图片描述

想要封装可以动态改变样式的组件,必须得熟练掌握vue组件的class和style绑定,这样才能游刃有余

2、除了这中传值来改变组件样式,当然还是通过行内样式,或者给组件添加一个class类来改变组件样式
当在一个自定义组件上使用 class property 时,这些 class 将被添加到该组件的根元素上面。这个元素上已经存在的 class 不会被覆盖。

//1、行内样式
<tupu-fenxi style="margin: 10px 0 0 3px"></tupu-fenxi>


//1、添加类名
Vue.component('my-component', { 
   
  template: '<p class="foo bar">Hi</p>'
})
//然后在使用它的时候添加一些 class:
<my-component class="baz boo"></my-component>
//HTML 将被渲染为:
<p class="foo bar baz boo">Hi</p>


//3、对于带数据绑定 class 也同样适用:
<my-component v-bind:class="{ active: isActive }"></my-component>
//当 isActive 为 truthy 时,HTML 将被渲染成为:
<p class="foo bar active">Hi</p>
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/193248.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • GFS – The Google File System

    GFS – The Google File SystemTheGoogleFileSystemhttp://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.125.789&amp;rep=rep1&amp;type=pdfhttp://www.dbthink.com/?p=501,中文翻译 Google牛人云集的地方,但在设计系统时,却非常务实,没有采用什么复杂和时髦…

    2022年6月1日
    48
  • k8s的apiserver_kube_apiserver

    k8s的apiserver_kube_apiserverAPIserverfunc main() { … #核心 command := app.NewAPIServerCommand() #日志 logs.InitLogs() defer logs.FlushLogs() …}#NewAPIServerCommand#核心是调用run函数 运行AIPserver 永远不会退出return Run(completedOptions, genericapiserver.SetupSignalHandler())#Run#创建

    2022年8月9日
    6
  • 图片打水印 缩放 和一个输入流的转换

    图片打水印 缩放 和一个输入流的转换

    2022年1月13日
    52
  • Cloneable_c++list容器

    Cloneable_c++list容器Cloneable类开心一笑开心是最重要的…..哥要出国旅游,让三哥帮忙看家,临走前特别交代:家里的藏獒随便逗,别惹鹦鹉。之后,三哥怎么逗藏獒,藏獒都不咬人。三哥心想:藏獒都这样,这鹦鹉也就一破鸟,能把我怎样?遂逗鹦鹉玩。结果,鹦鹉开口说话:咬他!藏獒扑上……三哥,享年27……自我介绍大家好,我叫能复制,英文名字叫“Cloneable”,住java.lang这个家,我这个人怎么说,

    2022年10月14日
    4
  • Java模式(适配器模式)

    Java模式(适配器模式)

    2021年11月14日
    45
  • C语言刷屏

    C语言刷屏QQ刷屏原理和复制粘贴差不多,只不过是叫系统帮你粘贴并摁下回车键。#include<stdio.h>#include<Windows.h>intmain(){ intn; charname[100]; printf(“请输入窗口名字:”); scanf(“%s”,&name);//窗口名字 printf(“请输入刷屏次数:”);…

    2022年6月8日
    62

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号