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

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

關于 Vue Component 的繼承問題

關于 Vue Component 的繼承問題

幕布斯6054654 2018-11-13 20:29:06
最近在看 Vue Component 如何實現繼承的寫法。通過瀏覽官方文檔,我看到了可以使用 mixins 和 extends 這兩個方式實現 Vue Component 的擴展。但是這兩個方法都會有一個問題,super 組件的 lifecycle function 都會執行。示例如下:var superComponent = {   //......    created() {      console.log("super component created!");    } }var component = {   extends: superComponent,   //......    created() {      console.log("component created!");    } }組件 component 被渲染到頁面上之后,查看控制臺,會出現兩段字樣:super component created! component created!那如何做到,在 super component 和 component 都有 lifecycle 的時候,只有 component 的 lifecycle function 會執行呢?
查看完整描述

1 回答

?
繁花不似錦

TA貢獻1851條經驗 獲得超4個贊

鉤子函數默認的合并策略:

function mergeHook (
  parentVal: ?Array<Function>,
  childVal: ?Function | ?Array<Function>): ?Array<Function> {  return childVal
    ? parentVal
      ? parentVal.concat(childVal)
      : Array.isArray(childVal)
        ? childVal
        : [childVal]
    : parentVal
}

鉤子函數最終會被合并為一個數組

config.optionMergeStrategies

你可以自定義optionMergeStrategies來寫自己的合并策略

Vue.config.optionMergeStrategies.created = function (parent, child, vm) {  return parent;
}


查看完整回答
反對 回復 2018-11-13
  • 1 回答
  • 0 關注
  • 469 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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