利用component组件和is属性实现动态组件

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

<head>
    <title></title>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <script src="/vue2.js"></script>
</head>

<body>
    <div id="app">
        <ul>
            <li @click='test="che"'><a href="#">车厘子</a></li>
            <li @click='test="mi"'><a href="#">猕猴桃</a></li>
        </ul>
        <!-- component标签创建动态组件,他的is属性指向谁,就显示哪个组件 -->
        <component :is='test'></component>
    </div>
    <script>
        Vue.component('che', {
            template: `<p>这是车厘子</p>`
        })
        Vue.component('mi', {
            template: `<p>这是猕猴桃</p>`
        })
        var vm = new Vue({
            el: '#app',
            data: {
                test: ''
            }
        })
    </script>
</body>

</html>

猜你喜欢

转载自blog.csdn.net/weixin_42442123/article/details/85791115