Vue2.0 render:h => h(App)


首先需要了解這是 es 6 的語法,表示 Vue 實例選項對象的 render 方法作為一個函數,接受傳入的參數 h 函數,返回 h(App) 的函數調用結果;

其次,Vue 在創建 Vue 實例時,通過調用 render 方法來渲染實例的 DOM 樹;

最后,Vue 在調用 render 方法時,會傳入一個 createElement 函數作為參數,也就是這里的 h 的實參是 createElement 函數,然后 createElement 會以 APP 為參數進行調用,關於 createElement 函數的參數說明參見:Element-Arguments

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
import '@/assets/iconfont.css'
import '@/assets/styles/main.scss'

Vue.config.productionTip = false
Vue.use(ElementUI)

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  // template: '<App/>',
  // components: {App}
  render:h => h(App)
})

render函數是渲染一個視圖,然后提供給el掛載,如果你沒有加那么就等於沒有視圖給el掛載,自然什么都沒有了。

//另外
{
    render: h => h(App)
}

//等價於
{
    render: h => {
        return h(App)
    }
}

//其實只是簡寫的方式。
//如果不涉及使用this的話下面這個也是等價的
{
    render: function (h) {
        return h(App)
    }
}

render 是vue里面實現渲染dom的函數,這句的目的是渲染這個app實例。用App這個模板。

const app=new Vue({
    el:'#app',
    router,
    compoments:{
        App
    },
    template:"<App/>"
});

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM