uni-app

uni-app自定义组件

2019-11-29  本文已影响0人  Mr_Bob_
uni-app自定义组件
步骤1.

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


image.png
<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>
<script>
    export default {
        name: "itemMoive",
        data() {
            return {
                
            };
        },
        // 此处定义传入的数据
        props: {
            movie: {
                type: Object,
                value: null
            }
        }
        
    }
</script>
步骤二.

在需要用组件的页面

import itemMoive from "components/itemMoive.vue"
warning注意不要写成绝对路径
错误写法
import itemMoive from "/components/itemMoive.vue"
components: {
            // 注册
            itemMoive
        }
步骤3.
<itemMoive v-for=" (movie,movieIdx) in item.movies " :movie="movie" :key="movieIdx" class="item"></itemMoive>
上一篇 下一篇

猜你喜欢

热点阅读