iT邦幫忙

0

Vue無法渲染新增的節點

  • 分享至 

  • xImage

各位好
菜比八想問一下為何新增的節點Vue完全沒渲染到。還請大德們不吝指教,感謝QQ
https://imgur.com/a/cS7Oe7k

目前試了forceUpdated和mounted,確認了確實會重跑過mounted和updated的階段,但是跑完仍然沒有渲染上去
https://imgur.com/a/tTC4Yy6

補充一下,確認有註冊到component
https://imgur.com/a/UHS8TA9

Ares iT邦研究生 3 級 ‧ 2021-03-02 14:52:12 檢舉
有把 component 註冊到 Vue 嗎?
通靈亡 iT邦高手 1 級 ‧ 2021-03-02 17:03:03 檢舉
良心建議一下,
在 Vue 的思維當中,非必要不要自行操作 Dom
請從資料的角度定義畫面,讓 Vue 幫你處理 Dom 的操作。
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 個回答

1
koro_michael
iT邦新手 2 級 ‧ 2021-03-02 15:54:56
最佳解答

一個簡單的範例,有解決問題幫忙按個最佳解答,謝謝

<html>
  <body>
    <div id="app">
      <button v-on:click="cl()">Add 1</button>
      <div class="hr1"></div>
    </div>
  </body>
</html>
let c = {
	template: '<h1>123456</h1>',
	methods: {}
};

var vm = new Vue({
  el: '#app',
  methods: {
  	cl() {
      let example = Vue.extend(c);
      let component = new example().$mount();
      document.querySelector('.hr1').appendChild(component.$el);
    },
  },
  data: {},
});

範例網址

這是另一種寫法,都是差不多的,看你喜歡哪一種

let c = {
	template: '<h1>123456</h1>',
	methods: {}
};

Vue.component('my-component', c);

var vm = new Vue({
  el: '#app',
  methods: {
  	cl() {
      let component = new (Vue.component('my-component'))().$mount();
      document.querySelector('.hr1').appendChild(component.$el);
    },
  },
  data: {},
});

範例網址

1
EN
iT邦好手 1 級 ‧ 2021-03-02 16:49:54

有可能是我的程度太差,怎麼感覺你是用了 JQuery 的 API @@
如果是這樣,你應該也不需要用 Vue 吧,直接做一個 Handler 就好了:

<button onclick="cl">Click me!</button>
var cl = function(){
// Write your methods here!
}

或是:

<button id="btn1">Click me!</button>
const btn1 = document.querySelector("#btn1");
btn1.addEventListener("click",()=>{
// Write your methods here!
})

然後認真回,你新增的節點會沒渲染到是因為 Vue 2.6 底層所使用的語法特性,請參考該問答串,希望能幫到你><

因為你用截圖附上程式碼,我就沒有貼到 codepen 上測試了,我猜測你是想問新增的節點沒有被 Vue observe ,如果不是,再麻煩你用 Markdown 語法附上程式碼。

EN iT邦好手 1 級 ‧ 2021-03-02 16:50:58 檢舉

PS: 如果你使用的是 Vue 3.0^ ,那就是用法錯誤了。

我要發表回答

立即登入回答