uni-app自定义组件

uni-app自定义组件
步骤1.

在项目中新建一个 component文件夹, 用来存放公用组件, 然后有新建组件如下图


image.png
  • 在新建组件中需要在 template中为组件绑定名称,如 itemMoive
<template name="itemMoive">
    <view class="item">
        <image :src=" movie.images.large " class="photo"></image>
        <view class="title">{{ movie.title }}</view>
        <view class="score">
            <block v-if=" movie.stars ">
                <view class="stars">
                    <image v-for="(starItem, starItemIdx) in movie.stars.on" :key="starItemIdx" src="/static/imgs/rating_star_small_on.png"
                     class="star"></image>
                    <image v-for="(starItem, starItemIdx) in movie.stars.half" :key="starItemIdx" src="/static/imgs/rating_star_small_half.png"
                     class="star"></image>
                    <image v-for="(starItem, starItemIdx) in movie.stars.off" :key="starItemIdx" src="/static/imgs/rating_star_small_off.png"
                     class="star"></image>
                    {{ movie.rating.average }}
                </view>
            </block>
            <block v-else>
                <view class="noscore">暂无评分</view>
            </block>
        </view>
    </view>
</template>
  • 还需要在 export default中声明方法,然后在``props```定义需要外界传入的参数
<script>
    export default {
        name: "itemMoive",
        data() {
            return {
                
            };
        },
        // 此处定义传入的数据
        props: {
            movie: {
                type: Object,
                value: null
            }
        }
        
    }
</script>
步骤二.

在需要用组件的页面

  • import 导入
import itemMoive from "components/itemMoive.vue"
warning注意不要写成绝对路径
错误写法
import itemMoive from "/components/itemMoive.vue"
  • 然后在components中注册组件名称,以后用的时候就直接用这个定义的名称
components: {
            // 注册
            itemMoive
        }
步骤3.
  • 具体用法 :movie=即为需要传入的数据
<itemMoive v-for=" (movie,movieIdx) in item.movies " :movie="movie" :key="movieIdx" class="item"></itemMoive>
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容