Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="app">
    <smart-item :data="data"></smart-item>
    <button @click="change('img')">切换为图片组件</button>
    <button @click="change('video')">切换为视频组件</button>
    <button @click="change('text')">切换为文本组件</button>
</div>
              
            
!

CSS

              
                body {
    text-align: center;
}

div img, div video {
    width: 100%;
    height: 100%;
}
              
            
!

JS

              
                //图片组件选项
var ImgItem = {
    props: ['data'],
    render: function (createElement) {
        return createElement('div', [
            createElement('p', '图片组件'),
            createElement('img', {
                attrs: {
                    src: this.data.url
                }
            })
        ])
    }
};
//视频组件选项
var VideoItem = {
    props: ['data'],
    render: function (createElement) {
        return createElement('div', [
            createElement('p', '视频组件'),
            createElement('video', {
                attrs: {
                    src: this.data.url,
                    controls: 'controls',
                    autoplay: 'autoplay'
                }
            })
        ])
    }
};

//文本组件选项
var TextItem = {
    props: ['data'],
    render: function (createElement) {
        return createElement('div', [
            createElement('p', '文本组件'),
            createElement('p', this.data.text)
        ])
    }
};

Vue.component('smart-item', {
    //函数化组件
    functional: true,
    render: function (createElement, context) {
        //根据传入的数据,智能判断显示哪种组件
        function getComponent() {
            var data = context.props.data;
            //判断prop:data的type字段是属于哪种类型的组件
            if (data.type === 'img') {
                return ImgItem;
            }
            if (data.type === 'video') {
                return VideoItem;
            }
            return TextItem;
        }

        return createElement(
            getComponent(),
            {
                props: {
                    //把smart-item的prop:data传给上面智能选择的组件
                    data: context.props.data
                }
            },
            context.children
        )
    },
    props: {
        data: {
            type: Object,
            required: true
        }
    }
});
new Vue({
    el: '#app',
    data: {
        data: {}
    },
    methods: {
        change: function (type) {
            if (type === 'img') {
                this.data = {
                    type: 'img',
                    url: 'http://n.sinaimg.cn/news/1_img/upload/34fa2aa3/729/w2048h1081/20180609/Dac8-hcscwxc3559915.jpg'
                }
            } else if (type === 'video') {
                this.data = {
                    type: 'video',
                    url: 'http://vjs.zencdn.net/v/oceans.mp4'
                }
            } else if (type === 'text') {
                this.data = {
                    type: 'text',
                    context: '这是一段文本。'
                }
            }
        }
    },
    mounted: function () {
        //初始化时,默认设置图片组件的数据
        this.change('img');
    }
});


              
            
!
999px

Console