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,  
  }
}
相关推荐
GinoWi15 分钟前
HTML基本格式 - 第一个HTML网页
前端
顶鲜花的牛粪18 分钟前
Astro 项目升级全栈:EdgeOne Pages 部署指南
前端
0***R51529 分钟前
前端云原生
前端·云原生
月弦笙音39 分钟前
【Promise.withResolvers】发现这个api还挺有用
前端·javascript·typescript
疯狂踩坑人41 分钟前
MCP理论和实战,然后做个MCP脚手架吧
前端·node.js·mcp
中杯可乐多加冰1 小时前
基于 DeepSeek + MateChat 的证券智能投顾技术实践:打造金融领域的专属大Q模型助手
前端·人工智能
凡人程序员1 小时前
搭建简易版monorepo + turborepo
前端·javascript
丸子哥哥1 小时前
同一个域名,如何添加多个网站?
服务器·前端·nginx·微服务
不努力也不会混1 小时前
vite联邦实现微前端(vite-plugin-federation)
前端·vue.js
伍亿伍千万1 小时前
Uptime Kuma修改作为内嵌页面的自适应
前端