Vue.js學習筆記(7)組件詳解

來源:互聯網
上載者:User

標籤:

在這篇文章之前小穎分享過小穎自己寫的組件:Vue.js學習筆記(5)tabs組件和Tree升級版(實現省市多級聯動)

先給大家看下小穎寫了一個簡單的組件樣本:

組件:

<template><div class=‘content‘ v-if=‘showFlag‘><input type="text" v-bind:style=‘{ width:compwidth+"px"}‘ v-model=‘compvalue‘ @keyup=‘myFun‘ v-el:getvalue><label class=‘example-label‘>觀察參數"compvalue"的變化:</label>{{compvalue}}<button class=‘btn btn-danger‘ @click=‘compfun‘>確定</button></div></template><script>export default {  // 從父組件接收收據  props:{    compvalue:{      type:String,//類型(原生構造器:String, Number, Boolean, Function, Object, Array),如果綁定類型不對將拋出一條警告      required: true, //是否是必須項      twoWay:true,//指定這個 prop 為雙向繫結,如果沒有  ‘sync‘ 將拋出一條警告      default:‘‘,//預設值    },    compwidth:{      coerce: function (val) {        return val + ‘‘ // 將值轉換為字串      }    },    compfun:{      type:Function,      required:true    }  },  ready: function() {},  computed:{},//計算屬性  methods: {//組件自身的方法    myFun:function(){      alert( this.$els.getvalue.value);    }  },  data() {//綁定資料    return {      showFlag:true,    }  }}</script>

調用組件:

<template><div class=‘example-content‘><compexample :compvalue.sync=‘values‘ :compfun=‘compFun‘></compexample></div></template><script>import compexample from ‘./componentExample.vue‘//引入組件export default {  components: {    compexample  },  ready: function() {  },  methods: {    compFun:function(){      alert(‘喵嘞個咪‘);    }  },  data() {    return {      values:‘hello‘    }  }}</script>

在小穎寫的組件中,小穎把建立組件時,組件的大部分屬性都加了相應注釋,大家看了要是還有什麼疑問,可以留言哦.

下面看寫調用組件後的吧:

 

Vue.js學習筆記(7)組件詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.