Vue练习1:组件开发1(头像组件)

样式预览

注释代码

javascript 复制代码
<template>
  <div 
    class="img-box"
    :style="{                //动态style必须为对象
        width: size + 'rem',
        height: size + 'rem'
    }">
    <img
        class="avatar-img"
        :src="url"    //动态url
    />
  </div>
</template>

<script>
export default {
    props:{    //外部传参
        url:{
            type: String,   //属性必须为字符串
            required: true, //属性必传
        },
        size:{
            type: Number,
        }
    }
}
</script>

<style lang="less" scoped>    //lang="less"    使用less /scoped->局部style
    .img-box{
        display: block;
        border-radius: 50%;
        overflow: hidden;
        img{
            width: 100%; height: 100%;
        }
    }
</style>

可运行代码

javascript 复制代码
<template>
  <div 
    class="img-box"
    :style="{
        width: size + 'rem',
        height: size + 'rem'
    }">
    <img
        class="avatar-img"
        :src="url"
    />
  </div>
</template>

<script>
export default {
    props:{
        url:{
            type: String,   //属性必须为字符串
            required: true, //属性必传
        },
        size:{
            type: Number,
        }
    }
}
</script>

<style lang="less" scoped>
    .img-box{
        display: block;
        border-radius: 50%;
        overflow: hidden;
        img{
            width: 100%; height: 100%;
        }
    }
</style>

引用

javascript 复制代码
import Avatar from "./components/Avatar.vue";
export default {
  components: {
    Avatar,  
  }
}
相关推荐
_r0bin_2 小时前
前端面试准备-7
开发语言·前端·javascript·fetch·跨域·class
IT瘾君2 小时前
JavaWeb:前端工程化-Vue
前端·javascript·vue.js
zhang98800002 小时前
JavaScript 核心原理深度解析-不停留于表面的VUE等的使用!
开发语言·javascript·vue.js
potender2 小时前
前端框架Vue
前端·vue.js·前端框架
站在风口的猪11083 小时前
《前端面试题:CSS预处理器(Sass、Less等)》
前端·css·html·less·css3·sass·html5
程序员的世界你不懂3 小时前
(9)-Fiddler抓包-Fiddler如何设置捕获Https会话
前端·https·fiddler
MoFe13 小时前
【.net core】天地图坐标转换为高德地图坐标(WGS84 坐标转 GCJ02 坐标)
java·前端·.netcore
去旅行、在路上4 小时前
chrome使用手机调试触屏web
前端·chrome
Aphasia3114 小时前
模式验证库——zod
前端·react.js
lexiangqicheng5 小时前
es6+和css3新增的特性有哪些
前端·es6·css3