是否可以$refs從dataVue (2.6.12) 組件的部分訪問,例如
flickingPlugins: [new Arrow({
prevElSelector: '.overview-arrow-prev',
nextElSelector: '.overview-arrow-next',
parentEl: this.$refs['overview-arrows'], // _this is undefined
})],
我問的原因是因為Flicking slider/carousel component 的要求。該組件有一個額外的箭頭插件,用于在兩端出現箭頭以控制左右滑動內容。該插件檔案(第三個例子)提供了Vue的例子,看起來像這樣:
data() {
return {
plugins: [new Arrow({ parentEl: document.body })]
}
}
該parentEl屬性是一種指定自定義箭頭控制元件元素所在位置的方法,需要一個 HTMLElement 值。我一直被告知應該使用 refs 來訪問元素,但是在這種情況下你會怎么做呢?
uj5u.com熱心網友回復:
據我所知和生命周期檔案所讀,由于 Vue.js 的設計,這是不可能的。
已創建
[...] 在此階段,實體已完成對選項的處理,這意味著已設定以下內容:資料觀察、計算屬性、[...]。但是,安裝階段尚未開始,該
$el屬性尚未可用。
因此,您必須等到組件安裝完畢才能訪問元素及其參考。
為什么不嘗試這樣的方法:
{
// ...
data () {
// no access to $el and $refs
return {
plugins: []
}
},
mounted () {
// here you have access to $el and $refs
this.plugins.push(new Arrow({ parentEl: /* ... */ }))
},
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/332655.html
上一篇:為輸入id生成uuid
