Implementing Picture Preview Component Based on Element UI

This is a simple click preview component
By the way, record scope pits in vue stepped on during component writing~

Start with registering global components!

  • Project catalogue:

Imitate elementUI directory structure, directory name is component name, SRC is component source file (or js service file), there is also an index.js in the file directory for the same management of all files in src, export and registration, this component we only have one vue file

  • First look at what's in the index.js file:
//vue component files under src are introduced
import starPicList from './src/star-pic-list';

/* istanbul ignore next */
starPicList.install = function(Vue) {
    // is the name of the component that can be used later (as defined in the star-pic-list.vue file), and install is the default method.
    Vue.component(, starPicList);

export default starPicList;

Next, I introduce the install method:
Vue. use (plugin): Install the Vue. JS plug-in. If the plug-in is an object, you must provide an install method. If the plug-in is a function, it will be used as an install method. The install method is called as a parameter to the Vue. When the install method is called multiple times by the same plug-in, the plug-in will only be installed once. The plug-in for Vue.js should have a public method install. The first parameter of this method is the Vue constructor, and the second parameter is an optional option object:

  • After exporting the starPicList component, it is referenced in the management component js file, and then exported and registered globally by the unified management js file:

Well, these are nonsense!

  • Use of components:
<! - Picture list form, Click to view the picture list, click on the display of the top (bottom) one - >
<template v-slot="scope">
    <star-pic-list :data="scope.row.pic" :max-show="2"/>

data: an array of incoming pictures;
max-show: Show up to a few pictures at a time

  • The results are as follows:

  • Supplementary: After adding scoped s to style in vue component development, modifying the third-party component style has no effect.

In the development of vue, we usually cooperate with element-UI, and we will encounter that after adding scoped s to component style, the style of sub-components used in element-ui can not be changed.
Remove this attribute without scoped, but pollute the global style (with less or scss recommended), all of which are written under the current component id or class)

Component source code:

    <div id="star-pic-vue">
        <template v-if="data">
            <img v-for="item in images"
            <template v-if="isDialogShow">
                <el-carousel :autoplay="false" arrow="always">
                    <el-carousel-item v-for="item in data" :key="item">
                        <img :src="item">

    export default {
        name: "star-pic-list",
        props: ["data","maxShow"],
                centerDialogVisible: false,
                showPic: '',
                isDialogShow: false,
                index: 0,
        computed: {
          images() {
              if ( instanceof Array && > 2) {
              } else {
        methods: {
            // Enlarge the picture.
                this.isDialogShow = true;
                this.centerDialogVisible = true;
                this.showPic =[0];

<style lang="less">
    width: 200px;
    height: auto;
    display: flex;
    flex-wrap: wrap;
        width: 80px;
        height: 80px;
        margin: 4px;
    .dialog {
            width: 100%;
            height: 100%;
            margin: 0;
    .el-carousel__item h3 {
        color: #475669;
        font-size: 18px;
        opacity: 0.75;
        line-height: 300px;
        margin: 0;
        height: 100%;
        width: 100%;
        display: none;
    .el-dialog__body {
        padding: 0 !important;
        margin: 0 !important;
        height: 600px;
        height: 100%;
    .el-carousel__container {
        height: 100%;

More Components Click Here - > ____________ link : github>components>star-pic-list

Keywords: Javascript Vue less Attribute github

Added by reddevil on Wed, 15 May 2019 05:40:54 +0300