【Vue2】3-使用Vue脚手架

目录

初始化脚手架

说明

具体步骤

模板项目的结构

关于不同版本的Vue

vue.config.js配置文件

ref属性

配置项props

mixin(混入)

插件

scoped样式

总结TodoList案例

webStorage(浏览器本地存储)

TodoList本地存储

组件的自定义事件

全局事件总线(GlobalEventBus)

TodoList事件总线

消息订阅与发布(pubsub)

TodoList_pubsub

nextTick

Vue封装的过渡与动画

TodoList_动画


初始化脚手架

说明

Vue脚手架是Vue官方提供的标准化开发工具(开发平台)

文档:https://cli.vuejs.org/zh/

具体步骤

1.全局安装@vue/cli

npm install -g @vue/cli

2.切换到要创建项目的目录,然后使用命令创建项目

vue create xxxx

3.启动项目

npm run serve

备注:

Vue脚手架隐藏了所有webpack相关的配置,若想查看具体的webpack配置,执行:

vue inspect > output.js

模板项目的结构

分析脚手架:

main.js:

/*** 该文件是整个项目的入口文件*///引入Vue
import Vue from 'vue'
//引入App组件,它是所有组件的父组件
import App from './App.vue'//关闭Vue的生产提示
Vue.config.productionTip = false//创建Vue实例对象vm
new Vue({el:'#app',//将App组件放入容器中render: h => h(App),
})

App.vue:

<template><div><img src="./assets/logo.png" alt="logo"><MySchool></MySchool><MyStudent></MyStudent></div>
</template><script>//引入组件import MySchool from './components/MySchool';import MyStudent from './components/MyStudent';export default {name:'App',components:{MySchool,MyStudent}}
</script><style></style>

MySchool.vue:

<template><!-- 组件的结构 --><div class="demo"><h2>学校名称:{{SchoolName}}</h2><h2>学校地址:{{address}}</h2><button @click="showName">点我提示学校名</button></div>
</template><script>//组件交互相关的代码(数据、方法等等)export default{name:'MySchool',data(){return{SchoolName:'尚硅谷1',address:'北京'}},methods: {showName(){alert(this.SchoolName)}}}</script><style>/* 组件的样式 */.demo{background-color: orange;}
</style>

MyStudent.vue:

<template><!-- 组件的结构 --><div><h2>学生姓名:{{StudentName}}</h2><h2>学校年龄:{{age}}</h2></div>
</template><script>//组件交互相关的代码(数据、方法等等)export default{name:'MyStudent',data(){return{StudentName:'张三',age:18}}}</script>

关于不同版本的Vue

vue.js与vue.runtime.xxx.js的区别:

(1)vue.js是完整版的Vue,包含:核心功能+模板解析器

(2)vue.runtime.xxx.js是运行版的Vue,只包含:核心功能,没有模板解析器

因为vue.runtime.xxx.js没有模板解析器,所以不能使用template配置项,需要使用render函数接收

到的createElement函数去指定具体内容

vue.config.js配置文件

使用vue inspect > output.js可以查看到Vue脚手架的默认配置

使用vue.config.js可以对脚手架进行个性化定制,详情见:https://cli.vuejs.org/zh

ref属性

  • 被用来给元素或子组件注册引用信息(id的替代者)
  • 应用在html标签上获取的是真实DOM元素,应用在组件标签上是组件实例对象(vc)
  • 使用方式:

        打标识:<h1 ref="xxx">......</h1>或<School ref="xxx"></School>

        获取:this.$refs.xxx

main.js:

//引入Vue
import Vue from "vue";
//引入App
import App from "./App.vue";Vue.config.productionTip = false//创建vm
new Vue({el:'#app',render: h => h(App)
})

App.vue:

<template><div><h1 v-text="msg" ref="title"></h1><button ref="btn" @click="showDOM">点我输出上方的DOM元素</button><School ref="sch"/></div>
</template>
<script>//引入School组件import School from './components/School'export default {name:'App',components:{School},data() {return {msg:'欢迎学习Vue'}},methods:{showDOM(){console.log(this.$refs.title) //真实DOM元素console.log(this.$refs.btn) //真实DOM元素console.log(this.$refs.sch) //School组件的实例对象(vc)}}}
</script>

School.vue:

<template><div class="school"><h2>学校名称:{{name}}</h2><h2>学校地址:{{address}}</h2></div>
</template>
<script>
export default {name:'School',data(){return{name:'尚硅谷1',address:'北京'}}
}
</script>
<style>.school{background-color: gray;}
</style>

配置项props

 功能:让组件接收外部传过来的数据

1.传递数据:

<Demo name="xxx"/>

2.接收数据:

(1)第一种方式(只接收):

props:['name']

(2)第二种方式(限制类型):

props:{name:String
}

(3)第三种方式(限制类型、限制必要性、指定默认值):

props:{name:{type:String, //类型required:true, //必要性default:'张三' //默认值}
}

备注:props是只读的,Vue底层会监测你对props的修改,如果进行了修改,就会发出警告,若业

务需求确实需要修改,那么请复制props的内容到data中一份,然后去修改data中的数据

main.js:

//引入Vue
import Vue from "vue";
//引入App
import App from "./App.vue";Vue.config.productionTip = false//创建vm
new Vue({el:'#app',render: h => h(App)
})

App.vue:

<template><div><Student name="李四" sex="女" :age="18"/></div>
</template>
<script>//引入Student组件import Student from './components/Student.vue';export default {name:'App',components:{Student}}
</script>

Student.vue:

<template><div><h1>{{msg}}</h1><h2>学生姓名:{{name}}</h2><h2>学生性别:{{sex}}</h2><h2>学生年龄:{{myAge+1}}</h2><button @click="updateAge">尝试修改收到的年龄</button></div>
</template>
<script>
export default {name:'Student',data(){return{msg:'我是一个尚硅谷的学生',myAge:this.age}},methods:{updateAge(){this.myAge++}},//简单声明接收props:['name','sex','age'] //接收的同时对数据进行类型限制/* props:{name:String,age:Number,sex:String} *///接收的同时对数据进行类型限制+默认值的指定+必要性的限制/* props:{name:{type:String, //name的类型required:true //name是必要的},age:{type:Number,default:99 //默认值},sex:{type:String,required:true}} */}
</script>

mixin(混入)

功能:可以把多个组件共用的配置提取成一个混入对象

使用方式:

  • 第一步:定义混入
{data(){},methods:{}......
}
  • 第二步:使用混入

(1)全局混入:Vue.mixin(xxx)

(2)局部混入:mixins:['xxx']

mixin.js:

export const hunhe = {methods:{showName(){alert(this.name)}},mounted() {console.log('你好')}
}export const hunhe2 = {data() {return {x:100,y:200}}
}

main.js:

//引入Vue
import Vue from "vue";
//引入App
import App from "./App.vue";
//引入混入
import { hunhe,hunhe2 } from "./mixin";Vue.config.productionTip = falseVue.mixin(hunhe)
Vue.mixin(hunhe2)//创建vm
new Vue({el:'#app',render: h => h(App)
})

App.vue:

<template><div><School/><hr><Student/></div>
</template>
<script>//引入School组件import School from './components/School.vue';import Student from './components/Student.vue';export default {name:'App',components:{School,Student}}
</script>

School.vue:

<template><div><h2 @click="showName">学校名称:{{name}}</h2><h2>学校地址:{{address}}</h2></div>
</template><script>
// import { hunhe,hunhe2 } from '@/mixin';export default {name:'School',data(){return{name:'尚硅谷',address:'北京'}},// mixins:[hunhe,hunhe2],mounted(){console.log('你好!!!')}
}
</script>

Student.vue:

<template><div><h2 @click="showName">学生姓名:{{name}}</h2><h2>学生性别:{{sex}}</h2></div>
</template><script>
// import { hunhe } from '@/mixin';
export default {name:'Student',data(){return{name:'张三',sex:'男'}},// mixins:[hunhe]
}
</script>

插件

功能:用于增强Vue

本质:包含install方法的一个对象,install的第一个参数是Vue,第二个以后的参数是插件使用者传

递的数据

定义插件:

对象.install = function(Vue,options){//1.添加全局过滤器Vue.filter(...)//2.添加全局指令Vue.directive(...)//3.配置全局混入Vue.mixin(...)//4.添加实例方法Vue.prototype.$myMethod = function(){...}Vue.prototype.$myProperty = xxx
}

使用插件:Vue.use()

plugins.js:

export default {install(Vue){//全局过滤器Vue.filter('mySlice',function(value){return value.slice(0,4)})//全局指令(同过滤器)Vue.directive('fbind',{//指令与元素成功绑定时(一上来)bind(element,binding){element.value = binding.value},//指令所在元素被插入页面时inserted(element,binding){element.focus()},//指令所在的模板被重新解析时update(element,binding){element.value = binding.value}})//定义混入Vue.mixin({data(){return{x:100,y:200}}})//给Vue原型上添加一个方法(vm和vc都能使用)Vue.prototype.hello = ()=>(alert('你好'))}
}

main.js:

//引入Vue
import Vue from "vue";
//引入App
import App from "./App.vue";
//引入插件
import plugins from "./plugins";Vue.config.productionTip = false//应用插件
Vue.use(plugins)//创建vm
new Vue({el:'#app',render: h => h(App)
})

App.vue:

<template><div><School/><hr><Student/></div>
</template>
<script>//引入School组件import School from './components/School.vue';import Student from './components/Student.vue';export default {name:'App',components:{School,Student}}
</script>

School.vue:

<template><div><h2>学校名称:{{name | mySlice}}</h2><h2>学校地址:{{address}}</h2><button @click="test">点我测试hello方法</button></div>
</template><script>
export default {name:'School',data(){return{name:'尚硅谷atguigu',address:'北京'}},methods:{test(){this.hello()}}
}
</script>

Student.vue:

<template><div><h2>学生姓名:{{name}}</h2><h2>学生性别:{{sex}}</h2><input type="text" v-fbind:value="name"></div>
</template><script>export default {name:'Student',data(){return{name:'张三',sex:'男'}}
}
</script>

scoped样式

作用:让样式在局部生效,防止冲突

写法:<style scoped>

main.js:

//引入Vue
import Vue from "vue";
//引入App
import App from "./App.vue";Vue.config.productionTip = false//创建vm
new Vue({el:'#app',render: h => h(App)
})

App.vue:

<template><div><School/><Student/></div>
</template>
<script>//引入School组件import School from './components/School.vue';import Student from './components/Student.vue';export default {name:'App',components:{School,Student}}
</script>

School.vue:

<template><div class="demo"><h2>学校名称:{{name}}</h2><h2>学校地址:{{address}}</h2></div>
</template><script>
export default {name:'School',data(){return{name:'尚硅谷atguigu',address:'北京'}}
}
</script><style scoped>.demo{background-color: skyblue}
</style>

Student.vue:

<template><div class="demo"><h2>学生姓名:{{name}}</h2><h2 class="atguigu">学生性别:{{sex}}</h2></div>
</template><script>export default {name:'Student',data(){return{name:'张三',sex:'男'}},}
</script><style lang="less" scoped>.demo{background-color: orange;.atguigu{font-size: 40px;}}
</style>

总结TodoList案例

组件化编码流程:

1.拆分静态组件:组件要按照功能点拆分,命名不要与html元素冲突

2.实现动态组件:考虑好数据的存放位置,数据是一个组件在用,还是一些组件在用:

    (1)一个组件在用:放在组件自身即可

    (2)一些组件在用:放在他们共同的父组件上(状态提升)

3.实现交互:从绑定事件开始

props适用于:

1.父组件 ===> 子组件 通信

2.子组件 ===> 父组件 通信(要求父先给子一个函数)

使用v-mode时要切记:v-model绑定的值不能是props传过来的值,因为props是不可以修改的

props传过来的若是对象类型的值,修改对象中的属性时Vue不会报错,但不推荐这样做

main.js:

//引入Vue
import Vue from "vue";
//引入App
import App from "./App.vue";Vue.config.productionTip = false//创建vm
new Vue({el:'#app',render: h => h(App)
})

App.vue:

<template><div id="root"><div class="todo-container"><div class="todo-wrap"><MyHeader :addTodo="addTodo"/><MyList :todos="todos" :checkedTodo="checkedTodo" :deleteTodo="deleteTodo"/><MyFooter :todos="todos" :checkAllTodo="checkAllTodo" :clearAllTodo="clearAllTodo"/></div></div></div>
</template>
<script>import MyHeader from './components/MyHeader.vue';import MyList from './components/MyList.vue';import MyFooter from './components/MyFooter.vue';export default {name:'App',components:{MyHeader,MyList,MyFooter},data(){return{//由于todos是MyHeader组件和MyFooter组件都在使用,所以放在App中(状态提升)todos:[{id:'001',title:'吃饭',done:true},{id:'002',title:'睡觉',done:false},{id:'003',title:'开车',done:true}]}},methods: {//添加一个todoaddTodo(todoObj){this.todos.unshift(todoObj)},//勾选or取消勾选一个todocheckedTodo(id){this.todos.forEach((todo)=>{if(todo.id == id) todo.done = !todo.done})},//删除一个tododeleteTodo(id){this.todos = this.todos.filter((todo)=>{return todo.id !== id})},//全选or取消全选checkAllTodo(done){this.todos.forEach((todo)=>{todo.done = done})},//清除所有已完成的todoclearAllTodo(){this.todos = this.todos.filter((todo)=>{return !todo.done})}}}
</script><style>/*base*/body {background: #fff;}.btn {display: inline-block;padding: 4px 12px;margin-bottom: 0;font-size: 14px;line-height: 20px;text-align: center;vertical-align: middle;cursor: pointer;box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);border-radius: 4px;}.btn-danger {color: #fff;background-color: #da4f49;border: 1px solid #bd362f;}.btn-danger:hover {color: #fff;background-color: #bd362f;}.btn:focus {outline: none;}.todo-container {width: 600px;margin: 0 auto;}.todo-container .todo-wrap {padding: 10px;border: 1px solid #ddd;border-radius: 5px;}
</style>

MyFooter.vue:

<template><div class="todo-footer" v-show="total"><label><input type="checkbox" :checked="isAll" @change="checkAll"/></label><span><span>已完成{{doneTotal}}</span> / 全部{{total}}</span><button class="btn btn-danger" @click="clearAll">清除已完成任务</button></div>
</template><script>export default {name:'MyFooter',props:['todos','checkAllTodo','clearAllTodo'],computed:{total(){return this.todos.length},doneTotal(){//第一种方式let i = 0this.todos.forEach((todo) => {if(todo.done) i++})return i//第二种方式// return this.todos.reduce((pre,todo)=> pre + (todo.done ? 1 : 0),0)},isAll(){return this.doneTotal === this.total && this.total > 0}},methods: {checkAll(e){this.checkAllTodo(e.target.checked)},clearAll(){this.clearAllTodo()}}}
</script><style scoped>/*footer*/.todo-footer {height: 40px;line-height: 40px;padding-left: 6px;margin-top: 5px;}.todo-footer label {display: inline-block;margin-right: 20px;cursor: pointer;}.todo-footer label input {position: relative;top: -1px;vertical-align: middle;margin-right: 5px;}.todo-footer button {float: right;margin-top: 5px;}
</style>

MyHeader.vue:

<template><div class="todo-header"><input type="text" placeholder="请输入你的任务名称,按回车键确认" v-model="title" @keyup.enter="add"/></div>
</template><script>import { nanoid } from 'nanoid';export default {name:'MyHeader',props:['addTodo'],data() {return {title:''}},methods:{add(){//校验数据if(!this.title.trim()) return alert('输入不能为空')//将用户的输入包装成一个todo对象const todoObj = {id:nanoid(),title:this.title,done:false}//通知App组件添加一个todo对象this.addTodo(todoObj)//清空输入this.title = ''}}}
</script><style scoped>/*header*/.todo-header input {width: 560px;height: 28px;font-size: 14px;border: 1px solid #ccc;border-radius: 4px;padding: 4px 7px;}.todo-header input:focus {outline: none;border-color: rgba(82, 168, 236, 0.8);box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);}
</style>

MyItem.vue:

<template><li><label><input type="checkbox" :checked="todo.done" @change="handleCheck(todo.id)"/><!-- 如下代码也能实现功能,但不推荐,违背原则,修改了props --><!-- <input type="checkbox" v-model="todo.done"/> --><span>{{todo.title}}</span></label><button class="btn btn-danger" @click="handleDelete(todo.id)">删除</button></li>
</template><script>export default {name:'MyItem',//声明接收todo对象props:['todo','checkedTodo','deleteTodo'],methods: {//勾选or取消勾选handleCheck(id){//通知App组件将对应的todo对象的done值取反this.checkedTodo(id)},//删除handleDelete(id){if(confirm('确定删除吗?')){this.deleteTodo(id)}}},}
</script><style scoped>/*item*/li {list-style: none;height: 36px;line-height: 36px;padding: 0 5px;border-bottom: 1px solid #ddd;}li label {float: left;cursor: pointer;}li label li input {vertical-align: middle;margin-right: 6px;position: relative;top: -1px;}li button {float: right;display: none;margin-top: 3px;}li:before {content: initial;}li:last-child {border-bottom: none;}li:hover{background-color: #ddd;}li:hover button{display: block;}
</style>

MyList.vue:

<template><ul class="todo-main"><MyItem v-for="todoObj in todos" :key="todoObj.id" :todo="todoObj" :checkedTodo="checkedTodo":deleteTodo="deleteTodo"/></ul>
</template><script>import MyItem from './MyItem.vue';export default {name:'MyList',components:{MyItem},props:['todos','checkedTodo','deleteTodo']}
</script><style scoped>/*main*/.todo-main {margin-left: 0px;border: 1px solid #ddd;border-radius: 2px;padding: 0px;}.todo-empty {height: 40px;line-height: 40px;border: 1px solid #ddd;border-radius: 2px;padding-left: 5px;margin-top: 10px;}
</style>

webStorage(浏览器本地存储)

存储内容大小一般支持5MB左右(不同的浏览器可能不一样)

浏览器端通过Window.sessionStorage和Window.localStorage属性来实现本地存储机制

相关API:

  • xxxxxStorage.setItem('key','value'); //该方法接收一个键和值作为参数,会把键值对添加到存储中,如果键名存在,则更新其对应的值
  • xxxxxStorage.getItem('person'); //该方法接收一个键名作为参数,返回键名对应的值
  • xxxxxStorage.removeItem('key'); //该方法接收一个键名作为参数,并把该键名从存储中删除
  • xxxxxStorage.clear(); //该方法会清空存储中的所有数据

备注:

  • sessionStorage存储的内容会随着浏览器窗口关闭而消失
  • localStorage存储的内容需要手动消除才会消失
  • xxxxxStorage.getItem(xxx)如果xxx对应的value获取不到,那么getItem的返回值是null
  • JSON.parse(null)的结果依然是null

localStorage.html:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>localStorage</title>
</head>
<body><h2>localStorage</h2><button onclick="saveData()">点我保存一个数据</button><button onclick="readData()">点我读取一个数据</button><button onclick="deleteData()">点我删除一个数据</button><button onclick="deleteAllData()">点我清空一个数据</button><script type="text/javascript">let p = {name:'张三',age:18}function saveData(){localStorage.setItem('msg','hello!')localStorage.setItem('msg2',666)localStorage.setItem('person',JSON.stringify(p))}function readData(){console.log(localStorage.getItem('msg'))console.log(localStorage.getItem('msg2'))const result  =localStorage.getItem('person')console.log(JSON.parse(result))}function deleteData(){localStorage.removeItem('msg')}function deleteAllData(){localStorage.clear()}</script>
</body>
</html>

sessionStorage.html:

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><meta name="viewport" content="width=device-width, initial-scale=1.0"><title>sessionStorage</title>
</head>
<body><h2>sessionStorage</h2><button onclick="saveData()">点我保存一个数据</button><button onclick="readData()">点我读取一个数据</button><button onclick="deleteData()">点我删除一个数据</button><button onclick="deleteAllData()">点我清空一个数据</button><script type="text/javascript">let p = {name:'张三',age:18}function saveData(){sessionStorage.setItem('msg','hello!')sessionStorage.setItem('msg2',666)sessionStorage.setItem('person',JSON.stringify(p))}function readData(){console.log(sessionStorage.getItem('msg'))console.log(sessionStorage.getItem('msg2'))const result  =sessionStorage.getItem('person')console.log(JSON.parse(result))}function deleteData(){sessionStorage.removeItem('msg')}function deleteAllData(){sessionStorage.clear()}</script>
</body>
</html>

TodoList本地存储

main.js:

//引入Vue
import Vue from "vue";
//引入App
import App from "./App.vue";Vue.config.productionTip = false//创建vm
new Vue({el:'#app',render: h => h(App)
})

App.vue:

<template><div id="root"><div class="todo-container"><div class="todo-wrap"><MyHeader :addTodo="addTodo"/><MyList :todos="todos" :checkedTodo="checkedTodo" :deleteTodo="deleteTodo"/><MyFooter :todos="todos" :checkAllTodo="checkAllTodo" :clearAllTodo="clearAllTodo"/></div></div></div>
</template>
<script>import MyHeader from './components/MyHeader.vue';import MyList from './components/MyList.vue';import MyFooter from './components/MyFooter.vue';export default {name:'App',components:{MyHeader,MyList,MyFooter},data(){return{//由于todos是MyHeader组件和MyFooter组件都在使用,所以放在App中(状态提升)todos:JSON.parse(localStorage.getItem('todos')) || []}},methods: {//添加一个todoaddTodo(todoObj){this.todos.unshift(todoObj)},//勾选or取消勾选一个todocheckedTodo(id){this.todos.forEach((todo)=>{if(todo.id == id) todo.done = !todo.done})},//删除一个tododeleteTodo(id){this.todos = this.todos.filter((todo)=>{return todo.id !== id})},//全选or取消全选checkAllTodo(done){this.todos.forEach((todo)=>{todo.done = done})},//清除所有已完成的todoclearAllTodo(){this.todos = this.todos.filter((todo)=>{return !todo.done})}},watch:{todos:{deep:true,handler(value){localStorage.setItem('todos',JSON.stringify(value))}}}}
</script><style>/*base*/body {background: #fff;}.btn {display: inline-block;padding: 4px 12px;margin-bottom: 0;font-size: 14px;line-height: 20px;text-align: center;vertical-align: middle;cursor: pointer;box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);border-radius: 4px;}.btn-danger {color: #fff;background-color: #da4f49;border: 1px solid #bd362f;}.btn-danger:hover {color: #fff;background-color: #bd362f;}.btn:focus {outline: none;}.todo-container {width: 600px;margin: 0 auto;}.todo-container .todo-wrap {padding: 10px;border: 1px solid #ddd;border-radius: 5px;}
</style>

MyFooter.vue:

<template><div class="todo-footer" v-show="total"><label><input type="checkbox" :checked="isAll" @change="checkAll"/></label><span><span>已完成{{doneTotal}}</span> / 全部{{total}}</span><button class="btn btn-danger" @click="clearAll">清除已完成任务</button></div>
</template><script>export default {name:'MyFooter',props:['todos','checkAllTodo','clearAllTodo'],computed:{//总数total(){return this.todos.length},//已完成数doneTotal(){//第一种方式let i = 0this.todos.forEach((todo) => {if(todo.done) i++})return i//第二种方式(使用reduce方法做条件统计)// return this.todos.reduce((pre,todo)=> pre + (todo.done ? 1 : 0),0)},//控制全选框isAll(){return this.doneTotal === this.total && this.total > 0}},methods: {checkAll(e){this.checkAllTodo(e.target.checked)},clearAll(){this.clearAllTodo()}}}
</script><style scoped>/*footer*/.todo-footer {height: 40px;line-height: 40px;padding-left: 6px;margin-top: 5px;}.todo-footer label {display: inline-block;margin-right: 20px;cursor: pointer;}.todo-footer label input {position: relative;top: -1px;vertical-align: middle;margin-right: 5px;}.todo-footer button {float: right;margin-top: 5px;}
</style>

MyHeader.vue:

<template><div class="todo-header"><input type="text" placeholder="请输入你的任务名称,按回车键确认" v-model="title" @keyup.enter="add"/></div>
</template><script>import { nanoid } from 'nanoid';export default {name:'MyHeader',props:['addTodo'],data() {return {title:''}},methods:{add(){//校验数据if(!this.title.trim()) return alert('输入不能为空')//将用户的输入包装成一个todo对象const todoObj = {id:nanoid(),title:this.title,done:false}//通知App组件添加一个todo对象this.addTodo(todoObj)//清空输入this.title = ''}}}
</script><style scoped>/*header*/.todo-header input {width: 560px;height: 28px;font-size: 14px;border: 1px solid #ccc;border-radius: 4px;padding: 4px 7px;}.todo-header input:focus {outline: none;border-color: rgba(82, 168, 236, 0.8);box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);}
</style>

MyItem.vue:

<template><li><label><input type="checkbox" :checked="todo.done" @change="handleCheck(todo.id)"/><!-- 如下代码也能实现功能,但不推荐,违背原则,修改了props --><!-- <input type="checkbox" v-model="todo.done"/> --><span>{{todo.title}}</span></label><button class="btn btn-danger" @click="handleDelete(todo.id)">删除</button></li>
</template><script>export default {name:'MyItem',//声明接收todo对象props:['todo','checkedTodo','deleteTodo'],methods: {//勾选or取消勾选handleCheck(id){//通知App组件将对应的todo对象的done值取反this.checkedTodo(id)},//删除handleDelete(id){if(confirm('确定删除吗?')){this.deleteTodo(id)}}},}
</script><style scoped>/*item*/li {list-style: none;height: 36px;line-height: 36px;padding: 0 5px;border-bottom: 1px solid #ddd;}li label {float: left;cursor: pointer;}li label li input {vertical-align: middle;margin-right: 6px;position: relative;top: -1px;}li button {float: right;display: none;margin-top: 3px;}li:before {content: initial;}li:last-child {border-bottom: none;}li:hover{background-color: #ddd;}li:hover button{display: block;}
</style>

MyList.vue:

<template><ul class="todo-main"><MyItem v-for="todoObj in todos" :key="todoObj.id" :todo="todoObj" :checkedTodo="checkedTodo":deleteTodo="deleteTodo"/></ul>
</template><script>import MyItem from './MyItem.vue';export default {name:'MyList',components:{MyItem},props:['todos','checkedTodo','deleteTodo']}
</script><style scoped>/*main*/.todo-main {margin-left: 0px;border: 1px solid #ddd;border-radius: 2px;padding: 0px;}.todo-empty {height: 40px;line-height: 40px;border: 1px solid #ddd;border-radius: 2px;padding-left: 5px;margin-top: 10px;}
</style>

组件的自定义事件

一种组件间通信的方式,适用于:子组件 ===> 父组件

使用场景:A是父组件,B是子组件,B想给A传数据,那么就要在A中给B绑定自定义事件(事件的

回调在A中)

绑定自定义事件:

第一种方式:在父组件中:

<Demo @atguigu="test"/>或<Demo v-on:atguigu="test"/>

第二种方式:

<Demo ref="demo"/>......mounted(){this.$refs.xxx.$on('atguigu',this.test)
}

若想让自定义事件只能触发一次,可以使用once修饰符或$once方法

触发自定义事件:this.$emit('atguigu',数据)

解绑自定义事件:this.$off('atguigu')

组件上也可以绑定原生DOM事件,需要使用native修饰符

注意:通过this.$refs.xxx.$on('atguigu',回调)绑定自定义事件时,回调要么配置在methods中,要

么用箭头函数,否则this指向会出问题

main.js:

//引入Vue
import Vue from "vue";
//引入App
import App from "./App.vue";Vue.config.productionTip = false//创建vm
new Vue({el:'#app',render: h => h(App),mounted() {setTimeout(()=>{this.$destroy()},3000)},
})

App.vue:

<template><div class="app"><h1>{{msg}},学生姓名是:{{studentName}}</h1><!-- 通过父组件给子组件传递函数类型的props实现:子给父传递数据 --><School :getSchoolName="getSchoolName"/><!-- 通过父组件给子组件绑定一个自定义事件实现:子给父传递数据(第一种写法:使用@或v-on) --><!-- <Student @atguigu="getStudentName" @demo="m1"/> --><!-- 通过父组件给子组件绑定一个自定义事件实现:子给父传递数据(第二种写法:使用ref) --><Student ref="student" @click.native="show"/></div>
</template>
<script>//引入School组件import School from './components/School.vue';import Student from './components/Student.vue';export default {name:'App',components:{School,Student},data() {return {msg:'你好',studentName:''}},methods:{getSchoolName(name){console.log('App收到了学校名:',name)},getStudentName(name){console.log('App收到了学生名:',name)this.studentName = name},m1(){console.log('demo事件被触发了')},show(){alert(123)}},mounted() {this.$refs.student.$on('atguigu',this.getStudentName) //绑定自定义事件// this.$refs.student.$once('atguigu',this.getStudentName) //绑定自定义事件(一次性)}}
</script><style scoped>.app{background-color: gray;padding: 5px}
</style>

School.vue:

<template><div class="school"><h2>学校名称:{{name}}</h2><h2>学校地址:{{address}}</h2><button @click="sendSchoolName">把学校名给App</button></div>
</template><script>
export default {name:'School',props:['getSchoolName'],data(){return{name:'尚硅谷',address:'北京'}},methods:{sendSchoolName(){this.getSchoolName(this.name)}}
}
</script><style scoped>.school{background-color: skyblue;padding: 5px;}
</style>

Student.vue:

<template><div class="student"><h2>学生姓名:{{name}}</h2><h2>学生性别:{{sex}}</h2><h2>当前求和为:{{number}}</h2><button @click="add">点我number++</button><button @click="sendStudentName">把学生名给App</button><button @click="unbind">解绑atguigu事件</button><button @click="death">销毁当前Student组件的实例(vc)</button></div>
</template><script>export default {name:'Student',data(){return{name:'张三',sex:'男',number:0}},methods: {add(){console.log('add回调被调用了')this.number++},sendStudentName(){//触发Student组件实例上的aiguigu事件this.$emit('atguigu',this.name)// this.$emit('demo')this.$emit('click')},unbind(){this.$off('atguigu') //解绑一个自定义事件// this.$off(['atguigu','demo']) //解绑多个自定义事件// this.$off() //解绑所有的自定义事件},death(){this.$destroy() //销毁了当前Student组件的实例,销毁后所有Student实例的自定义事件全都不奏效}},}
</script><style lang="less" scoped>.student{background-color: orange;padding: 5px;margin-top: 30px}
</style>

全局事件总线(GlobalEventBus)

一种组件间通信的方式,适用于任意组件间通信

安装全局事件总线:

new Vue({......beforeCreate(){Vue.prototype.$bus = this //安装全局事件总线,$bus就是当前应用的vm}......
})

使用事件总线:

1.接收数据:A组件想接收数据,则在A组件中给$bus绑定自定义事件,事件的回调留在A组件本身

methods:{demo(data){......}
}
......
mounted(){this.$bus.$on('xxxx',this.demo)
}

2.提供数据:this.$bus.$emit('xxxx',数据)

说明:最好在beforeDestroy钩子中,用$off去解绑当前组件所用到的事件

main.js:

//引入Vue
import Vue from "vue";
//引入App
import App from "./App.vue";Vue.config.productionTip = false//创建vm
new Vue({el:'#app',render: h => h(App),beforeCreate(){Vue.prototype.$bus = this //安装全局事件总线}
})

App.vue:

<template><div class="app"><h1>{{msg}}</h1><School/><Student/></div>
</template>
<script>//引入School组件import School from './components/School.vue';import Student from './components/Student.vue';export default {name:'App',components:{School,Student},data() {return {msg:'你好'}}}
</script><style scoped>.app{background-color: gray;padding: 5px}
</style>

School.vue:

<template><div class="school"><h2>学校名称:{{name}}</h2><h2>学校地址:{{address}}</h2></div>
</template><script>
export default {name:'School',props:['getSchoolName'],data(){return{name:'尚硅谷',address:'北京'}},mounted(){console.log('School',this)this.$bus.$on('hello',(data)=>{console.log('我是School组件,收到了数据',data)})},beforeDestroy() {this.$bus.$off('hello')},
}
</script><style scoped>.school{background-color: skyblue;padding: 5px;}
</style>

Student.vue:

<template><div class="student"><h2>学生姓名:{{name}}</h2><h2>学生性别:{{sex}}</h2><button @click="sendStudentName">把学生名给School组件</button></div>
</template><script>export default {name:'Student',data(){return{name:'张三',sex:'男'}},methods:{sendStudentName(){this.$bus.$emit('hello',this.name)}}
}
</script><style lang="less" scoped>.student{background-color: orange;padding: 5px;margin-top: 30px}
</style>

TodoList事件总线

main.js:

//引入Vue
import Vue from "vue";
//引入App
import App from "./App.vue";Vue.config.productionTip = false//创建vm
new Vue({el:'#app',render: h => h(App),beforeCreate(){Vue.prototype.$bus = this}
})

App.vue:

<template><div id="root"><div class="todo-container"><div class="todo-wrap"><MyHeader @addTodo="addTodo"/><MyList :todos="todos"/><MyFooter :todos="todos" @checkAllTodo="checkAllTodo" @clearAllTodo="clearAllTodo"/></div></div></div>
</template>
<script>import MyHeader from './components/MyHeader.vue';import MyList from './components/MyList.vue';import MyFooter from './components/MyFooter.vue';export default {name:'App',components:{MyHeader,MyList,MyFooter},data(){return{//由于todos是MyHeader组件和MyFooter组件都在使用,所以放在App中(状态提升)todos:JSON.parse(localStorage.getItem('todos')) || []}},methods: {//添加一个todoaddTodo(todoObj){this.todos.unshift(todoObj)},//勾选or取消勾选一个todocheckedTodo(id){this.todos.forEach((todo)=>{if(todo.id == id) todo.done = !todo.done})},//删除一个tododeleteTodo(id){this.todos = this.todos.filter((todo)=>{return todo.id !== id})},//全选or取消全选checkAllTodo(done){this.todos.forEach((todo)=>{todo.done = done})},//清除所有已完成的todoclearAllTodo(){this.todos = this.todos.filter((todo)=>{return !todo.done})}},watch:{todos:{deep:true,handler(value){localStorage.setItem('todos',JSON.stringify(value))}}},mounted() {this.$bus.$on('checkedTodo',this.checkedTodo)this.$bus.$on('deleteTodo',this.deleteTodo)},beforeDestroy(){this.$bus.$off('checkedTodo')this.$bus.$off('deleteTodo')}}
</script><style>/*base*/body {background: #fff;}.btn {display: inline-block;padding: 4px 12px;margin-bottom: 0;font-size: 14px;line-height: 20px;text-align: center;vertical-align: middle;cursor: pointer;box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);border-radius: 4px;}.btn-danger {color: #fff;background-color: #da4f49;border: 1px solid #bd362f;}.btn-danger:hover {color: #fff;background-color: #bd362f;}.btn:focus {outline: none;}.todo-container {width: 600px;margin: 0 auto;}.todo-container .todo-wrap {padding: 10px;border: 1px solid #ddd;border-radius: 5px;}
</style>

MyFooter.vue:

<template><div class="todo-footer" v-show="total"><label><input type="checkbox" :checked="isAll" @change="checkAll"/></label><span><span>已完成{{doneTotal}}</span> / 全部{{total}}</span><button class="btn btn-danger" @click="clearAll">清除已完成任务</button></div>
</template><script>export default {name:'MyFooter',props:['todos'],computed:{//总数total(){return this.todos.length},//已完成数doneTotal(){//第一种方式let i = 0this.todos.forEach((todo) => {if(todo.done) i++})return i//第二种方式(使用reduce方法做条件统计)// return this.todos.reduce((pre,todo)=> pre + (todo.done ? 1 : 0),0)},//控制全选框isAll(){return this.doneTotal === this.total && this.total > 0}},methods: {checkAll(e){// this.checkAllTodo(e.target.checked)this.$emit('checkAllTodo',e.target.checked)},clearAll(){// this.clearAllTodo()this.$emit('clearAllTodo')}}}
</script><style scoped>/*footer*/.todo-footer {height: 40px;line-height: 40px;padding-left: 6px;margin-top: 5px;}.todo-footer label {display: inline-block;margin-right: 20px;cursor: pointer;}.todo-footer label input {position: relative;top: -1px;vertical-align: middle;margin-right: 5px;}.todo-footer button {float: right;margin-top: 5px;}
</style>

MyHeader.vue:

<template><div class="todo-header"><input type="text" placeholder="请输入你的任务名称,按回车键确认" v-model="title" @keyup.enter="add"/></div>
</template><script>import { nanoid } from 'nanoid';export default {name:'MyHeader',data() {return {title:''}},methods:{add(){//校验数据if(!this.title.trim()) return alert('输入不能为空')//将用户的输入包装成一个todo对象const todoObj = {id:nanoid(),title:this.title,done:false}//通知App组件添加一个todo对象this.$emit('addTodo',todoObj)//清空输入this.title = ''}}}
</script><style scoped>/*header*/.todo-header input {width: 560px;height: 28px;font-size: 14px;border: 1px solid #ccc;border-radius: 4px;padding: 4px 7px;}.todo-header input:focus {outline: none;border-color: rgba(82, 168, 236, 0.8);box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);}
</style>

MyItem.vue:

<template><li><label><input type="checkbox" :checked="todo.done" @change="handleCheck(todo.id)"/><!-- 如下代码也能实现功能,但不推荐,违背原则,修改了props --><!-- <input type="checkbox" v-model="todo.done"/> --><span>{{todo.title}}</span></label><button class="btn btn-danger" @click="handleDelete(todo.id)">删除</button></li>
</template><script>export default {name:'MyItem',//声明接收todo对象props:['todo'],methods: {//勾选or取消勾选handleCheck(id){//通知App组件将对应的todo对象的done值取反// this.checkedTodo(id)this.$bus.$emit('checkedTodo',id)},//删除handleDelete(id){if(confirm('确定删除吗?')){// this.deleteTodo(id)this.$bus.$emit('deleteTodo',id)}}}}
</script><style scoped>/*item*/li {list-style: none;height: 36px;line-height: 36px;padding: 0 5px;border-bottom: 1px solid #ddd;}li label {float: left;cursor: pointer;}li label li input {vertical-align: middle;margin-right: 6px;position: relative;top: -1px;}li button {float: right;display: none;margin-top: 3px;}li:before {content: initial;}li:last-child {border-bottom: none;}li:hover{background-color: #ddd;}li:hover button{display: block;}
</style>

MyList.vue:

<template><ul class="todo-main"><MyItem v-for="todoObj in todos" :key="todoObj.id" :todo="todoObj" /></ul>
</template><script>import MyItem from './MyItem.vue';export default {name:'MyList',components:{MyItem},props:['todos']}
</script><style scoped>/*main*/.todo-main {margin-left: 0px;border: 1px solid #ddd;border-radius: 2px;padding: 0px;}.todo-empty {height: 40px;line-height: 40px;border: 1px solid #ddd;border-radius: 2px;padding-left: 5px;margin-top: 10px;}
</style>

消息订阅与发布(pubsub)

一种组件间通信的方式,适用于任意组件间通信

 使用步骤:

1.安装pubsub:npm i pubsub-js

2.引入:import pubsub from 'pubsub-js'

3.接收数据:A组件想接收数据,则在A组件中订阅消息,订阅的回调留在A组件自身

methods:{demo(data){......}
}
......
mounted(){this.pid = pubsub.subscribe('xxx',this.demo) //订阅消息
}

4.提供数据:pubsub.publish('xxx',数据)

5.最好在beforeDestroy钩子中,用pubsub.unsubscribe(pid)去<span style="color:red">取消订阅</span>

main.js:

//引入Vue
import Vue from "vue";
//引入App
import App from "./App.vue";Vue.config.productionTip = false//创建vm
new Vue({el:'#app',render: h => h(App)
})

App.vue:

<template><div class="app"><h1>{{msg}}</h1><School/><Student/></div>
</template>
<script>//引入School组件import School from './components/School.vue';import Student from './components/Student.vue';export default {name:'App',components:{School,Student},data() {return {msg:'你好'}}}
</script><style scoped>.app{background-color: gray;padding: 5px}
</style>

School.vue:

<template><div class="school"><h2>学校名称:{{name}}</h2><h2>学校地址:{{address}}</h2></div>
</template><script>
import pubsub from 'pubsub-js'
export default {name:'School',props:['getSchoolName'],data(){return{name:'尚硅谷',address:'北京'}},methods: {demo(msgName,data){console.log('有人发布了hello消息,hello消息的回调执行了',data,this)}},mounted(){// console.log('School',this)// this.$bus.$on('hello',(data)=>{// console.log('我是School组件,收到了数据',data)// })this.pubId = pubsub.subscribe('hello',this.demo)},beforeDestroy() {// this.$bus.$off('hello')pubsub.unsubscribe(this.pubId)},
}
</script><style scoped>.school{background-color: skyblue;padding: 5px;}
</style>

Student.vue:

<template><div class="student"><h2>学生姓名:{{name}}</h2><h2>学生性别:{{sex}}</h2><button @click="sendStudentName">把学生名给School组件</button></div>
</template><script>
import pubsub from 'pubsub-js'
export default {name:'Student',data(){return{name:'张三',sex:'男'}},methods:{sendStudentName(){// this.$bus.$emit('hello',this.name)pubsub.publish('hello',666)}}
}
</script><style lang="less" scoped>.student{background-color: orange;padding: 5px;margin-top: 30px}
</style>

TodoList_pubsub

main.js:

//引入Vue
import Vue from "vue";
//引入App
import App from "./App.vue";Vue.config.productionTip = false//创建vm
new Vue({el:'#app',render: h => h(App),beforeCreate(){Vue.prototype.$bus = this}
})

App.vue:

<template><div id="root"><div class="todo-container"><div class="todo-wrap"><MyHeader @addTodo="addTodo"/><MyList :todos="todos"/><MyFooter :todos="todos" @checkAllTodo="checkAllTodo" @clearAllTodo="clearAllTodo"/></div></div></div>
</template>
<script>import pubsub from 'pubsub-js'import MyHeader from './components/MyHeader.vue';import MyList from './components/MyList.vue';import MyFooter from './components/MyFooter.vue';export default {name:'App',components:{MyHeader,MyList,MyFooter},data(){return{//由于todos是MyHeader组件和MyFooter组件都在使用,所以放在App中(状态提升)todos:JSON.parse(localStorage.getItem('todos')) || []}},methods: {//添加一个todoaddTodo(todoObj){this.todos.unshift(todoObj)},//勾选or取消勾选一个todocheckedTodo(id){this.todos.forEach((todo)=>{if(todo.id == id) todo.done = !todo.done})},//删除一个tododeleteTodo(_,id){this.todos = this.todos.filter((todo)=>{return todo.id !== id})},//全选or取消全选checkAllTodo(done){this.todos.forEach((todo)=>{todo.done = done})},//清除所有已完成的todoclearAllTodo(){this.todos = this.todos.filter((todo)=>{return !todo.done})}},watch:{todos:{deep:true,handler(value){localStorage.setItem('todos',JSON.stringify(value))}}},mounted() {this.$bus.$on('checkedTodo',this.checkedTodo)this.pubId = pubsub.subscribe('deleteTodo',this.deleteTodo)},beforeDestroy(){this.$bus.$off('checkedTodo')pubsub.unsubscribe(this.pubId)}}
</script><style>/*base*/body {background: #fff;}.btn {display: inline-block;padding: 4px 12px;margin-bottom: 0;font-size: 14px;line-height: 20px;text-align: center;vertical-align: middle;cursor: pointer;box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);border-radius: 4px;}.btn-danger {color: #fff;background-color: #da4f49;border: 1px solid #bd362f;}.btn-danger:hover {color: #fff;background-color: #bd362f;}.btn:focus {outline: none;}.todo-container {width: 600px;margin: 0 auto;}.todo-container .todo-wrap {padding: 10px;border: 1px solid #ddd;border-radius: 5px;}
</style>

MyFooter.vue:

<template><div class="todo-footer" v-show="total"><label><input type="checkbox" :checked="isAll" @change="checkAll"/></label><span><span>已完成{{doneTotal}}</span> / 全部{{total}}</span><button class="btn btn-danger" @click="clearAll">清除已完成任务</button></div>
</template><script>export default {name:'MyFooter',props:['todos'],computed:{//总数total(){return this.todos.length},//已完成数doneTotal(){//第一种方式let i = 0this.todos.forEach((todo) => {if(todo.done) i++})return i//第二种方式(使用reduce方法做条件统计)// return this.todos.reduce((pre,todo)=> pre + (todo.done ? 1 : 0),0)},//控制全选框isAll(){return this.doneTotal === this.total && this.total > 0}},methods: {checkAll(e){// this.checkAllTodo(e.target.checked)this.$emit('checkAllTodo',e.target.checked)},clearAll(){// this.clearAllTodo()this.$emit('clearAllTodo')}}}
</script><style scoped>/*footer*/.todo-footer {height: 40px;line-height: 40px;padding-left: 6px;margin-top: 5px;}.todo-footer label {display: inline-block;margin-right: 20px;cursor: pointer;}.todo-footer label input {position: relative;top: -1px;vertical-align: middle;margin-right: 5px;}.todo-footer button {float: right;margin-top: 5px;}
</style>

MyHeader.vue:

<template><div class="todo-header"><input type="text" placeholder="请输入你的任务名称,按回车键确认" v-model="title" @keyup.enter="add"/></div>
</template><script>import { nanoid } from 'nanoid';export default {name:'MyHeader',data() {return {title:''}},methods:{add(){//校验数据if(!this.title.trim()) return alert('输入不能为空')//将用户的输入包装成一个todo对象const todoObj = {id:nanoid(),title:this.title,done:false}//通知App组件添加一个todo对象this.$emit('addTodo',todoObj)//清空输入this.title = ''}}}
</script><style scoped>/*header*/.todo-header input {width: 560px;height: 28px;font-size: 14px;border: 1px solid #ccc;border-radius: 4px;padding: 4px 7px;}.todo-header input:focus {outline: none;border-color: rgba(82, 168, 236, 0.8);box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);}
</style>

MyItem.vue:

<template><li><label><input type="checkbox" :checked="todo.done" @change="handleCheck(todo.id)"/><!-- 如下代码也能实现功能,但不推荐,违背原则,修改了props --><!-- <input type="checkbox" v-model="todo.done"/> --><span>{{todo.title}}</span></label><button class="btn btn-danger" @click="handleDelete(todo.id)">删除</button></li>
</template><script>import pubsub from 'pubsub-js'export default {name:'MyItem',//声明接收todo对象props:['todo'],methods: {//勾选or取消勾选handleCheck(id){//通知App组件将对应的todo对象的done值取反// this.checkedTodo(id)this.$bus.$emit('checkedTodo',id)},//删除handleDelete(id){if(confirm('确定删除吗?')){// this.deleteTodo(id)// this.$bus.$emit('deleteTodo',id)pubsub.publish('deleteTodo',id)}}}}
</script><style scoped>/*item*/li {list-style: none;height: 36px;line-height: 36px;padding: 0 5px;border-bottom: 1px solid #ddd;}li label {float: left;cursor: pointer;}li label li input {vertical-align: middle;margin-right: 6px;position: relative;top: -1px;}li button {float: right;display: none;margin-top: 3px;}li:before {content: initial;}li:last-child {border-bottom: none;}li:hover{background-color: #ddd;}li:hover button{display: block;}
</style>

MyList.vue:

<template><ul class="todo-main"><MyItem v-for="todoObj in todos" :key="todoObj.id" :todo="todoObj" /></ul>
</template><script>import MyItem from './MyItem.vue';export default {name:'MyList',components:{MyItem},props:['todos']}
</script><style scoped>/*main*/.todo-main {margin-left: 0px;border: 1px solid #ddd;border-radius: 2px;padding: 0px;}.todo-empty {height: 40px;line-height: 40px;border: 1px solid #ddd;border-radius: 2px;padding-left: 5px;margin-top: 10px;}
</style>

nextTick

语法:this.$nextTick(回调函数)

作用:在下一次DOM更新结束后执行其指定的回调

什么时候用:当改变数据后,要基于更新后的新DOM进行某些操作时,要在nextTick所指定的回调函数中执行

main.js:

//引入Vue
import Vue from "vue";
//引入App
import App from "./App.vue";Vue.config.productionTip = false//创建vm
new Vue({el:'#app',render: h => h(App),beforeCreate(){Vue.prototype.$bus = this}
})

App.vue:

<template><div id="root"><div class="todo-container"><div class="todo-wrap"><MyHeader @addTodo="addTodo"/><MyList :todos="todos"/><MyFooter :todos="todos" @checkAllTodo="checkAllTodo" @clearAllTodo="clearAllTodo"/></div></div></div>
</template>
<script>import pubsub from 'pubsub-js'import MyHeader from './components/MyHeader.vue';import MyList from './components/MyList.vue';import MyFooter from './components/MyFooter.vue';export default {name:'App',components:{MyHeader,MyList,MyFooter},data(){return{//由于todos是MyHeader组件和MyFooter组件都在使用,所以放在App中(状态提升)todos:JSON.parse(localStorage.getItem('todos')) || []}},methods: {//添加一个todoaddTodo(todoObj){this.todos.unshift(todoObj)},//勾选or取消勾选一个todocheckedTodo(id){this.todos.forEach((todo)=>{if(todo.id == id) todo.done = !todo.done})},//删除一个tododeleteTodo(_,id){this.todos = this.todos.filter((todo)=>{return todo.id !== id})},//全选or取消全选checkAllTodo(done){this.todos.forEach((todo)=>{todo.done = done})},//清除所有已完成的todoclearAllTodo(){this.todos = this.todos.filter((todo)=>{return !todo.done})},//更新一个todoupdateTodo(id,title){this.todos.forEach((todo)=>{if(todo.id == id) todo.title = title})}},watch:{todos:{deep:true,handler(value){localStorage.setItem('todos',JSON.stringify(value))}}},mounted() {this.$bus.$on('checkedTodo',this.checkedTodo)this.pubId = pubsub.subscribe('deleteTodo',this.deleteTodo)this.$bus.$on('updateTodo',this.updateTodo)},beforeDestroy(){this.$bus.$off('checkedTodo')pubsub.unsubscribe(this.pubId)this.$bus.$off('updateTodo')}}
</script><style>/*base*/body {background: #fff;}.btn {display: inline-block;padding: 4px 12px;margin-bottom: 0;font-size: 14px;line-height: 20px;text-align: center;vertical-align: middle;cursor: pointer;box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);border-radius: 4px;}.btn-danger {color: #fff;background-color: #da4f49;border: 1px solid #bd362f;}.btn-edit {color: #fff;background-color: skyblue;border: 1px solid rgb(102, 159, 181);margin-right: 5px}.btn-edit:hover {color: #fff;background-color: rgb(102, 159, 181);}.btn-danger:hover {color: #fff;background-color: #bd362f;}.btn:focus {outline: none;}.todo-container {width: 600px;margin: 0 auto;}.todo-container .todo-wrap {padding: 10px;border: 1px solid #ddd;border-radius: 5px;}
</style>

MyFooter.vue:

<template><div class="todo-footer" v-show="total"><label><input type="checkbox" :checked="isAll" @change="checkAll"/></label><span><span>已完成{{doneTotal}}</span> / 全部{{total}}</span><button class="btn btn-danger" @click="clearAll">清除已完成任务</button></div>
</template><script>export default {name:'MyFooter',props:['todos'],computed:{//总数total(){return this.todos.length},//已完成数doneTotal(){//第一种方式let i = 0this.todos.forEach((todo) => {if(todo.done) i++})return i//第二种方式(使用reduce方法做条件统计)// return this.todos.reduce((pre,todo)=> pre + (todo.done ? 1 : 0),0)},//控制全选框isAll(){return this.doneTotal === this.total && this.total > 0}},methods: {checkAll(e){// this.checkAllTodo(e.target.checked)this.$emit('checkAllTodo',e.target.checked)},clearAll(){// this.clearAllTodo()this.$emit('clearAllTodo')}}}
</script><style scoped>/*footer*/.todo-footer {height: 40px;line-height: 40px;padding-left: 6px;margin-top: 5px;}.todo-footer label {display: inline-block;margin-right: 20px;cursor: pointer;}.todo-footer label input {position: relative;top: -1px;vertical-align: middle;margin-right: 5px;}.todo-footer button {float: right;margin-top: 5px;}
</style>

MyHeader.vue:

<template><div class="todo-header"><input type="text" placeholder="请输入你的任务名称,按回车键确认" v-model="title" @keyup.enter="add"/></div>
</template><script>import { nanoid } from 'nanoid';export default {name:'MyHeader',data() {return {title:''}},methods:{add(){//校验数据if(!this.title.trim()) return alert('输入不能为空')//将用户的输入包装成一个todo对象const todoObj = {id:nanoid(),title:this.title,done:false}//通知App组件添加一个todo对象this.$emit('addTodo',todoObj)//清空输入this.title = ''}}}
</script><style scoped>/*header*/.todo-header input {width: 560px;height: 28px;font-size: 14px;border: 1px solid #ccc;border-radius: 4px;padding: 4px 7px;}.todo-header input:focus {outline: none;border-color: rgba(82, 168, 236, 0.8);box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);}
</style>

MyItem.vue:

<template><li><label><input type="checkbox" :checked="todo.done" @change="handleCheck(todo.id)"/><!-- 如下代码也能实现功能,但不推荐,违背原则,修改了props --><!-- <input type="checkbox" v-model="todo.done"/> --><span v-show="!todo.isEdit">{{todo.title}}</span><input type="text" v-show="todo.isEdit" :value="todo.title" @blur="handleBlur(todo,$event)"ref="inputTitle"></label><button class="btn btn-danger" @click="handleDelete(todo.id)">删除</button><button class="btn btn-edit" @click="handleEdit(todo)" v-show="!todo.isEdit">编辑</button></li>
</template><script>import pubsub from 'pubsub-js'export default {name:'MyItem',//声明接收todo对象props:['todo'],methods: {//勾选or取消勾选handleCheck(id){//通知App组件将对应的todo对象的done值取反// this.checkedTodo(id)this.$bus.$emit('checkedTodo',id)},//删除handleDelete(id){if(confirm('确定删除吗?')){// this.deleteTodo(id)// this.$bus.$emit('deleteTodo',id)pubsub.publish('deleteTodo',id)}},//编辑handleEdit(todo){if(todo.hasOwnProperty('isEdit')){todo.isEdit = true}else{this.$set(todo,'isEdit',true)}this.$nextTick(function(){this.$refs.inputTitle.focus()})},//失去焦点回调(真正执行修改逻辑)handleBlur(todo,e){todo.isEdit = falseif(!e.target.value.trim()) return alert('输入不能为空!')this.$bus.$emit('updateTodo',todo.id,e.target.value)}}}
</script><style scoped>/*item*/li {list-style: none;height: 36px;line-height: 36px;padding: 0 5px;border-bottom: 1px solid #ddd;}li label {float: left;cursor: pointer;}li label li input {vertical-align: middle;margin-right: 6px;position: relative;top: -1px;}li button {float: right;display: none;margin-top: 3px;}li:before {content: initial;}li:last-child {border-bottom: none;}li:hover{background-color: #ddd;}li:hover button{display: block;}
</style>

MyList.vue:

<template><ul class="todo-main"><MyItem v-for="todoObj in todos" :key="todoObj.id" :todo="todoObj" /></ul>
</template><script>import MyItem from './MyItem.vue';export default {name:'MyList',components:{MyItem},props:['todos']}
</script><style scoped>/*main*/.todo-main {margin-left: 0px;border: 1px solid #ddd;border-radius: 2px;padding: 0px;}.todo-empty {height: 40px;line-height: 40px;border: 1px solid #ddd;border-radius: 2px;padding-left: 5px;margin-top: 10px;}
</style>

Vue封装的过渡与动画

作用:在插入、更新或移除DOM元素时,在合适的时候给元素添加样式类名

图示:

写法:

1.准备好样式:

  (1)元素进入的样式:

    v-enter:进入的起点

    v-enter-active:进入过程中

    v-enter-to:进入的终点

  (2)元素离开的样式:

    v-leave:离开的起点

    v-leave-active:离开过程中

    v-leave-to:离开的终点

2.使用<transition>包裹要过渡的元素,并配置name属性:

<transition name="hello"><h1 v-show="isShow">你好</h1>
</transition>

3.备注:若有多个元素需要过渡,则需要使用:<transition-group>,且每个元素都要指定key值

main.js:

//引入Vue
import Vue from "vue";
//引入App
import App from "./App.vue";Vue.config.productionTip = false//创建vm
new Vue({el:'#app',render: h => h(App),beforeCreate(){Vue.prototype.$bus = this}
})

App.vue:

<template><div><Demo/><Demo2/><Demo3/>      </div>
</template>
<script>import Demo from './components/Demo.vue'import Demo2 from './components/Demo2.vue'import Demo3 from './components/Demo3.vue'export default {name:'App',components:{Demo,Demo2,Demo3}}
</script>

Demo.vue:

<template><div><button @click="isShow = !isShow">显示/隐藏</button><transition name="hello" appear><h1 v-show="isShow">你好</h1></transition></div>
</template>
<script>export default {name:'Demo',data() {return {isShow:true}}}
</script><style>h1{background-color: orange;}.hello-enter-active{animation: atguigu 0.5s linear;}.hello-leave-active{animation: atguigu 0.5s linear reverse;}@keyframes atguigu {from{transform: translateX(-100%);}to{transform: translateX(0px);}}
</style>

Demo2.vue:

<template><div><button @click="isShow = !isShow">显示/隐藏</button><transition-group name="hello" appear><h1 v-show="!isShow" key="1">你好</h1><h1 v-show="isShow" key="2">尚硅谷</h1></transition-group></div>
</template>
<script>export default {name:'Demo',data() {return {isShow:true}}}
</script><style scoped>h1{background-color: orange;}/* 进入的起点,离开的终点 */.hello-enter,.hello-leave-to{transform: translateX(-100%);}.hello-enter-active,.hello-leave-active{transform: 0.5s linear;}/* 进入的终点,离开的起点 */.hello-enter-to,.hello-leave{transform: translateX(0);}</style>

Demo3.vue:

<template><div><button @click="isShow = !isShow">显示/隐藏</button><transition-group name="animate__animated animate__bounce" appearenter-active-class="animate__swing"leave-active-class="animate__backOutUp"><h1 v-show="!isShow" key="1">你好</h1><h1 v-show="isShow" key="2">尚硅谷</h1></transition-group></div>
</template>
<script>import 'animate.css'export default {name:'Demo',data() {return {isShow:true}}}
</script><style scoped>h1{background-color: orange;}</style>

TodoList_动画

main.js:

//引入Vue
import Vue from "vue";
//引入App
import App from "./App.vue";Vue.config.productionTip = false//创建vm
new Vue({el:'#app',render: h => h(App),beforeCreate(){Vue.prototype.$bus = this}
})

App.vue:

<template><div id="root"><div class="todo-container"><div class="todo-wrap"><MyHeader @addTodo="addTodo"/><MyList :todos="todos"/><MyFooter :todos="todos" @checkAllTodo="checkAllTodo" @clearAllTodo="clearAllTodo"/></div></div></div>
</template>
<script>import pubsub from 'pubsub-js'import MyHeader from './components/MyHeader.vue';import MyList from './components/MyList.vue';import MyFooter from './components/MyFooter.vue';export default {name:'App',components:{MyHeader,MyList,MyFooter},data(){return{//由于todos是MyHeader组件和MyFooter组件都在使用,所以放在App中(状态提升)todos:JSON.parse(localStorage.getItem('todos')) || []}},methods: {//添加一个todoaddTodo(todoObj){this.todos.unshift(todoObj)},//勾选or取消勾选一个todocheckedTodo(id){this.todos.forEach((todo)=>{if(todo.id == id) todo.done = !todo.done})},//删除一个tododeleteTodo(_,id){this.todos = this.todos.filter((todo)=>{return todo.id !== id})},//全选or取消全选checkAllTodo(done){this.todos.forEach((todo)=>{todo.done = done})},//清除所有已完成的todoclearAllTodo(){this.todos = this.todos.filter((todo)=>{return !todo.done})},//更新一个todoupdateTodo(id,title){this.todos.forEach((todo)=>{if(todo.id == id) todo.title = title})}},watch:{todos:{deep:true,handler(value){localStorage.setItem('todos',JSON.stringify(value))}}},mounted() {this.$bus.$on('checkedTodo',this.checkedTodo)this.pubId = pubsub.subscribe('deleteTodo',this.deleteTodo)this.$bus.$on('updateTodo',this.updateTodo)},beforeDestroy(){this.$bus.$off('checkedTodo')pubsub.unsubscribe(this.pubId)this.$bus.$off('updateTodo')}}
</script><style>/*base*/body {background: #fff;}.btn {display: inline-block;padding: 4px 12px;margin-bottom: 0;font-size: 14px;line-height: 20px;text-align: center;vertical-align: middle;cursor: pointer;box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);border-radius: 4px;}.btn-danger {color: #fff;background-color: #da4f49;border: 1px solid #bd362f;}.btn-edit {color: #fff;background-color: skyblue;border: 1px solid rgb(102, 159, 181);margin-right: 5px}.btn-edit:hover {color: #fff;background-color: rgb(102, 159, 181);}.btn-danger:hover {color: #fff;background-color: #bd362f;}.btn:focus {outline: none;}.todo-container {width: 600px;margin: 0 auto;}.todo-container .todo-wrap {padding: 10px;border: 1px solid #ddd;border-radius: 5px;}
</style>

MyFooter.vue:

<template><div class="todo-footer" v-show="total"><label><input type="checkbox" :checked="isAll" @change="checkAll"/></label><span><span>已完成{{doneTotal}}</span> / 全部{{total}}</span><button class="btn btn-danger" @click="clearAll">清除已完成任务</button></div>
</template><script>export default {name:'MyFooter',props:['todos'],computed:{//总数total(){return this.todos.length},//已完成数doneTotal(){//第一种方式let i = 0this.todos.forEach((todo) => {if(todo.done) i++})return i//第二种方式(使用reduce方法做条件统计)// return this.todos.reduce((pre,todo)=> pre + (todo.done ? 1 : 0),0)},//控制全选框isAll(){return this.doneTotal === this.total && this.total > 0}},methods: {checkAll(e){// this.checkAllTodo(e.target.checked)this.$emit('checkAllTodo',e.target.checked)},clearAll(){// this.clearAllTodo()this.$emit('clearAllTodo')}}}
</script><style scoped>/*footer*/.todo-footer {height: 40px;line-height: 40px;padding-left: 6px;margin-top: 5px;}.todo-footer label {display: inline-block;margin-right: 20px;cursor: pointer;}.todo-footer label input {position: relative;top: -1px;vertical-align: middle;margin-right: 5px;}.todo-footer button {float: right;margin-top: 5px;}
</style>

MyHeader.vue:

<template><div class="todo-header"><input type="text" placeholder="请输入你的任务名称,按回车键确认" v-model="title" @keyup.enter="add"/></div>
</template><script>import { nanoid } from 'nanoid';export default {name:'MyHeader',data() {return {title:''}},methods:{add(){//校验数据if(!this.title.trim()) return alert('输入不能为空')//将用户的输入包装成一个todo对象const todoObj = {id:nanoid(),title:this.title,done:false}//通知App组件添加一个todo对象this.$emit('addTodo',todoObj)//清空输入this.title = ''}}}
</script><style scoped>/*header*/.todo-header input {width: 560px;height: 28px;font-size: 14px;border: 1px solid #ccc;border-radius: 4px;padding: 4px 7px;}.todo-header input:focus {outline: none;border-color: rgba(82, 168, 236, 0.8);box-shadow: inset 0 1px 1px rgba(0, 0, 0, 0.075), 0 0 8px rgba(82, 168, 236, 0.6);}
</style>

MyItem.vue:

<template><transition name="todo" appear><li><label><input type="checkbox" :checked="todo.done" @change="handleCheck(todo.id)"/><!-- 如下代码也能实现功能,但不推荐,违背原则,修改了props --><!-- <input type="checkbox" v-model="todo.done"/> --><span v-show="!todo.isEdit">{{todo.title}}</span><input type="text" v-show="todo.isEdit" :value="todo.title" @blur="handleBlur(todo,$event)"ref="inputTitle"></label><button class="btn btn-danger" @click="handleDelete(todo.id)">删除</button><button class="btn btn-edit" @click="handleEdit(todo)" v-show="!todo.isEdit">编辑</button></li></transition>
</template><script>import pubsub from 'pubsub-js'export default {name:'MyItem',//声明接收todo对象props:['todo'],methods: {//勾选or取消勾选handleCheck(id){//通知App组件将对应的todo对象的done值取反// this.checkedTodo(id)this.$bus.$emit('checkedTodo',id)},//删除handleDelete(id){if(confirm('确定删除吗?')){// this.deleteTodo(id)// this.$bus.$emit('deleteTodo',id)pubsub.publish('deleteTodo',id)}},//编辑handleEdit(todo){if(todo.hasOwnProperty('isEdit')){todo.isEdit = true}else{this.$set(todo,'isEdit',true)}this.$nextTick(function(){this.$refs.inputTitle.focus()})},//失去焦点回调(真正执行修改逻辑)handleBlur(todo,e){todo.isEdit = falseif(!e.target.value.trim()) return alert('输入不能为空!')this.$bus.$emit('updateTodo',todo.id,e.target.value)}}}
</script><style scoped>/*item*/li {list-style: none;height: 36px;line-height: 36px;padding: 0 5px;border-bottom: 1px solid #ddd;}li label {float: left;cursor: pointer;}li label li input {vertical-align: middle;margin-right: 6px;position: relative;top: -1px;}li button {float: right;display: none;margin-top: 3px;}li:before {content: initial;}li:last-child {border-bottom: none;}li:hover{background-color: #ddd;}li:hover button{display: block;}.todo-enter-active{animation: atguigu 0.5s linear;}.todo-leave-active{animation: atguigu 0.5s linear reverse;}@keyframes atguigu {from{transform: translateX(100%);}to{transform: translateX(0px);}}
</style>

MyList.vue:

<template><ul class="todo-main"><MyItem v-for="todoObj in todos" :key="todoObj.id" :todo="todoObj" /></ul>
</template><script>import MyItem from './MyItem.vue';export default {name:'MyList',components:{MyItem},props:['todos']}
</script><style scoped>/*main*/.todo-main {margin-left: 0px;border: 1px solid #ddd;border-radius: 2px;padding: 0px;}.todo-empty {height: 40px;line-height: 40px;border: 1px solid #ddd;border-radius: 2px;padding-left: 5px;margin-top: 10px;}
</style>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/pingmian/50942.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【初阶数据结构篇】栈的实现(赋源码)

文章目录 栈1 代码位置2 概念与结构1.1概念1.2结构 2 栈的实现2.1 栈的初始化和销毁2.1.1 初始化2.1.2 销毁 2.2 栈顶插入和删除数据2.2.1 栈顶插入数据&#xff08;压栈&#xff09;2.2.2 栈顶删除数据&#xff08;出栈&#xff09; 2.3 返回栈顶数据2.4 返回栈的有效数据个数…

嵌入式人工智能(31-基于树莓派4B的气压传感器-BMP280)

1、气压传感器 气压传感器&#xff08;Pressure Sensor&#xff09;是一种用于测量气体压力的装置。它可以将气体压力转换为电信号输出&#xff0c;进而实现对气体压力的监测和控制。气压传感器广泛应用于工业自动化、气象观测、建筑监测、航空航天等领域。 气压传感器的工作…

未来的智能农业:智能合约如何提升农业生产效率和可持续性

随着全球人口的增长和资源的有限性&#xff0c;农业生产面临着越来越大的挑战。如何在提高生产效率的同时保障可持续发展成为全球农业发展的关键问题。智能合约作为一种基于区块链技术的自动化执行合约&#xff0c;正在逐渐应用于农业领域&#xff0c;为农业生产带来了新的机遇…

Redis:RDB持久化

1. 简介 实现类似照片记录效果的方式&#xff0c;就是把某一时刻的数据和状态以文件的形式写到磁盘上&#xff0c;也就是 快照。这样一来即使故障宕机&#xff0c;快照文件也不会丢失&#xff0c;数据的可靠性也就得到了保证。 这个快照文件就称为RDB文件(dump.rdb)&#xff0c…

从代码层面熟悉UniAD,开始学习了解端到端整体架构

0. 简介 最近端到端已经是越来越火了&#xff0c;以UniAD为代表的很多工作不断地在不断刷新端到端的指标&#xff0c;比如最近SparseDrive又重新刷新了所有任务的指标。在端到端火热起来之前&#xff0c;成熟的模块化自动驾驶系统被分解为不同的独立任务&#xff0c;例如感知、…

数据倾斜优化思路实践

数据倾斜&#xff0c;顾名思义&#xff0c;就是在计算过程中数据分散度不够&#xff0c;导致某个节点数据过于集中&#xff0c;从而导致任务执行效率大大降低。参照对比下MR的整体流程和ODPS&#xff0c;整体结合理解数据倾斜发生的几个生命周期的节点&#xff0c;如下图&#…

WordPress设置固定连接后提示404

WordPress设置固定链接后出现404错误通常是因为服务器的伪静态规则没有正确设置。以下是几种常见的服务器环境下的解决方案&#xff1a; 宝塔面板&#xff1a;如果服务器安装了宝塔面板&#xff0c;可以在宝塔面板中选择对应的WordPress伪静态规则并保存设置 。 Apache服务器&a…

Linux——DNS服务搭建

&#xff08;一&#xff09;搭建nginx 1.首先布置基本环境 要求能够ping通外网&#xff0c;有yum源 2.安装nginx yum -y install nginx 然后查看验证 3.修改网页配置文件 修改文件&#xff0c;任意编写内容&#xff0c;然后去物理机测试 &#xff08;二&#xff09;创建一…

C++知识点总结:2.类和对象(自用)

类和对象 1. 类和对象的关系2. 对象指针3. 在堆上创建对象4. 成员访问限定符5. 名字编码&#xff08;Name Mangling&#xff09;6.构造函数7.构造函数的重载8.初始化列表8. 成员变量初始化的顺序&#xff08;通过初始化列表&#xff09;9. 初始化 const 成员变量10. 析构函数11…

【机器学习】pytorch 常用函数解析

目录 一、基本函数介绍 1.1 nn.Module 类 1.2 nn.Embedding 1.3 nn.LSTM 1.4 nn.Linear 1.5 nn.CrossEntropyLoss 1.6 torch.save 1.7 torch.load 1.8 nn.functional 1.9 nn.functional.softmax 本文主要对 pytorch 中用到的函数进行介绍&#xff0c;本文会不断更新~…

C语言内存函数精讲

目录 引言 1.内存分配函数malloc 2.内存释放函数free 3.内存拷贝函数memcpy 4.内存移动函数memmove 5.内存设置函数memset 6.内存比较函数memcmp 总结 引言 在C语言编程中&#xff0c;内存管理是核心技能之一。C语言提供了一系列内存操作函数&#xff0c;这些函数在动…

jmeter-beanshell学习-try处理异常

有时候代码执行过程中&#xff0c;出现一些不能处理的情况&#xff0c;就会报错&#xff0c;还影响之后的代码执行&#xff0c;就需要跳过异常。 上面这情况报错了&#xff0c;还影响了下面的打印。beanshell用try和catch处理异常&#xff0c;下面是try的用法&#xff0c;和if有…

技术守护尊严||Chat GPT在抵抗性骚扰的作用分析

就在本周&#xff0c;中国人民大学女博士实名举报导师性骚扰的事情&#xff0c;引发全网关注&#xff01; 性骚扰&#xff0c;无论在线上还是线下&#xff0c;无论在职场还是校园&#xff0c;都是对个人尊严与权益的严重侵犯。 幸运的是&#xff0c;随着人工智能技术的飞速发…

优化冗余代码:提升前端项目开发效率的实用方法

目录 前言代码复用与组件化模块化开发与代码分割工具辅助与自动化结束语 前言 在前端开发中&#xff0c;我们常常会遇到代码冗余的问题&#xff0c;这不仅增加了代码量&#xff0c;还影响了项目的可维护性和开发效率。还有就是有时候会接到紧急业务需求&#xff0c;要求立马完…

[网络通信原理]——TCP/IP模型—网络层

网络层 网络层概述 网络层位于OSI模型的第三层&#xff0c;它定义网络设备的逻辑地址&#xff0c;也就是我们说的IP地址&#xff0c;能够在不同的网段之间选择最佳数据转发路径。在网络层中有许多协议&#xff0c;其中主要的协议是IP协议。 IP数据包格式 IP数据报是可变长度…

《最新出炉》系列入门篇-Python+Playwright自动化测试-55- 上传文件 (非input控件)- 中篇

软件测试微信群&#xff1a;https://bbs.csdn.net/topics/618423372 有兴趣的可以扫码加入 1.简介 在实际工作中&#xff0c;我们进行web自动化的时候&#xff0c;文件上传是很常见的操作&#xff0c;例如上传用户头像&#xff0c;上传身份证信息等。所以宏哥打算按上传文件…

Java从入门到精通(十二)~ 动态代理

晚上好&#xff0c;愿这深深的夜色给你带来安宁&#xff0c;让温馨的夜晚抚平你一天的疲惫&#xff0c;美好的梦想在这个寂静的夜晚悄悄成长。 文章目录 目录 前言 主要作用和功能&#xff1a; 应用场景&#xff1a; 二、代理概念 1.静态代理 2.动态代理 2.1 概念介绍 …

网址导航系统PHP源码分享

1、采用光年全新v5模板开发后台 2、后台内置8款主题色&#xff0c;分别是简约白、炫光绿、渐变紫、活力橙、少女粉、少女紫、科幻蓝、护眼黑 3、可管理无数引导页主题并且主题内可以进行不同的自定义设置&#xff0c;目前内置16套主题 持续增加中… 4、可单独开发各种插件&a…

OSPF Type2 Message / DBD Packet (Database Descriptor)

注&#xff1a;机翻&#xff0c;未校对。 OSPF Type2 Message / DBD Packet (Database Descriptor) DBD (Database Description or Type2 OSPF Packet) is a sort of summary of the OSPF Database in a router. DBD is used to check if the LSDB between 2 routers is the s…

Linux---make/makefile工具

目录 基本了解 makefile基础语法 依赖关系 依赖方法 makefile文件内容格式 make执行机制 补充知识 机制解释 PHONY关键字 makefile补充语法 基本了解 在Linux中&#xff0c;make/makefile是项目自动化构建工具。如果我们没有make/makefile&#xff0c;那我们要编译一…