欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

vue封裝可復用組件confirm,并綁定在vue原型上的示例

 更新時間:2019年10月31日 09:06:28   作者:Amy o-O  
今天小編就為大家分享一篇vue封裝可復用組件confirm,并綁定在vue原型上的示例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

如下所示:

首先我們需要創(chuàng)建 confirm.vue , confirm.js這兩個文件,一個實現(xiàn)dom結(jié)構(gòu),一個實現(xiàn)相關邏輯

confirm.vue

<template>
 <div class="confirm" v-if="isShow">
  <div class="con_box" >
   <div class="context">
    <h6>{{text.type}}</h6>
    <p>{{text.msg}}</p>
    <div class="btn">
     <span @click="close()" v-if="text.btn.no">{{text.btn.no}}</span>
     <span @click="ok()" v-if="text.btn.ok">{{text.btn.ok}}</span>
    </div>
   </div>
  </div>
 </div>
</template>
<script>
export default {
 data(){
  return{
   isShow:true,
   text:{
    type:'提示',    
    msg:'確定刪除此條信息?',
    btn:{
     ok:'確定',
     no:'取消'
    },
   }
  }
 },
 methods:{
  close(){
   console.log('關閉');
  },
  ok(){
   console.log('確定')
  }
 }
}
</script>
<style scoped>
.confirm{background-color:rgba(0, 0, 0, 0.6);width: 100%;height: 100%; position: fixed;top: 0;}
.con_box{width: 75%;height: 22%;background-color: white;position: absolute;top: 0;bottom: 0;left: 0;right: 0;margin: auto;border-radius: 5px;}
.context{padding: 10px;}
.context h6{font-size: 24px;padding: 15px;}
.context p{font-size: 20px;padding: 35px 15px;text-overflow: ellipsis;overflow: hidden;white-space: nowrap;}
.btn{padding: 15px;text-align: right;}
.btn span{padding: 10px 35px; color: white;border-radius: 5px;}
.btn span:nth-child(1){background-color: #f1f1f1;color: rgb(106, 223, 223);}
.btn span:nth-child(2){background-color: rgb(106, 223, 223);}
</style>

confirm.js

import Vue from 'vue';
import confirm from './confirm.vue';

let confirmConstructor = Vue.extend(confirm);

let theConfirm = function (text) {
 return new Promise((res, rej) => { //promise封裝,ok執(zhí)行resolve,no執(zhí)行rejectlet
  let confirmDom = new confirmConstructor({ 
  el: document.createElement('div')
  })
  document.body.appendChild(confirmDom.$el); //new一個對象,然后插入body里面

  confirmDom.text = text; //為了使confirm的擴展性更強,這個采用對象的方式傳入,所有的字段都可以根據(jù)需求自定義
  confirmDom.ok = function () {
  res()
  confirmDom.isShow = false
  }
  confirmDom.close = function () {
  rej()
  confirmDom.isShow = false
  }

 })
 }

 export default theConfirm; 
 
 //暴露出去,別忘記掛載到vue的原型上 
 // => 在main.js里面先引入 import theConfirm from './components/confirm/confirm.js'
 // => 再掛載 Vue.prototype.$confirm = theConfirm;
 //在需要的地方直接用以下方法調(diào)用即可:
 // this.$confirm({
 //  type:'提示',
 //  msg:'是否刪除這條信息?',
 //  btn:{
 //   ok:'yes',
 //   no:'no'
 //  }
 // }).then(() => {
 //  console.log('ok')
 // })
 // .catch(() => {
 //  console.log('no')
 // })

main.js

// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'

//這里就是對組件的綁定
import theConfirm from './components/confirm/confirm.js'
Vue.prototype.$confirm = theConfirm;

Vue.config.productionTip = false
/* eslint-disable no-new */
new Vue({
 el: '#app',
 router,
 components: { App },
 template: '<App/>'
})

helloworld.vue

<template>
 <div class="hello">
  <span @click="handMe()">點擊一下</span>
 </div>
</template>

<script>
export default {
 name: 'HelloWorld',
 data () {
 return {
  
 }
 },
 methods:{
 handMe(){
 this.$confirm({
 type:'提示',
 msg:'是否刪除這條信息?',
 btn:{
  ok:'yes',
  no:'no'
 }
 }).then(() => {
 console.log('ok')
 })
 .catch(() => {
 console.log('no')
 })
 }
 }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
span{font-size: 24px;}
</style>

以上這篇vue封裝可復用組件confirm,并綁定在vue原型上的示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關文章

  • Vuejs入門教程之Vue生命周期,數(shù)據(jù),手動掛載,指令,過濾器

    Vuejs入門教程之Vue生命周期,數(shù)據(jù),手動掛載,指令,過濾器

    本篇文章主要介紹了Vuejs入門教程之Vue生命周期,數(shù)據(jù),手動掛載,指令,過濾器的相關知識。具有很好的參考價值。下面跟著小編一起來看下吧
    2017-04-04
  • 結(jié)合Vue控制字符和字節(jié)的顯示個數(shù)的示例

    結(jié)合Vue控制字符和字節(jié)的顯示個數(shù)的示例

    這篇文章主要介紹了結(jié)合Vue控制字符和字節(jié)的顯示個數(shù)的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-05-05
  • vue3中v-model的用法詳解

    vue3中v-model的用法詳解

    vue 提供了很多自定義指令,大大方便了我們的開發(fā),其中最常用的也就是 v-model,他可以在組件上使用以實現(xiàn)雙向綁定。它可以綁定多種數(shù)據(jù)結(jié)構(gòu), 今天總結(jié)一下用法
    2023-04-04
  • vue請求按順序執(zhí)行的示例詳解

    vue請求按順序執(zhí)行的示例詳解

    我們有時候會碰到這種情況,需要連續(xù)發(fā)送兩個請求,第二個請求需要用第一個請求的某個返回值作為參數(shù)來作為第二個請求的請求參數(shù),這篇文章主要介紹了vue請求如何按順序執(zhí)行,需要的朋友可以參考下
    2023-12-12
  • Vue-element-admin平臺側(cè)邊欄收縮控制問題

    Vue-element-admin平臺側(cè)邊欄收縮控制問題

    這篇文章主要介紹了Vue-element-admin平臺側(cè)邊欄收縮控制問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-10-10
  • vite+vue3代碼風格校驗及格式化方式

    vite+vue3代碼風格校驗及格式化方式

    這篇文章主要介紹了vite+vue3代碼風格校驗及格式化方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03
  • Vue 3.0中Treeshaking特性及作用

    Vue 3.0中Treeshaking特性及作用

    Tree shaking 是一種通過清除多余代碼方式來優(yōu)化項目打包體積的技術,就是在保持代碼運行結(jié)果不變的前提下,去除無用的代碼,本文給大家介紹Vue 3.0中Treeshaking特性是什么,感興趣的朋友一起看看吧
    2023-10-10
  • vue跳轉(zhuǎn)同一個路由參數(shù)不同的問題

    vue跳轉(zhuǎn)同一個路由參數(shù)不同的問題

    這篇文章主要介紹了vue跳轉(zhuǎn)同一個路由參數(shù)不同的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • vue源碼解讀子節(jié)點優(yōu)化更新

    vue源碼解讀子節(jié)點優(yōu)化更新

    這篇文章主要為大家介紹了vue源碼解讀子節(jié)點優(yōu)化更新示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-08-08
  • 詳解vue 單頁應用(spa)前端路由實現(xiàn)原理

    詳解vue 單頁應用(spa)前端路由實現(xiàn)原理

    這篇文章主要介紹了詳解vue 單頁應用(spa)前端路由實現(xiàn)原理,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-04-04

最新評論