vue的常用組件操作方法應用分析
項目技術(shù):
webpack + vue + element + axois (vue-resource) + less-loader+ ...
vue的操作的方法案例:
1.數(shù)組數(shù)據(jù)還未獲取到,做出預加載的動畫
<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>// 實際顯示的內(nèi)容-跑馬燈
<div v-if="!movieArr.length" class="ticket-index-movie-loading">
<span class="el-icon-loading "></span>
</div>// 當 movirArr的數(shù)組為空的時候,做出的預加載 loading
</el-carousel>
2. 按鈕狀態(tài)的判斷,按鈕能不能點的問題
<p v-if="!multipleSelection.length"> <el-button type="success" round disabled>導出</el-button> </p><!-- 不能點, 判斷數(shù)組為空 --> <p v-else> <el-button type="success" round >導出</el-button> </p><!-- 可以點, 判斷數(shù)組為不為空 -->
3.像jquery 一樣,追加dom (vue 是以數(shù)據(jù)為導向的,應該擺脫jquery的 dom的繁雜操作)
<el-form-item label="時間" 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="時間" 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:
相當于jq 中的 dom 字符串
timeInputString: '<el-input v-model="ruleForm.name"></el-input><span class="el-icon-minus"></span>'
原生的js 往數(shù)組里壓入和彈出 數(shù)據(jù)(抓數(shù)組的長度),因為vue的是以數(shù)據(jù)驅(qū)動,以數(shù)據(jù)判斷,該不該渲染dom
addTime () {
this.timeArr.push('str')
},
minusTime () {
this.timeArr.shift('str')
}
4. 追加class , 場景 在循環(huán)某個列表時候,某個列表有class,綁定一個方法,可以支持穿參數(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)測這個屬性,給這個屬性綁定方法 receiveTitle,方法傳參數(shù),這個參數(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é)套路:父組件將變量傳到子組件,需要在子組件標簽上綁定這個變量,然后子組件就可以在props 里接受這個變量
7.錯誤路由的處理,重定向, 在router里添加一個路由信息
{
path: '*',
redirect: '/'
}
這里是重新定向到首頁,也可以單獨做一個 404頁面,重定向到這個頁面
編程式導航里面,
router.push({ path: 'login-regist' }) // 如果這樣寫的話,會尋找路由最近的 / 然后在后面直接拼接login-regist;
為了防止在多級嵌套路由里面出現(xiàn)bug ,應該寫全路由的全部信息,包括 /
router.push({ path: '/login-regist' })
8. dom 里拼接css
<div class="img" :style="{background: 'url(' + item.logoFileURL + ')'}"></div>
9. 監(jiān)聽滾動事件
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="請輸入內(nèi)容" ></el-input>
總結(jié)
以上所述是小編給大家介紹的vue的常用組件操作方法應用分析,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
vue新建環(huán)境變量以及網(wǎng)絡請求工具axios的二次封裝詳解
這篇文章主要為大家介紹了vue新建環(huán)境變量以及網(wǎng)絡請求工具axios的二次封裝詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-06-06
vue el-table字段點擊出現(xiàn)el-input輸入框,失焦保存方式
這篇文章主要介紹了vue el-table字段點擊出現(xiàn)el-input輸入框,失焦保存方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-02-02
Vue動態(tài)權(quán)限登錄實現(xiàn)(基于路由與角色)
很多應用都會需要對不同的用戶進行權(quán)限控制,本文主要介紹了Vue動態(tài)權(quán)限登錄實現(xiàn)(基于路由與角色),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2023-05-05

