vue笔记-13(组件基础)

2020-09-23  本文已影响0人  7ColorLotus

组件--基础

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>创建组件</title>
    <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script>

</head>
<body>
    <div id="app">
        <!-- 必须包含在vm实例指定的el中 -->
        <my-com1></my-com1> 

        <mycom2></mycom2>

        <mycom3></mycom3>
        
    </div>

    <template id="tmpl1">
        <div>
            <h3>这是template元素,在外部定义的组件结构</h3>
        </div>
    </template>


    <script>

        //方式一
        // var com1 = Vue.extend({
        //     template: '<h3>这是使用Vue.extend创建的组件</h3>'
        // })
        // Vue.component('myCom1', com1)
        
        //方式一的简写
        Vue.component('myCom1', Vue.extend({
            template: '<h3>这是使用Vue.extend创建的组件</h3>'
        }))

        //方式二 
        Vue.component('mycom2', {
            template: '<h3>这是使用方式二创建的组件</h3>'
        })
        
        //方式三
        Vue.component('mycom3',{
            template: '#tmpl1'
        })


        var vm = new Vue({
            el :"#app"
        })

    </script>
</body>
</html>
上一篇 下一篇

猜你喜欢

热点阅读