欢迎光临
我们一直在努力

uni-app自定义组件

步骤1.

在项目中新建一个 components文件夹, 用来存放公用组件,

在新建组件中需要在 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>
赞(0)
版权归原作者所有,如有侵权请告知。达维营-前端网 » uni-app自定义组件

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址