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,  
  }
}
相关推荐
天下无贼!6 分钟前
【Python】2026版——FastAPI 框架快速搭建后端服务
开发语言·前端·后端·python·aigc·fastapi
GISer_Jing41 分钟前
两种AI交互方式深度解析——浏览器书签&插件
前端·人工智能·ai·prompt
哈__1 小时前
ReactNative项目OpenHarmony三方库集成实战:react-native-device-info
javascript·react native·react.js
前端布鲁伊1 小时前
零代码上线一个图片处理网站,我是如何使唤AI干活的?
前端·ai编程
庄小焱1 小时前
React——React基础语法(2)
前端·javascript·react.js
终端鹿1 小时前
Vue3 核心 API 深度解析:ref / reactive / computed / watch
前端·javascript·vue.js
console.log('npc')1 小时前
partial在react接口定义中是什么意思
前端·javascript·typescript
SuperEugene1 小时前
前端 utils 工具函数规范:拆分 / 命名 / 复用全指南,避开全局污染等高频坑|编码语法规范篇
开发语言·前端·javascript
C澒1 小时前
微前端容器标准化 —— 公共能力篇:通用请求
前端·架构