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,  
  }
}
相关推荐
光影少年1 天前
WEBNN是什么,对前端工程带来哪些优势
前端·web3·web
djk88881 天前
极简后台框架
前端·css·css3
LilySesy1 天前
ABAP+如果在join的时候需要表1的字段某几位等于表2的字段的某几位,需要怎么做?
服务器·前端·数据库·sap·abap·alv
涤生啊1 天前
一键搭建 Coze 智能体对话页面:支持流式输出 + 图片直显,开发效率拉满!
javascript·html5
吃饺子不吃馅1 天前
⚡️ Zustand 撤销重做利器:Zundo 实现原理深度解析
前端·javascript·github
幼儿园技术家1 天前
网站在苹果 Safari 进行适配遇到的问题
前端
IT_陈寒1 天前
7个鲜为人知的JavaScript性能优化技巧,让你的网页加载速度提升50%
前端·人工智能·后端
不坑老师1 天前
不坑盒子的插入网页功能是完全免费的!
前端·html
Wang's Blog1 天前
前端FAQ: 描述⼀下你最近使⽤过的前端框架,并解释为何选择它们?
前端·vue.js·faq
wgego1 天前
做题笔记BUU (XSS-Lab)(1-14)
前端·笔记·xss