1.用extend写全局组件
组件部分
<!-- * @Descripttion: * @Version: * @Date: 2021-09-07 14:12:10 --> <template> <transition name="component-fade" mode="out-in"> <div class="alert_box" v-show="show"> <div :class="{'alert_icon':true, 'iconfont':true,'iconjinggao':icon==0,'iconchenggong':icon==1,'iconcuowu1' :icon==2} "> </div> <div class="tit_alert" v-text="text"></div> </div> </transition> </template> <script> export default { data () { return { } } } </script> <style scoped lang="scss"> .flex_style{ display: flex; flex-direction: row; align-items: center; } .alert_box{ background: rgba(0,0,0,.7); border-radius: 10px; text-align: center; overflow: hidden; position: fixed; top: 50%; 385px; left: 50%; transform: translate(-50%,-50%); padding: 40px 10px; box-sizing: border-box; color: #fff; transition: all .5s; .alert_icon{ display: block; margin: 0 auto 30px; font-size: 80px; } .tit_alert{ font-size: 28px; margin-bottom: 0; } } .component-fade-enter-active{ transition: all .4s ease; } .component-fade-leave-active { transition: all .4s ease; } .component-fade-enter{ 185px; opacity: 0; } .component-fade-leave-to{ 185px; opacity: 0; } </style>
tips.js
import tips from '@/components/tip' import vue from 'vue'; const tipsCompontent = vue.extend(tips); function showTips(parma={ text:'提示',time:2000,icon:0 }){ var tips=new tipsCompontent({ el:document.createElement('div'), data(){ return { show:true, text:parma.text, icon:parma.icon } } }); document.body.appendChild(tips.$el); let timeIndex=setTimeout(()=>{ tips.show=false; clearTimeout(timeIndex) },parma.time) } export default showTips
入口main.js
import showTips from './tips.js' Vue.prototype.showTips=showTips;
使用
this.showTips({ text:'提示',time:2000 });
列表:
列表组建
<template> <div> <ul> <li v-for='(item) in todoData' :key="item.id"> {{ item.text }} </li> </ul> </div> </template> <script> export default { props:['todoData'] } </script>
list.js
import listCom from '@/components/list' import vue from 'vue' const listComp =vue.extend(listCom); function listView(data){ let list =new listComp({ el:document.createElement('div'), propsData:{ todoData:data } }) document.body.appendChild(list.$el); } function listFn(){ vue.prototype.$list=listView; } export default listFn
入口函数main.js
import listFn from './list.js' Vue.use(listFn)
调用
this.$list([{text:1,id:1},{text:2,id:2}])