亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何有條件地在作用域 Vue 組件中附加元素?

如何有條件地在作用域 Vue 組件中附加元素?

Helenr 2022-06-09 11:24:55
我正在嘗試為雙擊時可以編輯的標題創建一個組件。組件將應該使用的 h-tag 和 title 作為 props,并且應該生成一個普通的 h-tag,一旦雙擊它就會變成一個輸入字段。如果頁面上只有一個標題,這已經有效,但是一旦在一頁上使用了多個組件,它就會中斷,因為組件的范圍不正確。但我不知道怎么做。這是代碼:<template>  <div class="edit-on-click">    <input      :class="sizeClass"      type="text"      v-if="edit"      v-model="editedTitle"      @blur="finishEdit"      @keyup.enter="finishEdit"      v-focus="true"    />    <span v-show="!edit" @dblclick.prevent="edit = true"></span>  </div></template>安裝的鉤子我不知道如何確定范圍:  mounted() {    let node = document.createElement(this.size); // Takes h-tag (h1, h2 etc.)    let titleText = document.createTextNode(this.finalTitle); // Takes title    node.appendChild(titleText);    node.classList.add("editable-title");    // This breaks the code once there are multiple components in the document    document.getElementsByTagName("span")[0].appendChild(node);  },如何以有效的方式確定范圍?非常感謝您!
查看完整描述

1 回答

?
青春有我

TA貢獻1784條經驗 獲得超8個贊

好吧,使用 Vue,您可能希望盡可能避免以“本機”方式創建 DOM 元素,因為您可能會遇到競爭條件,其中 Vue 不知道這些元素的存在,您可能希望在某些時候做出反應時間(在你的情況下,<span>雙擊)。


相反,您可以做的可能是使用 this<component>和v-bind:isprop 動態地“在”這些不同的標題之間“切換”??紤]以下示例:


Vue.component('EditableHeading', {

  template: '#editable-heading',


  props: {

    size: {

      type: String,

      default: 'h1'

    },

    value: {

      type: String,

      required: true

    }

  },


  data() {

    return {

      editing: false

    }

  },


  methods: {

    confirm(e) {

      this.$emit('input', e.target.value);

      this.close();

    },

    start() {

      this.editing = true;

      

      this.$nextTick(() => {

        this.$el.querySelector('input[type="text"]').select();

      });

    },

    close() {

      this.editing = false;

    }

  }

})


new Vue({

  el: '#app',


  data: () => ({

    titleList: [],

    text: 'New Title',

    size: 'h3'

  }),


  methods: {

    addNewTitle() {

      this.titleList.push({

        text: this.text,

        size: this.size

      });

    }

  }

})

.edit-on-click {

  user-select: none;

}


.heading-size {

  margin-top: 1rem;

  width: 24px;

}


p.info {

  background-color: beige;

  border: 1px solid orange;

  color: brown;

  padding: 4px 5px;

  margin-top: 2rem;

}

<script src="https://vuejs.org/js/vue.min.js"></script>


<div id="app">

  <editable-heading 

    v-for="(title, index) of titleList" :key="index" 

    v-model="title.text" 

    :size="title.size">

  </editable-heading>


  <div>

    <label>

      Heading size: 

      <input v-model="size" class="heading-size" />

    </label>

  </div>

  <div>

    <label>

      Title: 

      <input v-model="text" />

    </label>

  </div>

  <div>

    <button @click="addNewTitle()">Add new title</button>

  </div>


  <p class="info">

    [double-click]: Edit <br />

    [enter]: Confirm <br />

    [esc/mouseleave]: Cancel

  </p>

</div>


<script id="editable-heading" type="text/x-template">

  <div class="edit-on-click">

    <input 

      type="text" 

      v-if="editing" 

      :value="value" 

      @blur="close" 

      @keydown.enter="confirm" 

      @keydown.esc="close" />


    <component :is="size" v-else @dblclick="start">{{value}}</component>

  </div>

</script>


查看完整回答
反對 回復 2022-06-09
  • 1 回答
  • 0 關注
  • 104 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號