Vue3 | 基礎(chǔ)語法 與 生命周期知識(shí)及案例詳解

完整原文地址見簡書http://www.reibang.com/p/8fa37a3c8e6e




createApp()、mount()、MVVM测蘑、根組件實(shí)例

-【createApp()】
Vue.createApp()灌危,創(chuàng)建Vue應(yīng)用實(shí)例,開始使用Vue碳胳;

-【mount()】
.mount()指定在哪個(gè)組件上使用Vue
(這個(gè)在《Vue3 | 基本特性概念 與語法的 應(yīng)用與案例》中已經(jīng)講過了)勇蝙;

-傳入createApp()的參數(shù),描述了這個(gè)Vue應(yīng)用實(shí)例的展示內(nèi)容固逗,包括數(shù)據(jù)浅蚪、UI、各種事件等烫罩;

-【MVVM】
我們說Vue架構(gòu)使用了MVVM的設(shè)計(jì)思想,其中data()是M層洽故,template是V層贝攒,
VM層視圖數(shù)據(jù)連接層,由Vue組件幫我們完成时甚;

-【Vue應(yīng)用實(shí)例根組件實(shí)例】
以下代碼中隘弊,const vm = heheApp.mount('#root_div');這里,
vm接收的組件實(shí)例荒适,就是這個(gè)Vue應(yīng)用實(shí)例根組件梨熙;
我們可以使用這個(gè)根組件實(shí)例訪問到組件內(nèi)相關(guān)的 數(shù)據(jù)(data)等字段(詳見下面案例):

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello World! heheheheheheda</title>
    <script src="https://unpkg.com/vue@next"></script>
</head>
<body>
    <div id="root_div"></div>
</body>

<script>
    const heheApp = Vue.createApp({
        data(){
            return{
                divText:'hehehedadada'
            }
        },
        template:`
        <div>{{divText}}</div>
        `
    });

    const vm = heheApp.mount('#root_div');
</script>
</html>

運(yùn)行效果:



可以打開控制臺(tái)進(jìn)行終端操作:

輸入剛剛案例代碼中的Vue實(shí)例(heheApp),查看實(shí)例內(nèi)容:

查看根組件實(shí)例vm及其內(nèi)容:
查看vm的字段:
查看vm的data字段:
對data中的數(shù)據(jù)字段賦值刀诬,
觸發(fā)Vue的ViewModel層效果咽扇,UI數(shù)據(jù)雙向綁定,
使得綁定這個(gè)數(shù)據(jù)字段對應(yīng)的UI發(fā)生改變:
再嘗試:




生命周期

先貼上案例代碼:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Hello World! heheheheheheda</title>
    <script src="https://unpkg.com/vue@next"></script>
</head>
<body>
    <div id="heheApp">
      <h1>{{heheString + ' ———— string in outer HTML'}}</h1>
    </div>
</body>
  <script>
    var vm = Vue.createApp({
      el:'#heheApp',
      data() {
        return{
            heheString: 'Vue的生命周期'
        }
      },
      beforeCreate: function() {
        console.group('------beforeCreate創(chuàng)建前狀態(tài)------');
        console.log("%c%s", "color:red" , "el     : " + this.$el); //undefined
        console.log("%c%s", "color:red","data   : " + this.$data); //undefined 
        console.log("%c%s", "color:red","heheString: " + this.heheString) 
      },
      created: function() {
        console.group('------created創(chuàng)建完畢狀態(tài)------');
        console.log("%c%s", "color:red","el     : " + this.$el); //undefined
        console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化 
        console.log("%c%s", "color:red","heheString: " + this.heheString); //已被初始化
      },
      beforeMount: function() {
        console.group('------beforeMount掛載前狀態(tài)------');
        console.log("%c%s", "color:red","el     : " + (this.$el)); //已被初始化
        console.log(this.$el);
        console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化  
        console.log("%c%s", "color:red","heheString: " + this.heheString); //已被初始化  
        console.log("%c%s", "color:red", 
            document.getElementById('heheApp').innerHTML, " ———— beforeMount");
      },
      mounted: function() {
        console.group('------mounted 掛載結(jié)束狀態(tài)------');
        console.log("%c%s", "color:red","el     : " + this.$el); //已被初始化
        console.log(this.$el);    
        console.log("%c%s", "color:red","data   : " + this.$data); //已被初始化
        console.log("%c%s", "color:red","heheString: " + this.heheString); //已被初始化 
        console.log("%c%s", "color:red", 
            document.getElementById('heheApp').innerHTML, " ———— mounted");
      },
      beforeUpdate: function () {
        console.group('beforeUpdate 更新前狀態(tài)===============》');
        console.log("%c%s", "color:red","el     : " + this.$el);
        console.log(this.$el);   
        console.log("%c%s", "color:red","data   : " + this.$data); 
        console.log("%c%s", "color:red","heheString: " + this.heheString); 
        console.log("%c%s", "color:red", 
            document.getElementById('heheApp').innerHTML, " ———— beforeUpdate");
      },
      updated: function () {
        console.group('updated 更新完成狀態(tài)===============》');
        console.log("%c%s", "color:red","el     : " + this.$el);
        console.log(this.$el); 
        console.log("%c%s", "color:red","data   : " + this.$data); 
        console.log("%c%s", "color:red","heheString: " + this.heheString); 
        console.log("%c%s", "color:red", 
            document.getElementById('heheApp').innerHTML, " ———— updated");
      },
      beforeUnmount: function () {
        console.group('beforeDestroy 銷毀前狀態(tài)===============》');
        console.log("%c%s", "color:red","el     : " + this.$el);
        console.log(this.$el);    
        console.log("%c%s", "color:red","data   : " + this.$data); 
        console.log("%c%s", "color:red","heheString: " + this.heheString); 
        console.log("%c%s", "color:red", 
            document.getElementById('heheApp').innerHTML, " ———— beforeUnmount");
      },
      unmounted: function () {
        console.group('destroyed 銷毀完成狀態(tài)===============》');
        console.log("%c%s", "color:red","el     : " + this.$el);
        console.log(this.$el);  
        console.log("%c%s", "color:red","data   : " + this.$data); 
        console.log("%c%s", "color:red","heheString: " + this.heheString);
        console.log("%c%s", "color:red", 
            document.getElementById('heheApp').innerHTML, " ———— unmounted");
      },
      template: "<h1>{{this.heheString +' ———— string in template'}}</h1>"
    })
    const vmR = vm.mount('#heheApp');
  </script>
</html>

運(yùn)行結(jié)果:
還有 54% 的精彩內(nèi)容
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
支付 ¥1.00 繼續(xù)閱讀
  • 序言:七十年代末陕壹,一起剝皮案震驚了整個(gè)濱河市质欲,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌糠馆,老刑警劉巖嘶伟,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異又碌,居然都是意外死亡九昧,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進(jìn)店門毕匀,熙熙樓的掌柜王于貴愁眉苦臉地迎上來铸鹰,“玉大人,你說我怎么就攤上這事期揪〉粞伲” “怎么了?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵摩幔,是天一觀的道長狼纬。 經(jīng)常有香客問我,道長赁温,這世上最難降的妖魔是什么速兔? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任墅拭,我火速辦了婚禮,結(jié)果婚禮上涣狗,老公的妹妹穿的比我還像新娘谍婉。我一直安慰自己,他們只是感情好镀钓,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布穗熬。 她就那樣靜靜地躺著,像睡著了一般丁溅。 火紅的嫁衣襯著肌膚如雪唤蔗。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天窟赏,我揣著相機(jī)與錄音妓柜,去河邊找鬼。 笑死涯穷,一個(gè)胖子當(dāng)著我的面吹牛棍掐,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播拷况,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼作煌,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了蝠嘉?” 一聲冷哼從身側(cè)響起最疆,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蚤告,沒想到半個(gè)月后努酸,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡杜恰,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年获诈,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片心褐。...
    茶點(diǎn)故事閱讀 39,779評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡舔涎,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出逗爹,到底是詐尸還是另有隱情亡嫌,我是刑警寧澤嚎于,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站挟冠,受9級特大地震影響于购,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜知染,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一肋僧、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧控淡,春花似錦嫌吠、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至涧狮,卻和暖如春泥栖,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背勋篓。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留魏割,地道東北人譬嚣。 一個(gè)月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓,卻偏偏與公主長得像钞它,于是被迫代替她去往敵國和親拜银。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評論 2 354

推薦閱讀更多精彩內(nèi)容