鍍金池/ 問答/HTML/ vue組件可以自己復(fù)制自己么?

vue組件可以自己復(fù)制自己么?

需求的話是這樣的 右邊的封裝成組件 點(diǎn)擊+ 可以無限制復(fù)制 自己 到右邊

圖片描述

我在組件中引用了自己 但是會(huì)報(bào)錯(cuò) 怎么解決?
圖片描述

組件是這樣的:

<template>
  <div class="c1" >
     <input type="text">
     <i class="el-icon-circle-plus" @click="add('a-component')" ></i>
     <ul class="main-pic">
        <li :is="item.component"  v-for="item in allComponents"></li>
     </ul>

  </div>
 
</template>

<script>
// component1就是這個(gè)組件本身 
import AComponent from './component1.vue'
export default {
  name: 'Node',
  components: {
    'a-component': AComponent,
  },
  data () {
    return {
      allComponents: [],
    }
  },
  methods: {
      add(component) {
        this.allComponents.push({
          'component': component,
            })
        }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.domDiv{
  height: 20px;
  width: 400px;
}

input{
  border: none;
  border-bottom: 1px solid #ccc;
  width: 100px;
}

.el-icon-circle-plus{
  color: green;
}

.c1{
  height: 50px;
  width: 200px;
}

</style>
回答
編輯回答
涼心人

components中注冊(cè)組件了嗎?

components: {aComponent}

2017年3月24日 04:22
編輯回答
小眼睛

vue的組件操作應(yīng)該是這樣:

1、寫好一個(gè)vue組件
2、import引入組件
3、components里注冊(cè)組件
4、使用組件

你的需求是復(fù)制DOM結(jié)構(gòu),而不是復(fù)制vue組件。
根據(jù)你的代碼,我給你提供個(gè)思路,動(dòng)態(tài)循環(huán)組件:

template

<template>
  <div class="c1" v-for="temp in tempArr">
     <input type="text">
     <i class="el-icon-circle-plus" @click="addTemp" ></i>
     <ul class="main-pic">
        <li :is="item.component"  v-for="item in allComponents"></li>
     </ul>

  </div>
 
</template>
methods
data () {
    return {tempArr: [] // 隨便填數(shù)據(jù),占個(gè)位就行了}
},
methods: {
    addTemp () {
        this.tempArr.push('')  // 隨便添加一個(gè)數(shù)據(jù),讓數(shù)組長(zhǎng)度+1,dom自然多一組了
    }

}
2018年9月19日 06:24
編輯回答
尛憇藌

你可以搜一下遞歸組件,其實(shí)name就是用來引用自己的

2017年2月22日 17:19
編輯回答
離夢(mèng)

直接添加組件標(biāo)簽就行了啊 <main-theme></main-theme>

2017年9月27日 01:24
編輯回答
尐飯團(tuán)

遞歸組件 ,使用name命名此組件一樣的名字,然后直接在此組件里面引入

2018年5月28日 16:00