组件之间的切换

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>全局组件</title>
</head>
<body>
<div id="app">
    <father></father>
    <son></son>
    <hr>
    <div :is='component'></div>
    <button @click="toggle">切换</button>
</div>
<template id="father">
    <div>
        我是父组件
    </div>
</template>
<template id="son">
    <div>
        我是子组件
    </div>
</template>
<script src="vue.js"></script>
<script>
    Vue.component("father",{
        template:"#father"
    });
    Vue.component("son",{
        template:"#son"
    });
    new Vue({
        el:"#app",
        data:{
            component:'son'
        },
        methods:{
            toggle(){
                this.component=this.component=='son'?"father":'son';
            }
    }
    });
</script>
</body>
</html>

结果截图:

猜你喜欢

转载自blog.csdn.net/hemeng0115/article/details/85244203
今日推荐