javascript - vue 使用component 動態元件為什麼不成功
仅有的幸福
仅有的幸福 2017-05-31 10:39:56
0
2
753

1.為什麼使用component 動態的新增元件沒有成功,

<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是可以動態的添加上的,為什麼引入的modal 元件不行呢?

仅有的幸福
仅有的幸福

全部回覆(2)
PHPzhong

modal不是最開始的組件嗎..
是mounted時候無法載入modal.
點了button之後反而可以載入first ?

還有一點.
data正確寫法是需要回傳一個物件

data() {
    return {}
}
黄舟
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';

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!