Vue.component('home', { template: '<div>首頁</div>' }); Vue.component('news', { template: '<div>最新消息</div>' }); Vue.component('about', { template: '<div>關於我</div>' }); new Vue({ el: '#app', data: { currentView: 'home' }, methods: { goto: function (view) { this.currentView = view } } });
<div id="app"> <button @click="goto('home')">Home</button> <button @click="goto('news')">News</button> <button @click="goto('about')">About</button> <hr> <component :is="currentView"></component> </div> <script src="https://unpkg.com/vue/dist/vue.js"></script>