当前位置: 首页 > news >正文

Vue的脚手架

脚手架配置

脚手架文档:Vue CLI

npm config set registry https://registry.npm.taobao.org

vue.config.js配置选项:

配置参考 | Vue CLI

ref选项

ref和id类似,给标签打标识。

document.getElementById('btn');

this.$ref.btn;

父子组件间通信

App.vue 加上冒号之后,18就成了一个表达式。 传递到Student中,就可以进行运算了。

<Student name="李四" age="18"/>
<Student name="李四" :age="18"/>  

Student.vue,接受的数据,相当于跑到了VC中的data里边。

props:['name','age']
props:{name:String,age:Number,sex:String
}
props:{name:{type:String,required:true,default:''}
}

props收到的属性,是不建议修改的,控制台会报错的。

data(){return{myAge:this.age}
}

但是将收到的属性值,赋值到新的字段中,是可以支持修改的。

引入混合js[复用配置]

mixin.js
export const hunhe = {methods:{showName(){//....}},mounted(){//....}
}

引入

import {hunhe} from '../mixin'
export default{name:'Student',data(){return{}},mixins:[hunhe],
}

全局混合,不建议用

import {hunhe,hunhe2} from './mixin'
Vue.mixin(hunhe)

插件plugins

plugins.js
export default{install(Vue){console.log('aaa');Vue.filter(...)}
}
main.js 在new Vue({})上边引入
import plugins from './plugins'
Vue.use(plugins)

scoped作用域

<style lang="less" scoped>
....
</style>

查看npm库版本

npm view webpack versionsnpm i less-loader@7

子传父组件通信-v1

App.vue
methods:{receive(x){}
}
<Myheader :receive="receive"/>
//==============================================================
MyHeader.vue
props:['receive'],
methods:{add(e){this.reveive(e.target.value);}
}

统计数量

computed:{doneTotal(){let i = 0;this.todos.forEach((todo)=>{if(todo.done) i++;})return i;}
}
const x = this.todus.reduce((pre,current)=>{return pre+(current.todo ? 1: 0);
},0); //0是传入的初始值 {}里边的逻辑,数组有多少个,就调用多少次  
第二次调用时,pre就是第一次调用函数的返回值
current就是每一个todo项
x就是计算todo项为true的统计
computed:{doneTotal(){return this.todos.reduce((pre,todo)=>pre+(todo.done?1:0),0);}
}

浏览器的本地存储

localstorage也可以用在移动端开发中

**组件的自定义事件通信**

1、通过父组件给子组件传递函数类型的props实现,子给父传递数据

2、绑定自定义事件传递给父组件

//自定义事件,给Student所在的vc绑定事件
App.vue
<Student v-on:pshdhx="demo"/> 
methods:{demo(name){console.log('demo被调用了',name)}
}
Student.vue
<button @click="sendStudentName">把学生名传递给app</button>
methods:{sendStudentName(){this.$emit('pshdhx',this.name) //触发Student组件实例的pshdhx事件}
}

3、使用ref来替换

//使用ref来替换  <Student v-on:pshdhx="demo"/>  <Student @pshdhx.once="demo"/>
App.vue
<Student ref="student"/> 
methods:{getStudentName(name,...params){ //params是一个数组console.log('App.vue收到了Student.vue传递过来的name值',name,params);}
}
mounted:{this.$refs.student.$on('pshdhx',this.getStudentName);this.$refs.student.$once('pshdhx',this.getStudentName);
}

解绑自定义事件

//方法区域
unbind(){this.$off('pshdhx'); //只适用于解绑一个this.$off(['pshdhx','demo2']); //用数组来解绑多个自定义事件this.$off();//解绑所有
}

箭头函数没有自己的this,所以就往外找。

自定义组件要想使用Vue事件

<Student @click.native="showInfo"/>
//如果不加.native,它就是一个自定义事件。

全局事件总线

任意组件之间的通信。

傀儡VueComponent,就是x

//App.vue
const Demo = Vue.extend({})
const d = new Demo();
Vue.prototype.x = d;//School.vue
mounted(){this.x.$on('hello',(data)=>{console.log('我是school组件,收到了数据',data);})
}
//Student.vue
methods:{sendStudentName(){this.x.$emit('hello',666);}
}

构建傀儡组件2 ,就是x

new Vue({el:'#app',render:h=>h(App),beforeCreate(){Vue.prototype.x = this}
})

x就是$bus了。

$bus很累了,所以销毁组件的时候,就要关闭

beforeDestory(){this.$bus.off('hello');
}

消息订阅与发布

npm i pubsub-js

School.vue
import pubsub from 'pubsub-js'
mounted:{this.pubid = pubsub.subscribe('hello',function(name,data){console.log('有人发布了hello消息,hello的回调执行了',data)})
}
Student.vue
import pubsub from 'pubsub-js'
methods:{sendStudentName(){pubsub.publish('hello',666);}
}

取消订阅,订阅之后,返回了一个id,需要销毁。

beforeDestory(){pubsub.unsubscribe(this.pubid);
}

判断对象有没有这个属性

todo.hasOwnProperty('isEdit')

$nextTick

点击编辑按钮,input获取焦点:

<input ref="inputTitle"/>
this.$ref.inputTitle.focus(); settimeout
this.$nextTick(function(){//会在dom节点更新之后,才会触发
})

动画效果

<template><div><button @click="isShow = !isShow">显示/隐藏</button>
<!--        <transition name="test" :appear="true"> 刷新时,也能有动画效果 --><transition name="test" appear><h1 v-show="isShow" class="come">你好呀</h1></transition></div>
</template><script>export default {// eslint-disable-next-line vue/multi-word-component-namesname: "Test",data(){return{isShow:true}}}
</script><style scoped>/*借助动画效果去实现过度*/h1{background-color: aqua;}/*入场动画 名称固定 配合transition 使用*//*.v-enter-active{*/.test-enter-active{animation: pshdhx 1s ;}/*离场动画,名称固定*//*.v-leave-active{*/.test-leave-active{animation: pshdhx 1s reverse;}/*定义动画*/@keyframes pshdhx {from{transform: translateX(-100%);}to{transform: translateX(0px);}}
</style>
<template><div><button @click="isShow = !isShow">显示/隐藏2</button>
<!--        <transition name="test" :appear="true"> 刷新时,也能有动画效果 --><transition name="test2" appear><h1 v-show="isShow" class="come">你好呀2</h1></transition></div>
</template><script>export default {// eslint-disable-next-line vue/multi-word-component-namesname: "Test2",data(){return{isShow:true}}}
</script><style scoped>/*接触过度效果去实现过度*/h1{background-color: aqua;}/*进入的起点、离开的终点*/.test2-enter,.test2-leave-to{transform: translateX(-100%);}.test2-enter-active,.test2-leave-active{transition: 0.5s linear;}/*进入的终点,离开的起点*/.test2-enter-to,.test2-leave{transform: translateX(0);}</style>
<template><div><button @click="isShow = !isShow">显示/隐藏3</button><transition appearname="test"enter-active-class="animate__animated animate__swing"leave-active-class="animate__animated animate__backOutUp"><h1 v-if="isShow">你好呀3</h1></transition></div>
</template><script>import 'animate.css';export default {// eslint-disable-next-line vue/multi-word-component-namesname: "Test3",data() {return {isShow: true}}}
</script><style scoped>/*接触过度效果去实现过度*/h1 {background-color: antiquewhite;}</style>

http://www.lryc.cn/news/264089.html

相关文章:

  • Java实现Word中插入上标和下标
  • Java和Python中的目标堆栈规划实现
  • (前端)后管系统登录后隐藏url上信息同时获取url上携带参数~开发需求(bug)总结7
  • CSS3新增样式
  • HP服务器idrac设置以及系统安装
  • rpc和消息队列区别
  • Permission denied (publickey,gssapi-keyex,gssapi-with-mic).
  • 虚幻学习笔记18—C++委托(多播)和事件
  • 【UML】第9篇 类图
  • I.MX6ULL启动详解:Boot配置、Bootable image启动头的组成
  • 隐藏通信隧道技术——防御SSH隧道攻击的思路
  • UE-近战战斗系统学习笔记一
  • 使用 Layui 的 template 模块来动态加载select选项
  • 《数据分析-JiMuReport》积木报表详细入门教程
  • React面试题:React.Component和React.PureComponent的区别?
  • 力扣:203. 移除链表元素(Python3)
  • 微信小程序-选择和分割打开地图选择位置的信息
  • Flink Table API 与 SQL 编程整理
  • 华为OS与麒麟OS:华为自研操作系统的对决
  • Java解决比特维位计数
  • 【深度学习目标检测】九、基于yolov5的路标识别(python,目标检测)
  • PyCharm添加自动函数文档注释
  • 数字图像处理 基于Numpy、PyTorch在频率空间中建模运动模糊
  • 海康威视对讲广播系统 RCE漏洞复现(CVE-2023-6895)
  • 【优化】Springboot 修改 tomcat连接池
  • 百度侯震宇:AI原生与大模型将从三个层面重构云计算
  • 【SpringBoot快速入门】(2)SpringBoot的配置文件与配置方式详细讲解
  • 麒麟V10 ARM 离线生成RabbitMQ docker镜像并上传Harbor私有仓库
  • AI创作系统ChatGPT商业运营网站系统源码,支持AI绘画,GPT语音对话+DALL-E3文生图
  • 剑指offer题解合集——Week1day5