最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue.js組件高級特性實例詳解

 更新時間:2018年12月24日 14:31:44   作者:deniro_li  
這篇文章主要介紹了Vue.js組件高級特性,結合實例形式詳細分析了vue.js組件遞歸、模板、動態(tài)加載、渲染等相關操作技巧,需要的朋友可以參考下

本文實例講述了Vue.js組件高級特性。分享給大家供大家參考,具體如下:

1 遞歸

為組件設置 name 屬性,這個組件就可以在自身的模板內遞歸調用自己。

html:

<div id="app">
  <deniro-component :count="1"></deniro-component>
</div>

js:

Vue.component('deniro-component',{
  name:'deniro-component',
  props:{
    count:{
      type:Number,
      default:1
    }
  },
  template:'\
  <div class="child">\
  <p>{{count}} 微信大變樣!看了這些新功能后,網友淡定不住了</p>\
    <deniro-component\
      :count="count + 1"\
      v-if="count < 3"></deniro-component>\
  </div>\
  '
});
var app = new Vue({
  el: '#app',
  data: {}
});

效果:

渲染結果:

可以利用組件的可遞歸特性,來實現(xiàn)一些具有不確定層級的組件,比如級聯(lián)選擇器和樹型組件。

2 內聯(lián)模板

一般情況下,組件的模板都是在 template 中定義的。我們也可以在組件標簽中加上 inline-template 屬性,這樣組件就會把它的內容作為實際的模板內容。

內聯(lián)模板可以接收父、子組件中聲明的數(shù)據,所以更加靈活。

html:

<div id="app2">
  <deniro-component2 inline-template>
    <div>
      <h2>父組件中定義子組件模板</h2>
      <p>{{content1}}</p>
      <p>{{content2}}</p>
    </div>
  </deniro-component2>
</div>

js:

Vue.component('deniro-component2',{
  data:function () {
    return {
      content1:'雙屏手機一碰就碎?實測結果意外(來源于子組件)'
    }
  }
});
var app2 = new Vue({
  el: '#app2',
  data: {
    content2:'AI正在改變所有行業(yè) 咖啡也將被消滅(來源于父組件)'
  }
});

渲染結果:

<div id="app2">
 <div>
  <h2>父組件中定義子組件模板</h2>
  <p>雙屏手機一碰就碎?實測結果意外(來源于子組件)</p>
  <p>AI正在改變所有行業(yè) 咖啡也將被消滅(來源于父組件)</p>
 </div>
</div>

如果父子組件定義的數(shù)據同名,那么優(yōu)先使用子組件中的數(shù)據。

因為作用域較難理解,所以除非必要,否則不建議使用。

3 動態(tài)加載

我們可以使用 is 來實現(xiàn)動態(tài)掛載組件。

html:

<div id="app3">
  <deniro-component3 :is="currentView"></deniro-component3>
  <button @click="change('A')">切換到 A 組件</button>
  <button @click="change('B')">切換到 B 組件</button>
  <button @click="change('C')">切換到 C 組件</button>
</div>

js:

var app3 = new Vue({
  el: '#app3',
  components: {
    componentA: {
      template: '<div>組件 A</div>'
    },
    componentB: {
      template: '<div>組件 B</div>'
    },
    componentC: {
      template: '<div>組件 C</div>'
    }
  },
  data: {
    currentView: 'componentA'
  },
  methods: {
    change: function (component) {
      this.currentView = 'component' + component;
    }
  }
});

效果:

data 中的 is 變量也可以直接綁定組件對象。

html:

<div id="app4">
  <deniro-component4 :is="currentView"></deniro-component4>
</div>

js:

var news={
  template:'<p>無人機送快遞 漸行漸近</p>'
}
var app4 = new Vue({
  el: '#app4',
  data: {
    currentView: news
  }
});

渲染結果:

<div id="app4">
 <p>無人機送快遞 漸行漸近</p>
</div>

4 異步加載

當工程變得越來越大時,就需要考慮性能嘍。我們可以把組件定義成一個工廠函數(shù),實現(xiàn)組件動態(tài)解析。Vue.js 會把本次渲染后的結果緩存起來,從而提高性能。

html:

<div id="app5">
  <deniro-component5></deniro-component5>
</div>

js:

Vue.component('deniro-component5', function (resolve,reject) {
  window.setTimeout(function () {
    resolve({
      template:'<div>全球首個5G電話撥通</div>'
    });
  },1000);
});
var app5 = new Vue({
  el: '#app5'
});

效果:

這里使用 setTimeout 來模擬異步下載,下載成功后會調用 resolve 方法。

一般情況下,會把組件的配置定義為對象配置,然后通過 Ajax 請求組件配置信息,最后通過 resolve 傳入這些配置。

完整實例代碼:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>vue.js組件高級特性</title>
<script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
</head>
<body>
<div id="app">
  <deniro-component :count="1"></deniro-component>
</div>
<div id="app2">
  <deniro-component2 inline-template>
    <div>
      <h2>父組件中定義子組件模板</h2>
      <p>{{content1}}</p>
      <p>{{content2}}</p>
    </div>
  </deniro-component2>
</div>
<div id="app3">
  <deniro-component3 :is="currentView"></deniro-component3>
  <button @click="change('A')">切換到 A 組件</button>
  <button @click="change('B')">切換到 B 組件</button>
  <button @click="change('C')">切換到 C 組件</button>
</div>
<div id="app4">
  <deniro-component4 :is="currentView"></deniro-component4>
</div>
<div id="app5">
  <deniro-component5></deniro-component5>
</div>
<script>
Vue.component('deniro-component5', function (resolve,reject) {
    window.setTimeout(function () {
      resolve({
        template:'<div>全球首個5G電話撥通</div>'
      });
    },1000);
  });
  var app5 = new Vue({
    el: '#app5'
  });
  var news={
    template:'<p>無人機送快遞 漸行漸近</p>'
  }
  var app4 = new Vue({
    el: '#app4',
    data: {
      currentView: news
    }
  });
  var app3 = new Vue({
    el: '#app3',
    components: {
      componentA: {
        template: '<div>組件 A</div>'
      },
      componentB: {
        template: '<div>組件 B</div>'
      },
      componentC: {
        template: '<div>組件 C</div>'
      }
    },
    data: {
      currentView: 'componentA'
    },
    methods: {
      change: function (component) {
        this.currentView = 'component' + component;
      }
    }
  });
  Vue.component('deniro-component2', {
    data: function () {
      return {
        content1: '雙屏手機一碰就碎?實測結果意外(來源于子組件)'
      }
    }
  });
  var app2 = new Vue({
    el: '#app2',
    data: {
      content2: 'AI正在改變所有行業(yè) 咖啡也將被消滅(來源于父組件)'
    }
  });
  Vue.component('deniro-component', {
    name: 'deniro-component',
    props: {
      count: {
        type: Number,
        default: 1
      }
    },
    template: '\
    <div class="child">\
    <p>{{count}} 微信大變樣!看了這些新功能后,網友淡定不住了</p>\
      <deniro-component\
        :count="count + 1"\
        v-if="count < 3"></deniro-component>\
    </div>\
    '
  });
  var app = new Vue({
    el: '#app',
    data: {}
  });
</script>
</body>
</html>

感興趣的朋友可以使用在線HTML/CSS/JavaScript代碼運行工具http://tools.jb51.net/code/HtmlJsRun 測試上述代碼運行效果。

希望本文所述對大家vue.js程序設計有所幫助。

相關文章

  • 關于vue.extend和vue.component的區(qū)別淺析

    關于vue.extend和vue.component的區(qū)別淺析

    最近工作中遇到了vue.extend,vue.component,但二者之間的區(qū)別與聯(lián)系是什么呢?下面這篇文章主要給大家介紹了關于vue.extend和vue.component區(qū)別的相關資料,需要的朋友可以參考借鑒,下面來一起看看吧。
    2017-08-08
  • 如何解決uni-app編譯后?vendor.js?文件過大

    如何解決uni-app編譯后?vendor.js?文件過大

    這篇文章主要介紹了如何解決uni-app編譯后?vendor.js?文件過大的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • 關于vue-i18n在單文件js中的使用

    關于vue-i18n在單文件js中的使用

    這篇文章主要介紹了關于vue-i18n在單文件js中的使用,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09
  • vue中的keep-alive詳解與應用場景

    vue中的keep-alive詳解與應用場景

    keep-alive是vue中的內置組件,能在組件切換過程中將狀態(tài)保留在內存中,防止重復渲染DOM,本文給大家介紹vue中的keep-alive詳解與應用場景,感興趣的朋友一起看看吧
    2023-11-11
  • vue 如何設置背景顏色及透明度

    vue 如何設置背景顏色及透明度

    這篇文章主要介紹了vue 設置背景顏色及透明度的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • vue Element-ui input 遠程搜索與修改建議顯示模版的示例代碼

    vue Element-ui input 遠程搜索與修改建議顯示模版的示例代碼

    本文分為html,js和css代碼給大家詳細介紹了vue Element-ui input 遠程搜索與修改建議顯示模版功能,感興趣的朋友一起看看吧
    2017-10-10
  • 基于vue-ssr服務端渲染入門詳解

    基于vue-ssr服務端渲染入門詳解

    這篇文章主要介紹了基于vue-ssr服務端渲染入門詳解,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-01-01
  • 手拉手教你如何處理vue項目中的錯誤

    手拉手教你如何處理vue項目中的錯誤

    在項目開發(fā)中經常遇到各種報錯,每次總是通過這樣或那樣的辦法解決掉,這篇文章主要給大家介紹了關于如何處理vue項目中錯誤的相關資料,需要的朋友可以參考下
    2022-06-06
  • Vue.js實現(xiàn)一個SPA登錄頁面的過程【推薦】

    Vue.js實現(xiàn)一個SPA登錄頁面的過程【推薦】

    本篇文章主要介紹了Vue.js寫一個SPA登錄頁面過程的相關知識,具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • vue3中實現(xiàn)使用element-plus調用message

    vue3中實現(xiàn)使用element-plus調用message

    這篇文章主要介紹了vue3中實現(xiàn)使用element-plus調用message,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-09-09

最新評論

龙游县| 津市市| 武穴市| 商洛市| 株洲县| 永年县| 平顶山市| 嵩明县| 县级市| 南和县| 延安市| 韶山市| 化隆| 滦平县| 化州市| 佛山市| 合水县| 巢湖市| 双桥区| 巴里| 雷山县| 汶上县| 新竹县| 江阴市| 潮州市| 织金县| 贵德县| 博乐市| 凌源市| 杨浦区| 涟水县| 邳州市| 满洲里市| 赣州市| 龙泉市| 射阳县| 肇源县| 溧阳市| 探索| 贵港市| 桐梓县|