## DealEvents.vue <template> <div> <button @click="clickBtn('撩课', $event)">点我</button> <h3>事件修饰符</h3> <a href="http://www.itlike.com" @click.prevent="aClick">撩课</a> <div style="width: 100px; height: 100px; background-color:red;" @click="divClick"> <button @click.stop="btnClick">点我</button> </div> <h3>按键修饰符</h3> <input type="text" @keyup.enter="dealKey"> </div> </template> <script> export default { name: "DealEvents", methods:{ clickBtn(args, event){ console.log(args); console.log(event); }, aClick(){ alert(0); }, btnClick(){ alert('点击了里面的按钮'); }, divClick(){ alert('点击了父标签'); }, dealKey(event){ console.log(event['keyCode']); } } } </script> <style scoped> </style>
## App.vue <template> <div id="app"> <DealEvents /> </div> </template> <script> import DealEvents from './components/DealEvents' export default { name: 'app', components: { DealEvents } } </script> <style> </style>
// 注册全局过滤器 Vue.filter('totalMonetFormat', (value)=>{ return '¥' + Number(value).toFixed(3); });
filters: { moneyFormat(value) { return '¥' + Number(value).toFixed(2); }, timeFormat(value, format='YYYY-MM-DD HH:mm:ss'){ return moment(value).format(format); } }
<template> <div> <h3>格式化人民币</h3> <p>局部过滤:{{money | moneyFormat}}</p> <p>局部过滤:{{page | moneyFormat}}</p> <p>全局过滤:{{money | totalMonetFormat}}</p> <p>全局过滤:{{page | totalMonetFormat}}</p> <p>------------------------------------------------</p> <h3>格式化日期</h3> <p>{{time | timeFormat}}</p> <p>{{time | timeFormat('YYYY-MM-DD')}}</p> <p>{{time | timeFormat('HH:mm:ss')}}</p> </div> </template>
## main.js import Vue from 'vue' import App from './App.vue' Vue.config.productionTip = false; // 注册全局过滤器 Vue.filter('wholeMoneyFormat', (value)=>{ return '¥' + Number(value).toFixed(4); }); new Vue({ render: h => h(App), }).$mount('#app');
## LkFilters.vue <template> <div> <h3>格式化人民币</h3> <p>{{money | moneyFormat}}</p> <p>{{page | moneyFormat}}</p> <p>---------------------------------------------</p> <p>{{money | wholeMoneyFormat}}</p> <p>{{page | wholeMoneyFormat}}</p> <h3>格式化日期</h3> <p>{{time | timeFormat}}</p> <p>{{time | timeFormat('YYYY-MM-DD')}}</p> <p>{{time | timeFormat('HH:mm:ss')}}</p> </div> </template> <script> import moment from 'moment' export default { name: "LkFilters", data(){ return { money: 22345, page: 189.4345, time: new Date() } }, mounted(){ setInterval(()=>{ this.time = new Date() }, 1000) }, // 局部过滤器 filters: { moneyFormat(value){ return '¥' + Number(value).toFixed(2); }, timeFormat(value, format='YYYY-MM-DD HH:mm:ss'){ return moment(value).format(format); } } } </script> <style scoped> </style>
## App.vue <template> <div id="app"> <LkFilters /> </div> </template> <script> import LkFilters from './components/LkFilters' export default { name: 'app', components: { LkFilters } } </script> <style> </style>
<transition name="xxx">
## TransitionAndAnimate.vue <template> <div> <button @click="show = !show">切换</button> <transition name="fade"> <div class="box" v-if="show">撩课学院</div> </transition> </div> </template> <script> export default { name: "TransitionAndAnimate", data(){ return { show: false } } } </script> <style scoped> .box{ width: 200px; height: 200px; background-color: red; color: #fff; font-size: 20px; display: flex; justify-content: center; align-items: center; } .fade-enter, .fade-leave-to{ opacity: 0; transform: translateX(200px) scale(3); } .fade-enter-active, .fade-leave-active{ transition: all 2s ease-in-out; } </style>
## TransitionAndAnimateTwo.vue <template> <div> <button @click="flag = !flag">切换</button> <p></p> <transition name="bounce"> <img v-if="flag" :src="pic" alt=""> </transition> </div> </template> <script> import pic from '@/assets/img_02.jpg' export default { name: "TransitionAndAnimateTwo", data() { return { pic: pic, flag: false } } } </script> <style scoped> .bounce-enter-active { animation: bounce 1s; } .bounce-leave-active { animation: bounce 1s reverse; } @keyframes bounce { 0% { transform: scale(0); } 25% { transform: scale(0.2); } 50% { transform: scale(0.4); } 75% { transform: scale(0.6); } 100% { transform: scale(1); } } </style>
## TransitionAndAnimateThree.vue <template> <div> <button @click="flag = !flag">切换</button> <p></p> <transition enter-active-class="animated rollIn" leave-active-class="animated rollOut" :duration="{ enter: 1000, leave: 500 }" > <img v-if="flag" :src="pic" alt=""> </transition> </div> </template> <script> import pic from '@/assets/img_02.jpg' import animate from 'animate.css' export default { name: "TransitionAndAnimateThree", data() { return { pic: pic, flag: false } } } </script> <style scoped> </style>
## App.vue <template> <div id="app"> <TransitionAndAnimate /> <TransitionAndAnimateTwo /> <TransitionAndAnimateThree /> </div> </template> <script> import TransitionAndAnimate from './components/TransitionAndAnimate' import TransitionAndAnimateTwo from './components/TransitionAndAnimateTwo' import TransitionAndAnimateThree from './components/TransitionAndAnimateThree' export default { name: 'app', components: { TransitionAndAnimate, TransitionAndAnimateTwo, TransitionAndAnimateThree } } </script> <style> </style>
## LifeCycle.vue <template> <div> <p v-if="isShow">{{str1}}</p> <p v-else>{{str2}}</p> <button @click="destory">销毁</button> </div> </template> <script> export default { name: "LifeCycle", beforeCreate(){ console.log('1:beforeCreate()'); }, data(){ return { isShow: false, str1: '撩课学院', str2: 'itLike.com' } }, methods:{ destory(){ this.$destroy(); } }, created() { console.log('2:created()'); }, beforeMount() { console.log('3:beforeMount()'); }, mounted() { console.log('4:mounted()'); // 定时器 this.intervalId = setInterval(()=>{ console.log('+++++++++++---++++++++++'); this.isShow = !this.isShow; }, 1000); }, beforeUpdate() { console.log('5:beforeUpdate()'); }, updated() { console.log('6:updated()'); }, beforeDestroy() { console.log('7:beforeDestroy()'); // 清除定时器 clearInterval(this.intervalId); }, destroyed() { console.log('8:destroyed()'); } } </script> <style scoped> </style>
## App.vue <template> <div id="app"> <LifeCycle /> </div> </template> <script> import LifeCycle from './components/LifeCircle' export default { name: 'app', components: { LifeCycle } } </script> <style> </style>
props: ['name', 'age', 'logDog']
props: { name: String, age: Number, logDog: Function }
props: { name: {type: String, required: true, default:xxx}, }
## PropsComponent.vue <template> <div> <h4>姓名:{{name}}</h4> <h4>年龄:{{age}}</h4> <p>{{person}}</p> <button @click="logPerson('大撩撩', 60)">调用方法</button> </div> </template> <script> export default { name: "PropsComponent", // props: ['name', 'age'] /* props: { name: String, age: Number, person: Object, logPerson: Function } */ props: { name: {type: String, required: true, default: '撩课'}, age: {type: Number, required: true, default: 20}, person: Object, logPerson: Function } } </script> <style scoped> </style>
App.vue <template> <div id="app"> <PropsComponent :age=25 :person="p" :log-person="logPerson" /> </div> </template> <script> import PropsComponent from './components/PropsComponent' export default { name: 'app', data(){ return { p: { name: '张三丰', age: 600 } } }, components: { PropsComponent }, methods: { logPerson(name, age){ alert(`姓名:${name}, 年龄:${age}`); } } } </script> <style> .word{ width: 300px; height: 200px; background-color: red; color: #fff; display: flex; justify-content: center; align-items: center; } </style>
<button @click="btnClick">删除父标签P</button> this.$emit('btnClick', {name: '小撩', age: 25, sex:'女'});
<CustomEvents @btnClick="deleteP"/> deleteP(args){ console.log(args); this.$refs.word.remove(); }
props: { name: {type: String, required: true, default:xxx}, }
## CustomEvents.vue <template> <div> <button @click="btnClick">删除父组件的P标签</button> </div> </template> <script> export default { name: "CustomEvents", methods: { btnClick(){ // 告诉父组件,我点击了按钮 this.$emit('btnClick', {name: '哈哈哈', sex:'男'}); } } } </script> <style scoped> </style>
## App.vue <template> <div id="app"> <CustomEvents @btnClick="deleteP"/> <p ref="word" class="word">我是即将被删除的</p> </div> </template> <script> import CustomEvents from './components/CustomEvents' export default { name: 'app', data(){ return { } }, components: { CustomEvents }, methods: { deleteP(args){ console.log(args); this.$refs.word.remove(); } } } </script> <style> .word{ width: 300px; height: 200px; background-color: red; color: #fff; display: flex; justify-content: center; align-items: center; } </style>
>> npm install --save pubsub-js
import PubSub from ‘pubsub-js’
// 发布删除P标签消息 PubSub.publish("delete-p", {name:"大撩撩", sex:"女", age: 45}); // 订阅关闭弹窗事件,参数event指"delete-p" PubSub.subscribe("delete-p", (event, data) => { console.log(data); // 删除P标签 this.deleteP(); });
本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。
我来说两句