vue動態(tài)組件的使用:
1.在父組件中去引入對應的組件。
import HomeView from "./HomeView.vue";
import AboutView from "./AboutView.vue";
export default {
components: {
HomeView,
AboutView,
}
}
2.在data中設置currentTabComponent屬性。
export default {
data () {
return {
currentTabComponent: "HomeView",
}
}
}
3.在父組件中去使用動態(tài)組件,通過使用currentTabComponent屬性確定需要展示的組件。
<component v-bind:is="currentTabComponent"></component>
4.如何切換動態(tài)組件的組件。
可以去設置一個點擊事件,在點擊時切換需要顯示的內(nèi)容。
例如:點擊時調(diào)用方法切換動態(tài)組件要展示組件的名字,即可以切換組件內(nèi)容。
changeComponentName() {
this.currentTabComponent = "AboutView";
}