<html> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Vue</title> <script src="https://www.hxstrive.com/cdn/libs/vue/2.7.16/vue.min.js"></script> </head> <body> <div id="app"> <div> <button type="button" v-on:click="currentTab='tab-home'">Home</button> <button type="button" v-on:click="currentTab='tab-posts'">Posts</button> <button type="button" v-on:click="currentTab='tab-archive'">Archive</button> </div> <div style="border:solid 1px #000;padding:20px;"> <!-- 失活的组件将会被缓存!--> <keep-alive> <!-- 动态组件,根据 currentTab 中的值(组件)动态加载组件 --> <component v-bind:is="currentTab"></component> </keep-alive> </div> </div> <script type="text/javascript"> Vue.component("tab-home", { template: ` <div> <input type="checkbox" /> Home component </div> ` }); Vue.component("tab-posts", { template: ` <div> <input type="checkbox" /> Posts component </div> ` }); Vue.component("tab-archive", { template: ` <div> <input type="checkbox" /> Archive component </div> ` }); var app = new Vue({ el: "#app", data: { currentTab: "tab-home" } }); </script> </body> </html>
在线代码编辑器
HTML / CSS / JavaScript 实时预览
更多工具
运行
保存
自动执行
模板
JS 库
清空
主题
代码编辑区
字符数: 0
提示: 输入完整的 HTML 代码,需包含 DOCTYPE 声明与 html 根标签
预览效果
选择模板
取消
选择库和版本
库
版本
文件
取消
已复制到剪贴板