<span id="mktg5"></span>

<i id="mktg5"><meter id="mktg5"></meter></i>

        <label id="mktg5"><meter id="mktg5"></meter></label>
        最新文章專(zhuān)題視頻專(zhuān)題問(wèn)答1問(wèn)答10問(wèn)答100問(wèn)答1000問(wèn)答2000關(guān)鍵字專(zhuān)題1關(guān)鍵字專(zhuān)題50關(guān)鍵字專(zhuān)題500關(guān)鍵字專(zhuān)題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專(zhuān)題關(guān)鍵字專(zhuān)題tag2tag3文章專(zhuān)題文章專(zhuān)題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專(zhuān)題3
        問(wèn)答文章1 問(wèn)答文章501 問(wèn)答文章1001 問(wèn)答文章1501 問(wèn)答文章2001 問(wèn)答文章2501 問(wèn)答文章3001 問(wèn)答文章3501 問(wèn)答文章4001 問(wèn)答文章4501 問(wèn)答文章5001 問(wèn)答文章5501 問(wèn)答文章6001 問(wèn)答文章6501 問(wèn)答文章7001 問(wèn)答文章7501 問(wèn)答文章8001 問(wèn)答文章8501 問(wèn)答文章9001 問(wèn)答文章9501
        當(dāng)前位置: 首頁(yè) - 科技 - 知識(shí)百科 - 正文

        vue2.0 自定義組件的方法(vue組件的封裝)

        來(lái)源:懂視網(wǎng) 責(zé)編:小采 時(shí)間:2020-11-27 22:13:17
        文檔

        vue2.0 自定義組件的方法(vue組件的封裝)

        vue2.0 自定義組件的方法(vue組件的封裝):一、前言 之前的博客聊過(guò) vue2.0和react的技術(shù)選型;聊過(guò)vue的axios封裝和vuex使用。今天簡(jiǎn)單聊聊 vue 組件的封裝。 vue 的ui框架現(xiàn)在是很多的,但是鑒于移動(dòng)設(shè)備的復(fù)雜性,兼容性問(wèn)題突出。像 Mint-UI 等說(shuō)實(shí)話很不錯(cuò)了,但是坑也是不少,而且很多功能也是僅
        推薦度:
        導(dǎo)讀vue2.0 自定義組件的方法(vue組件的封裝):一、前言 之前的博客聊過(guò) vue2.0和react的技術(shù)選型;聊過(guò)vue的axios封裝和vuex使用。今天簡(jiǎn)單聊聊 vue 組件的封裝。 vue 的ui框架現(xiàn)在是很多的,但是鑒于移動(dòng)設(shè)備的復(fù)雜性,兼容性問(wèn)題突出。像 Mint-UI 等說(shuō)實(shí)話很不錯(cuò)了,但是坑也是不少,而且很多功能也是僅

        一、前言

        之前的博客聊過(guò) vue2.0和react的技術(shù)選型;聊過(guò)vue的axios封裝和vuex使用。今天簡(jiǎn)單聊聊 vue 組件的封裝。

        vue 的ui框架現(xiàn)在是很多的,但是鑒于移動(dòng)設(shè)備的復(fù)雜性,兼容性問(wèn)題突出。像 Mint-UI 等說(shuō)實(shí)話很不錯(cuò)了,但是坑也是不少,而且很多功能也是僅憑這些實(shí)現(xiàn)不了,這需要我們?nèi)シ庋b自己的可復(fù)用組件

        二、封裝組件的步驟

        1. 建立組件的模板,先把架子搭起來(lái),寫(xiě)寫(xiě)樣式,考慮你的組件的基本邏輯。

        os:思考1小時(shí),碼碼10分鐘,程序猿的準(zhǔn)則。

        2. 準(zhǔn)備組件的好數(shù)據(jù)輸入。即分析好邏輯,定好 props 里面的數(shù)據(jù)、類(lèi)型。(后面詳解)

        3.準(zhǔn)備組件的好數(shù)據(jù)輸出。即根據(jù)組件邏輯,做好要暴露出來(lái)的方法。(后面詳解)

        4.封裝完畢了,直接調(diào)用即可。

        os: 代碼可以不看,結(jié)論在文章最后

        接下來(lái)以一個(gè)很簡(jiǎn)單的例子具體說(shuō)明一下

        現(xiàn)在先看一下demo的效果圖

         

        三、 demo代碼

        父組件:

        <template>
         <section class="f-mainPage">
         <!--selectFunc 選擇完成的回調(diào) searchList 下拉列表的數(shù)據(jù)-->
         <search @selectFunc="selectFunc" :searchList="searchList" :selectValue="selectValue"></search>
         </section>
        </template>
        
        <script type="text/ecmascript-6">
         import Search from '../vuePlugin/search'
         export default {
         data() {
         return {
         searchList: ['草船借箭', '大富翁', '測(cè)試數(shù)據(jù)'],
         // 直接通過(guò)props傳遞對(duì)象 修改,挺便捷的,但是不規(guī)范
         selectValue: {
         data: '1'
         },
         // 通過(guò)emit修改,規(guī)范寫(xiě)法
         selectValue2: ''
         }
         },
         mounted() {},
         methods: {
         pageGo(path) {
         this.$router.push('/' + path)
         },
         selectFunc(value) {
         this.selectValue2 = value
         console.log(this.selectValue)
         console.log(this.selectValue2)
         }
         },
         components: {
         Search
         }
         }
        </script>
        
        <style lang="scss" scoped>
        .f-mainPage{
         width: 100%;
         .g-banner{
         width: 100%;
         background-image: url(../../../static/main_bg.png);
         background-repeat: no-repeat;
         background-size: 100% 100%;
         position: relative;
         overflow: hidden;
         color: white;
         text-align: center;
         p:nth-child(1) {
         margin: 10px auto 0px auto;
         font-size: 1.3rem;
         }
         .f-banscri {
         margin: 15px auto 8px auto;
         font-size: 0.95rem;
         }
         .f-moneyMax{
         margin: 5px auto 0px auto;
         font-size: 2.4rem;
         }
         .f-returnCash{
         width: 120px;
         height: 35px;
         text-align: center;
         line-height: 35px;
         background-color: white;
         color: #169BD5;
         display: inline-block;
         border-radius: 5px;
         font-size: 1rem;
         margin-top: 35px;
         position: relative;
         .f-mmmbd{
         position: absolute;
         width: 100%;
         height: 100%;
         background-color: transparent;
         top: 0;
         left: 0;
         }
         }
         }
         .g-cashInfor{
         width: 100%;
         text-align: center;
         display: flex;
         justify-content: space-between;
         div{
         width: 50%;
         height: 60px;
         line-height: 60px;
         box-sizing: border-box;
         }
         div:nth-child(1){
         border-bottom: 1px solid #878787;
         border-right: 1px solid #878787;
         }
         div:nth-child(2){
         border-bottom: 1px solid #878787;
         }
        }
         .g-operate{
         width: 100%;
         height: auto;
         overflow: hidden;
         ul{
         list-style: none;
         padding: 0;
         margin: 0;
         font-size: 1.05rem;
         li{
         height: 60px;
         line-height: 60px;
         padding-left: 25px;
         position: relative;
         span{
         width: 20px;
         height: 20px;
         position: absolute;
         top: 20px;
         right: 20px; 
         background-image: url(../../../static/go.png);
         background-repeat: no-repeat;
         background-size: 100% 100%;
         }
         }
         }
         .f-goodNews{
         width: 340px;
         height: 144.5px;
         margin: 20px auto 30px auto;
         text-align: center;
         background-image: url(../../../static/banner.png);
         background-repeat: no-repeat;
         background-size: 100% 100%;
         }
         }
        }
        </style>

        子組件:

        <template>
         <div class="searchZJ">
         <div class="f-search">
         <div class="f-searchIn" v-bind:class="{searchInFous: this.fousFlag}">{{this.searchValue}}<span v-bind:class="{searchActive: this.searchFlag}" v-on:click="searchDown"></span></div>
         <div class="f-searchXl" v-if="this.dataHas" v-bind:style="{height:this.searchFous, border:this.searchBorder}">
         <div v-for="item in searchList" v-on:click="choseValue(item)">{{item}}</div>
         </div>
         <div class="f-searchXl" v-else >
         <div>暫無(wú)數(shù)據(jù)</div>
         </div>
         </div>
         </div>
        </template>
        <script type="text/ecmascript-6">
         export default {
         data() {
         return {
         data: [],
         dataHas: true,
         searchFlag: false,
         searchFous: '0',
         fousFlag: false,
         searchValue: '',
         searchBorder: 'none'
         }
         },
         props: {
         searchList: Array,
         selectValue: Object
         },
         mounted() {
         this.data = this.searchList
         },
         methods: {
         searchDown() {
         this.searchFlag === false ? this.searchFlag = true : this.searchFlag = false
         this.searchFous === '0' ? this.searchFous = 'auto' : this.searchFous = '0'
         this.searchBorder === 'none' ? this.searchBorder = '1px solid #D9D9D9' : this.searchBorder = 'none'
         this.fousFlag === false ? this.fousFlag = true : this.fousFlag = false
         },
         choseValue(value) {
         this.searchValue = value
         this.searchDown()
         this.selectValue.data = '我被修改了'
         this.$emit('selectFunc', value)
         }
         }
         }
        </script>
        
        <style scoped lang="stylus" rel="stylesheet/stylus">
         .f-search{
         width: 250px;
         height: auto;
         position: relative;
         margin-left: 20px;
         box-sizing: border-box;
         }
         .f-searchIn{
         width: 250px;
         height: 35px;
         line-height: 35px;
         font-size: 0.95rem;
         border-radius: 5px;
         overflow: hidden;
         position: relative;
         background-color: white;
         box-shadow: none;
         box-sizing: border-box;
         color: #000000;
         padding-left: 10px;
         border: 1px solid #A3A3A3;
         }
         .searchInFous{
         border: 1px solid #57C4F6;
         box-shadow: 0px 0px 5px #57C4F6;
         }
         .f-searchIn > span{
         display: block;
         width: 28px;
         height: 28px;
         background-image: url(../../../static/upDown.png);
         background-size: 100% 100%;
         background-repeat: no-repeat;
         background-position: 0px -13px;
         position: absolute;
         top: 10px;
         right: 5px;
         }
         .f-searchIn .searchActive{
         background-position: 0px 12px;
         top: -2px;
         }
         .f-search .f-searchXl{
         position: absolute;
         width: 100%;
         height: auto;
         max-height: 220px;
         top: 41px;
         left: -1px;
         border-radius: 5px;
         /*border: 1px solid #D9D9D9;*/
         background-color: white;
         overflow-x: hidden;
         overflow-y: scroll;
         }
         .f-search .f-searchXl > div{
         height: 35px;
         line-height: 38px;
         color: #000000;
         padding-left: 25px;
         font-size: 0.92rem;
         }
         .f-search .f-searchXl > div:hover{
         background-color: #D5F1FD;
         }
        </style>

        四、代碼詳解

        1. 先說(shuō)一下 props

        我們?cè)诟附M件中需要將子組件需要的數(shù)據(jù)導(dǎo)入,用法如下:

        <search @selectFunc="selectFunc" :searchList="searchList" :selectValue="selectValue"></search>

        :searchList ="searchList"  就是我們的數(shù)據(jù),這個(gè)可以寫(xiě)多個(gè)。這里我傳輸了2個(gè)參數(shù)過(guò)去,主要是做數(shù)據(jù)修改的說(shuō)明。大家可以先忽略。

        在子組件中,我們的接收和使用方法如下:

        props: {
         searchList: Array,
         selectValue: Object
         },
        mounted() {
         this.data = this.searchList
         },

        我們?cè)?props 中接收數(shù)據(jù),注意props對(duì)象里面 鍵值 是對(duì)改數(shù)據(jù)的 數(shù)據(jù)類(lèi)型 的規(guī)定。做了規(guī)范,使用者就只能傳輸指定類(lèi)型的數(shù)據(jù),否則報(bào)警告

        二props對(duì)象中的數(shù)據(jù),我們可以直接在當(dāng)前組件中使用   this.searchList,可以直接使用。至于原理嘛,不懂的可以取腦補(bǔ)一下 js的原型 。os:這些基礎(chǔ),在這就不做詳述了

        聲明:本網(wǎng)頁(yè)內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問(wèn)題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

        文檔

        vue2.0 自定義組件的方法(vue組件的封裝)

        vue2.0 自定義組件的方法(vue組件的封裝):一、前言 之前的博客聊過(guò) vue2.0和react的技術(shù)選型;聊過(guò)vue的axios封裝和vuex使用。今天簡(jiǎn)單聊聊 vue 組件的封裝。 vue 的ui框架現(xiàn)在是很多的,但是鑒于移動(dòng)設(shè)備的復(fù)雜性,兼容性問(wèn)題突出。像 Mint-UI 等說(shuō)實(shí)話很不錯(cuò)了,但是坑也是不少,而且很多功能也是僅
        推薦度:
        標(biāo)簽: VUE 自定義 組件
        • 熱門(mén)焦點(diǎn)

        最新推薦

        猜你喜歡

        熱門(mén)推薦

        專(zhuān)題
        Top
        主站蜘蛛池模板: 四虎影视永久免费观看地址| 亚洲无人区一区二区三区| 久久精品国产亚洲av水果派| 中国一级特黄的片子免费| 99精品一区二区免费视频| 亚洲高清专区日韩精品| 一道本在线免费视频| 免费能直接在线观看黄的视频| 人人狠狠综合久久亚洲婷婷| 免费无码又爽又刺激高潮软件| 亚洲中文字幕久久精品无码喷水| 国产免费伦精品一区二区三区| 中国亚洲女人69内射少妇| 中文字幕在线视频免费| 亚洲成av人片天堂网| 日韩在线不卡免费视频一区| 亚洲伦理一区二区| 成人浮力影院免费看| 中文日韩亚洲欧美制服| 国产一区二区三区免费看| 一个人看的www视频免费在线观看 一个人看的免费观看日本视频www | 免费在线看黄的网站| 亚洲一区二区三区夜色 | **真实毛片免费观看| 亚洲www在线观看| 国产乱子伦片免费观看中字| 国产精品hd免费观看| 亚洲AV永久纯肉无码精品动漫| 97在线视频免费播放| 亚洲爆乳少妇无码激情| 国产亚洲色视频在线| 中文字幕在线观看免费视频| 亚洲综合欧美色五月俺也去| 性xxxxx大片免费视频| 亚洲AV无码专区在线亚| 国产精品四虎在线观看免费| 日本免费A级毛一片| 亚洲日本人成中文字幕| 免费在线观看中文字幕| 日本免费中文字幕| 亚洲av片在线观看|