Vue是怎么渲染template內(nèi)的標(biāo)簽內(nèi)容的
我們在使用Vue做項(xiàng)目時(shí),都會(huì)用到腳手架,相應(yīng)的我們會(huì)在template寫標(biāo)簽內(nèi)容。那么你知道為什么會(huì)在template寫標(biāo)簽嗎?這當(dāng)中經(jīng)過了怎樣的處理呢?
<template> <div id="app"> <div id="nav"> </div> <router-view/> </div> </template> <style lang="less"> </style>
其實(shí)Vue在處理template時(shí),是經(jīng)過這樣處理的,它是通過內(nèi)置的render方法處理我們輸入的標(biāo)簽,生成VNodes。下面我注釋了template內(nèi)的代碼,你可以先看下效果,然后注釋掉render方法內(nèi)的內(nèi)容,取消注釋template??聪虑昂笮Ч欠褚粯?。
<!DOCTYPE html> <html> <head> <title>render</title> </head> <style type="text/css"> #text{ font-weight: bold; font-size: 26px; } </style> <body> <div id="app"> </div> </body> <script type="text/javascript" src="https://unpkg.com/vue@2.6.11/dist/vue.js"></script> <script type="text/javascript"> const vm = new Vue({ el:'#app', data: { text: 'hello world', style1: { width: '200px', height: '200px', border: '1px solid red' }, style2: { textAlign: 'center' }, colorText: { color:'blue' } }, // template:`<div :style='style1'> // <p :style='style2'> // <span :style='colorText' @click='cli()' id='text'>{{text}}</span> // </p> // </div>`, // methods:{ // cli(){ // alert(1) // } // }, render(createElement) { return createElement('div', { style: this.style1 }, [ createElement('p', { style: this.style2 }, [createElement('span', { style: this.colorText, attrs:{ id:'text' }, on:{ click:()=>{ alert(1) } } }, this.text)]) ]) } }) </script> </html>
到此這篇關(guān)于Vue是怎么渲染template內(nèi)的標(biāo)簽內(nèi)容的的文章就介紹到這了,更多相關(guān)Vue渲染template內(nèi)容內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Javascript vue.js表格分頁,ajax異步加載數(shù)據(jù)
這篇文章主要介紹了Javascript vue.js表格分頁,ajax異步加載數(shù)據(jù)的相關(guān)資料,需要的朋友可以參考下2016-10-10vue使用節(jié)流函數(shù)的踩坑實(shí)例指南
防抖和節(jié)流的目的都是為了減少不必要的計(jì)算,下面這篇文章主要給大家介紹了關(guān)于vue使用節(jié)流函數(shù)踩坑的相關(guān)資料,需要的朋友可以參考下2021-05-05vue使用splice()刪除數(shù)組中的一個(gè)數(shù)據(jù) 彈出窗口提示問題
這篇文章主要介紹了vue使用splice()刪除數(shù)組中的一個(gè)數(shù)據(jù) 彈出窗口提示問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07vue element-ui之怎么封裝一個(gè)自己的組件的詳解
這篇文章主要介紹了vue element-ui之怎么封裝一個(gè)自己的組件,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05在 Vue 3 中設(shè)置 `@` 指向根目錄的幾種常見方法匯總
在 Vue 3 項(xiàng)目開發(fā)中,為了方便管理和引用文件路徑,設(shè)置 @ 指向根目錄是一項(xiàng)常見的需求,下面給大家分享在Vue3中設(shè)置 `@` 指向根目錄的方法匯總,感興趣的朋友一起看看吧2024-06-06