441 vue内置组件component

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8" />
    <title>Document</title>
</head>

<body>
    <!-- 
       component 内置组件
     -->

    <div id="app">
        <button @click="fn('one')">One</button>
        <button @click="fn('two')">Two</button>

        <!-- 
        is : 负责显示哪个组件
        值 : 组件的名称 , name 
       -->
        <component :is="componentName">
            <one></one>
            <two></two>
        </component>
    </div>

    <script src="./vue.js"></script>
    <script>
        Vue.component('one', {
            name: 'one',
            template: `<div>子组件one</div>`
        })
        
        Vue.component('two', {
            name: 'two',
            template: `<div>子组件two</div>`
        })

        const vm = new Vue({
            el: '#app',
            data: {
                componentName: 'one'
            },
            methods: {
                fn(name) {
                    this.componentName = name
                }
            }
        })
    </script>
</body>

</html>

posted on 2020-04-13 18:08  冲啊!  阅读(523)  评论(0编辑  收藏  举报

导航