javascript - vue 使用component 動(dòng)態(tài)組件為什么不成功
問題描述
1.為什么使用component 動(dòng)態(tài)的添加組件沒有成功,
<template>
<component @showHide='recieveAddData' :is='addModal' ></component> <button @click='switchComponent'></button>
</template>import modal from ’./company/modal.vue’export default {
name: ’addItem’,data () { addModal: ’modal’},methods: { switchComponent () { this.addModal = ’first’},components: { modal, first: { template: '<p>這里是子組件3</p>' }}
}
為什么組件first是可以動(dòng)態(tài)的添加上的,為什么引入的modal 組件不行呢?
問題解答
回答1:modal不是最開始的組件嗎..是mounted時(shí)候無法加載modal.點(diǎn)了button之后反而可以加載first ?
還有一點(diǎn).data正確寫法是需要返回一個(gè)對象
data() { return {}}回答2:
import modal from ’./company/modal.vue’;export default {name: ’addItem’,methods: { switchComponent () { this.addModal = ’first’},computed:{ addmodal:modal },components: { first: { template: '<p>這里是子組件3</p>' }}}
你在components中的modal去掉,addModal的值寫成modal,而不是’modal’;
相關(guān)文章:
1. mysql優(yōu)化 - 關(guān)于mysql分區(qū)2. javascript - 循環(huán)嵌套多個(gè)promise應(yīng)該如何實(shí)現(xiàn)?3. 前端 - IE9 css兼容問題4. css - 移動(dòng)端字體設(shè)置問題5. html5 - 如何實(shí)現(xiàn)帶陰影的不規(guī)則容器?6. vue.js - vue 打包后 nginx 服務(wù)端API請求跨域問題無法解決。7. javascript - ionic2 input autofocus 電腦成功,iOS手機(jī)鍵盤不彈出8. node.js - 在vuejs-templates/webpack中dev-server.js里為什么要exports readyPromise?9. css3 - rem布局下,用戶瀏覽器的最小字號(hào)是12px怎么辦?10. objective-c - iOS開發(fā)支付寶和微信支付完成為什么跳轉(zhuǎn)到了之前開發(fā)的一個(gè)app?
