vue的常用組件操作方法應(yīng)用分析
項(xiàng)目技術(shù):
webpack + vue + element + axois (vue-resource) + less-loader+ ...
vue的操作的方法案例:
1.數(shù)組數(shù)據(jù)還未獲取到,做出預(yù)加載的動(dòng)畫
<el-carousel :interval="3000" type="card" height="200px" class="common-mt-md"> <el-carousel-item v-for="item in movieArr" :key="item.id" class="text-center"> <img v-bind:src="item.images.small" alt="電影封面" class="ticket-index-movie-img"> </el-carousel-item>// 實(shí)際顯示的內(nèi)容-跑馬燈 <div v-if="!movieArr.length" class="ticket-index-movie-loading"> <span class="el-icon-loading "></span> </div>// 當(dāng) movirArr的數(shù)組為空的時(shí)候,做出的預(yù)加載 loading </el-carousel>
2. 按鈕狀態(tài)的判斷,按鈕能不能點(diǎn)的問題
<p v-if="!multipleSelection.length"> <el-button type="success" round disabled>導(dǎo)出</el-button> </p><!-- 不能點(diǎn), 判斷數(shù)組為空 --> <p v-else> <el-button type="success" round >導(dǎo)出</el-button> </p><!-- 可以點(diǎn), 判斷數(shù)組為不為空 -->
3.像jquery 一樣,追加dom (vue 是以數(shù)據(jù)為導(dǎo)向的,應(yīng)該擺脫jquery的 dom的繁雜操作)
<el-form-item label="時(shí)間" prop="name"> <el-input v-model="ruleForm.name"></el-input>//綁定模型,檢測輸入的格式 <span class="el-icon-plus ticket-manage-timeinput" @click="addTime(this)"></span>//綁定方法,增加dom的操作 </el-form-item> <el-form-item label="時(shí)間" prop="name" v-for="item in timeArr" :key='item.id'> //timeArr數(shù)組與數(shù)據(jù)就渲染下面的dom,沒有就不顯示 <el-input v-model="ruleForm.name"></el-input> <span class="el-icon-minus ticket-manage-timeinput" @click="minusTime(this)"></span> </el-form-item>
js:
相當(dāng)于jq 中的 dom 字符串
timeInputString: '<el-input v-model="ruleForm.name"></el-input><span class="el-icon-minus"></span>'
原生的js 往數(shù)組里壓入和彈出 數(shù)據(jù)(抓數(shù)組的長度),因?yàn)関ue的是以數(shù)據(jù)驅(qū)動(dòng),以數(shù)據(jù)判斷,該不該渲染dom
addTime () { this.timeArr.push('str') }, minusTime () { this.timeArr.shift('str') }
4. 追加class , 場景 在循環(huán)某個(gè)列表時(shí)候,某個(gè)列表有class,綁定一個(gè)方法,可以支持穿參數(shù)
dom
<li v-for="section in item.sections" :key='section.id' @click="hideParMask" :class="getSectionId(section.id)"> <router-link :to="{ name: 'learning', params: { sectionId: section.id}, query: { courseId: courseId}}" > <span>{{item.orderInCourse}}.{{section.sectionNumber}}</span> <span>{{section.name}}</span> </router-link> </li>
js
getSectionId (sectionId) { return { active: this.$route.params.sectionId === sectionId, } }
5.子->父組件的通信,vue.$emit vue.on
子組件:
getSectionId (sectionId) { return { active: this.$route.params.sectionId === sectionId, } }
父組件:
dom
<v-child :courseId="courseId" v-on:receiveTitle="receiveTitle"></v-child>
js
methods: { receiveTitle (name) { this.titleName = name; // titleName 就是 **@課程 } }
總結(jié)套路: 子組件使用函數(shù)(事件)給父組件傳遞 receiveTitle 屬性,然后父組件監(jiān)測這個(gè)屬性,給這個(gè)屬性綁定方法 receiveTitle,方法傳參數(shù),這個(gè)參數(shù)就是 要傳遞的 值
6.父-> 子
父組件:
dom:
<course-tab :courseList = courseList ></course-tab>
js:
courseList().then(res => { this.courseList = res.data.courses; }).catch( err => { console.log(err) });
子組件:
props: { courseList: { type: Array } }
總結(jié)套路:父組件將變量傳到子組件,需要在子組件標(biāo)簽上綁定這個(gè)變量,然后子組件就可以在props 里接受這個(gè)變量
7.錯(cuò)誤路由的處理,重定向, 在router里添加一個(gè)路由信息
{ path: '*', redirect: '/' }
這里是重新定向到首頁,也可以單獨(dú)做一個(gè) 404頁面,重定向到這個(gè)頁面
編程式導(dǎo)航里面,
router.push({ path: 'login-regist' }) // 如果這樣寫的話,會(huì)尋找路由最近的 / 然后在后面直接拼接login-regist; 為了防止在多級(jí)嵌套路由里面出現(xiàn)bug ,應(yīng)該寫全路由的全部信息,包括 / router.push({ path: '/login-regist' })
8. dom 里拼接css
<div class="img" :style="{background: 'url(' + item.logoFileURL + ')'}"></div>
9. 監(jiān)聽滾動(dòng)事件
data () { return { scrolled: false, show: true } }, methods: { handleScroll () { this.scrolled = window.scrollY > 0; if (this.scrolled) { this.show = false; } } }, mounted () { window.addEventListener('scroll', this.handleScroll); }
10.監(jiān)聽輸入框輸入值的變化
@input="search",
監(jiān)聽 element-UI 的<el-input
的方法,
<el-input v-model="input" @keyup.enter.native="add" placeholder="請(qǐng)輸入內(nèi)容" ></el-input>
總結(jié)
以上所述是小編給大家介紹的vue的常用組件操作方法應(yīng)用分析,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
nuxt實(shí)現(xiàn)封裝axios并且獲取token
這篇文章主要介紹了nuxt實(shí)現(xiàn)封裝axios并且獲取token,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-10-10vue新建環(huán)境變量以及網(wǎng)絡(luò)請(qǐng)求工具axios的二次封裝詳解
這篇文章主要為大家介紹了vue新建環(huán)境變量以及網(wǎng)絡(luò)請(qǐng)求工具axios的二次封裝詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06vue el-table字段點(diǎn)擊出現(xiàn)el-input輸入框,失焦保存方式
這篇文章主要介紹了vue el-table字段點(diǎn)擊出現(xiàn)el-input輸入框,失焦保存方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02Vue動(dòng)態(tài)權(quán)限登錄實(shí)現(xiàn)(基于路由與角色)
很多應(yīng)用都會(huì)需要對(duì)不同的用戶進(jìn)行權(quán)限控制,本文主要介紹了Vue動(dòng)態(tài)權(quán)限登錄實(shí)現(xiàn)(基于路由與角色),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-05-05vue中實(shí)現(xiàn)可編輯table及其中加入下拉選項(xiàng)
這篇文章主要介紹了vue中實(shí)現(xiàn)可編輯table及其中加入下拉選項(xiàng),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08