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>//綁定模型,檢測(cè)輸入的格式 <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ù)組的長(zhǎng)度),因?yàn)関ue的是以數(shù)據(jù)驅(qū)動(dòng),以數(shù)據(jù)判斷,該不該渲染dom
addTime () {
this.timeArr.push('str')
},
minusTime () {
this.timeArr.shift('str')
}
4. 追加class , 場(chǎng)景 在循環(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)測(cè)這個(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)文章
Vue?Router路由hash模式與history模式詳細(xì)介紹
Vue?Router是Vue.js官方的路由管理器。它和Vue.js的核心深度集成,讓構(gòu)建單頁面應(yīng)用變得易如反掌。路由實(shí)際上就是可以理解為指向,就是我在頁面上點(diǎn)擊一個(gè)按鈕需要跳轉(zhuǎn)到對(duì)應(yīng)的頁面,這就是路由跳轉(zhuǎn)2022-08-08
vue實(shí)現(xiàn)檢測(cè)敏感詞過濾組件的多種思路
這篇文章主要介紹了vue編寫檢測(cè)敏感詞匯組件的多種思路,幫助大家更好的理解和學(xué)習(xí)使用vue框架,感興趣的朋友可以了解下2021-04-04
Vue使用正則校驗(yàn)文本框?yàn)檎麛?shù)
這篇文章主要介紹了Vue使用正則校驗(yàn)文本框?yàn)檎麛?shù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-10-10
Vue3 構(gòu)建 Web Components使用詳解
這篇文章主要為大家介紹了Vue3 構(gòu)建 Web Components使用詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-09-09
使用this.$nextTick()獲取不到數(shù)據(jù)更新后的this.$refs.xxx.及場(chǎng)景分析
今天遇到了這樣一個(gè)場(chǎng)景,在數(shù)據(jù)更新之后,使用this.$nextTick(()=>{console.log(this.$refs.xxx)}) 獲取不到改dom,但是用setTimeout能夠獲取到,在此記錄一下,感興趣的朋友跟隨小編一起看看吧2023-02-02
vue中ref標(biāo)簽屬性和$ref的關(guān)系解讀
這篇文章主要介紹了vue中ref標(biāo)簽屬性和$ref的關(guān)系,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
vue-router 中router-view不能渲染的解決方法
本篇文章主要結(jié)合了vue-router 中router-view不能渲染的解決方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05

