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

vue組件定義,全局、局部組件,配合模板及動(dòng)態(tài)組件功能示例

 更新時(shí)間:2019年03月19日 08:46:02   作者:白楊-M  
這篇文章主要介紹了vue組件定義,全局、局部組件,配合模板及動(dòng)態(tài)組件功能,結(jié)合實(shí)例形式分析了vue.js中組件的定義、全局組件、局部組件、配合模板組件及動(dòng)態(tài)組件的相關(guān)使用方法與操作注意事項(xiàng),需要的朋友可以參考下

本文實(shí)例講述了vue組件定義,全局、局部組件,配合模板及動(dòng)態(tài)組件功能。分享給大家供大家參考,具體如下:

一、定義一個(gè)組件

定義一個(gè)組件:

1. 全局組件

var Aaa=Vue.extend({
 template:'<h3>我是標(biāo)題3</h3>'
});
Vue.component('aaa',Aaa);

*組件里面放數(shù)據(jù):

data必須是函數(shù)的形式,函數(shù)必須返回一個(gè)對(duì)象(json)

2. 局部組件

放到某個(gè)組件內(nèi)部

var vm=new Vue({
 el:'#box',
 data:{
  bSign:true
 },
 components:{ //局部組件
  aaa:Aaa
 }
});

1. 全局組件

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
 <style>
 </style>
</head>
<body>
 <div id="box">
  <aaa></aaa>
 </div>
 <script>
  var Aaa=Vue.extend({
   template:'<h3>我是標(biāo)題3</h3>'
  });
  Vue.component('aaa',Aaa);
  var vm=new Vue({
   el:'#box',
   data:{
    bSign:true
   }
  });
 </script>
</body>
</html>

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
 <style>
 </style>
</head>
<body>
 <div id="box">
  <my-aaa></my-aaa>
 </div>
 <script>
    //另外一種寫法,全局
  Vue.component('my-aaa',{
   template:'<strong>好</strong>'
  });
  var vm=new Vue({
   el:'#box'
  });
 </script>
</body>
</html>

組件里面放數(shù)據(jù):

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
</head>
<body>
 <div id="box">
  <aaa></aaa>
 </div>
 <script>
  var Aaa=Vue.extend({
   //組件里面放數(shù)據(jù):data必須是函數(shù)的形式,函數(shù)必須返回一個(gè)對(duì)象(json)
   data(){
    return {
     msg:'我是標(biāo)題^^'
    };
   },
   methods:{
    change(){
     this.msg='changed'
    }
   },
   template:'<h3 @click="change">{{msg}}</h3>'
  });
  Vue.component('aaa',Aaa);//放在這里是全局
  var vm=new Vue({
   el:'#box',
   data:{
    bSign:true
   }
  });
 </script>
</body>
</html>

2. 局部組件

放到某個(gè)組件內(nèi)部

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
 <style>
 </style>
</head>
<body>
 <div id="box">
  <aaa></aaa>
  <br/>
  <br/>
  <my-aaa></my-aaa>
 </div>
 <script>
  var Aaa=Vue.extend({
   template:'<h3>{{msg}}</h3>',
   data(){
    return {
     msg:'ddddd'
    }
   }
  });
  var vm=new Vue({
   el:'#box',
   data:{
    bSign:true
   },
   components:{ //局部組件
    aaa:Aaa,
    'my-aaa':Aaa//這里的my-aaa需要用引號(hào)
   }
  });
 </script>
</body>
</html>

另外一種寫法,局部

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
 <style>
 </style>
</head>
<body>
 <div id="box">
  <my-aaa></my-aaa>
 </div>
 <script>
  var vm=new Vue({
   el:'#box',
   components:{
    'my-aaa':{
     data(){
      return {
       msg:'welcome vue'
      }
     },
     methods:{
      change(){
       this.msg='changed';
      }
     },
     template:'<h2 @click="change">標(biāo)題2->{{msg}}</h2>'
    }
   }
  });
 </script>
</body>
</html>

二、配合模板

配合模板:

1. template:'<h2 @click="change">標(biāo)題2->{{msg}}</h2>'

2. 單獨(dú)放到某個(gè)地方

a).

<script type="x-template" id="aaa">
 <h2 @click="change">標(biāo)題2->{{msg}}</h2>
</script>

b).

<template id="aaa">
 <h1>標(biāo)題1</h1>
 <ul>
  <li v-for="val in arr">
   {{val}}
  </li>
 </ul>
</template>

方法一:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
 <style>
 </style>
</head>
<body>
 <div id="box">
  <my-aaa></my-aaa>
 </div>
 <script type="x-template" id="aaa">
  <h2 @click="change">標(biāo)題2->{{msg}}</h2>
  <ul>
   <li>1111</li>
   <li>222</li>
   <li>3333</li>
   <li>1111</li>
  </ul>
 </script>
 <script>
  var vm=new Vue({
   el:'#box',
   components:{
    'my-aaa':{
     data(){
      return {
       msg:'welcome vue'
      }
     },
     methods:{
      change(){
       this.msg='changed';
      }
     },
     template:'#aaa'
    }
   }
  });
 </script>
</body>
</html>

方法二:

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
 <style>
 </style>
</head>
<body>
 <div id="box">
  <my-aaa></my-aaa>
 </div>
 <template id="aaa">
  <h1 @click="change">{{msg}}</h1>
  <ul>
   <li v-for="val in arr">
    {{val}}
   </li>
  </ul>
 </template>
 <script>
  var vm=new Vue({
   el:'#box',
   components:{
    'my-aaa':{
     data(){
      return {
       msg:'welcome vue',
       arr:['apple','banana','orange']
      }
     },
     methods:{
      change(){
       this.msg='changed title';
      }
     },
     template:'#aaa'
    }
   }
  });
 </script>
</body>
</html>

三、動(dòng)態(tài)組件

動(dòng)態(tài)組件:

<component :is="組件名稱"></component>

<!DOCTYPE html>
<html lang="en">
<head>
 <meta charset="UTF-8">
 <title>Document</title>
 <script src="https://cdn.bootcss.com/vue/2.4.4/vue.min.js"></script>
 <style>
 </style>
</head>
<body>
 <div id="box">
  <input type="button" @click="a='aaa'" value="aaa組件">
  <input type="button" @click="a='bbb'" value="bbb組件">
  <component :is="a"></component>
  <!--<component :is="組件名稱"></component>-->
 </div>
 <script>
  var vm=new Vue({
   el:'#box',
   data:{
    a:'aaa'
   },
   components:{
    'aaa':{
     template:'<h2>我是aaa組件</h2>'
    },
    'bbb':{
     template:'<h2>我是bbb組件</h2>'
    }
   }
  });
 </script>
</body>
</html>

運(yùn)行效果:

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

希望本文所述對(duì)大家vue.js程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 在vue中如何引入外部的css文件

    在vue中如何引入外部的css文件

    這篇文章主要介紹了在vue中如何引入外部的css文件,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • 在Vue2中v-model和.sync區(qū)別解析

    在Vue2中v-model和.sync區(qū)別解析

    在vue2中提供了.sync修飾符,但是在vue3中不再支持.sync,取而代之的是v-model,本文給大家介紹在Vue2中v-model和.sync區(qū)別,感興趣的朋友一起看看吧
    2023-10-10
  • vue3.0實(shí)踐之寫tsx語(yǔ)法實(shí)例

    vue3.0實(shí)踐之寫tsx語(yǔ)法實(shí)例

    很久不寫博客了,最近在使用ts和tsx開發(fā)vue類項(xiàng)目,網(wǎng)上資料比較少,順便記錄一下方便同樣開發(fā)的人互相學(xué)習(xí)共同進(jìn)步,下面這篇文章主要給大家介紹了關(guān)于vue3.0實(shí)踐之寫tsx語(yǔ)法的相關(guān)資料,需要的朋友可以參考下
    2022-07-07
  • vue-cli5搭建vue項(xiàng)目的實(shí)現(xiàn)步驟

    vue-cli5搭建vue項(xiàng)目的實(shí)現(xiàn)步驟

    本文主要介紹了vue-cli5搭建vue項(xiàng)目的實(shí)現(xiàn)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-05-05
  • Vue-Router2.X多種路由實(shí)現(xiàn)方式總結(jié)

    Vue-Router2.X多種路由實(shí)現(xiàn)方式總結(jié)

    下面小編就為大家分享一篇Vue-Router2.X多種路由實(shí)現(xiàn)方式總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • vue后臺(tái)返回格式為二進(jìn)制流進(jìn)行文件的下載方式

    vue后臺(tái)返回格式為二進(jìn)制流進(jìn)行文件的下載方式

    這篇文章主要介紹了vue后臺(tái)返回格式為二進(jìn)制流進(jìn)行文件的下載方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • uni-app 使用編輯器創(chuàng)建vue3 項(xiàng)目并且運(yùn)行的操作方法

    uni-app 使用編輯器創(chuàng)建vue3 項(xiàng)目并且運(yùn)行的操作方法

    這篇文章主要介紹了uni-app 使用編輯器創(chuàng)建vue3 項(xiàng)目并且運(yùn)行的操作方法,目前uniapp 創(chuàng)建的vue3支持 vue3.0 -- 3.2版本 也就是說(shuō)setup語(yǔ)法糖也是支持的,需要的朋友可以參考下
    2023-01-01
  • 關(guān)于elementUI select控件綁定多個(gè)值(對(duì)象)

    關(guān)于elementUI select控件綁定多個(gè)值(對(duì)象)

    這篇文章主要介紹了關(guān)于elementUI select控件綁定多個(gè)值(對(duì)象),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • vue中nextTick用法實(shí)例

    vue中nextTick用法實(shí)例

    在本篇文章里小編給大家整理了關(guān)于vue中nextTick用法實(shí)例以及相關(guān)代碼內(nèi)容,需要的朋友們可以參考下。
    2019-09-09
  • vue項(xiàng)目中使用ueditor的實(shí)例講解

    vue項(xiàng)目中使用ueditor的實(shí)例講解

    下面小編就為大家分享一篇vue項(xiàng)目中使用ueditor的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03

最新評(píng)論

新兴县| 庆阳市| 沾化县| 海安县| 哈尔滨市| 临汾市| 容城县| 永州市| 青阳县| 万宁市| 曲松县| 肇源县| 大同县| 昔阳县| 台中市| 南汇区| 梅河口市| 光山县| 嘉鱼县| 凤台县| 安丘市| 繁峙县| 义乌市| 吉木萨尔县| 庆城县| 贵定县| 崇州市| 新巴尔虎左旗| 临朐县| 时尚| 万山特区| 喜德县| 芜湖县| 于田县| 樟树市| 彭阳县| 嘉定区| 文化| 靖江市| 白河县| 银川市|