<template>
<div class="base-one">
BaseOne
div>
template>
<script>
export default {
}
script>
<style scoped>
div {
border: 3px solid blue;
margin: 30px;
}
style>
<template>
<div class="base-count">
<button @click="count--">-button>
<span>{{ count }}span>
<button @click="count++">+button>
div>
template>
<script>
export default {
data() {
return {
count: 100,
}
},
}
script>
<style>
.base-count {
margin: 20px;
}
style>
props, $emit
provide, inject
或eventbus
vuex
<template>
<div class="app" style="border: 3px solid #000; margin: 10px">
我是APP组件
<Son :title="myTitle">Son>
div>
template>
<script>
import Son from "./components/Son.vue"
export default {
name: "App",
components: {
Son,
},
data() {
return {
myTitle: "学前端,就来黑马程序员",
}
},
}
script>
<style>
style>
<template>
<div class="son" style="border:3px solid #000;margin:10px">
我是Son组件 {{title}}
div>
template>
<script>
export default {
name: 'Son-Child',
// 2.通过props来接受
props: ['title']
}
script>
<style>
style>
<template>
<div class="app" style="border: 3px solid #000; margin: 10px">
我是APP组件
<Son :title="myTitle" @changeTitle="handleChange">Son>
div>
template>
<script>
import Son from "./components/Son.vue"
export default {
name: "App",
components: {
Son,
},
data() {
return {
myTitle: "学前端,就来黑马程序员",
}
},
methods: {
// 3. 提供处理函数, 提供逻辑
handleChange(newTitle){
this.myTitle = newTitle
}
}
}
script>
<style>
style>
<template>
<div class="son" style="border:3px solid #000;margin:10px">
我是Son组件 {{title}}
<button @click="changeFn">修改titlebutton>
div>
template>
<script>
export default {
name: 'Son-Child',
props: ['title'],
methods: {
changeFn(){
// 1. 通过$emit, 向父组件发送消息通知
this.$emit('changeTitle', "传智教育")
}
}
}
script>
<style>
style>
父组件
<template>
<div class="app">
<UserInfo
:username="username"
:age="age"
:isSingle="isSingle"
:car="car"
:hobby="hobby"
>UserInfo>
div>
template>
<script>
import UserInfo from './components/UserInfo.vue'
export default {
data() {
return {
username: '小帅',
age: 28,
isSingle: true,
car: {
brand: '宝马',
},
hobby: ['篮球', '足球', '羽毛球'],
}
},
components: {
UserInfo,
},
}
script>
<style>
style>
子组件
<template>
<div class="userinfo">
<h3>我是个人信息组件h3>
<div>姓名:{{username}} div>
<div>年龄:{{age}} div>
<div>是否单身:{{isSingle ? '是' : '否'}} div>
<div>座驾:{{car.brand}} div>
<div>兴趣爱好:{{hobby.join(', ')}} div>
div>
template>
<script>
export default {
props: ['username', 'age', 'isSingle', 'car', 'hobby']
}
script>
<style>
.userinfo {
width: 300px;
border: 3px solid #000;
padding: 20px;
}
.userinfo > div {
margin: 20px 10px;
}
style>
父组件
<template>
<div class="app">
<BaseProgress :w="width">BaseProgress>
div>
template>
<script>
import BaseProgress from './components/BaseProgress.vue'
export default {
data() {
return {
width: 23,
}
},
components: {
BaseProgress,
},
}
script>
<style>
style>
子组件
<template>
<div class="base-progress">
<div class="inner" :style="{ width: w + '%' }">
<span>{{ w }}%span>
div>
div>
template>
<script>
export default {
// props: ["w"],
// 1.基础写法(类型校验)
// props: {
// w: Number // Number String Boolean Array Object
// }
// 2.完整写法(类型、是否必填、默认值、自定义校验)
props: {
w: {
type: Number,
// required: true
default: 0,
validator (value) {
if(value >= 0 && value <= 100){
return true
}
console.error('传入的prop w, 必须是0-100的数字')
return false
}
}
}
}
script>
<style scoped>
.base-progress {
height: 26px;
width: 400px;
border-radius: 15px;
background-color: #272425;
border: 3px solid #272425;
box-sizing: border-box;
margin-bottom: 30px;
}
.inner {
position: relative;
background: #379bff;
border-radius: 15px;
height: 25px;
box-sizing: border-box;
left: -3px;
top: -2px;
}
.inner span {
position: absolute;
right: 0;
top: 26px;
}
style>
父组件
<template>
<div class="app">
<BaseCount
@changeCount="handleChange"
:count="count">
BaseCount>
div>
template>
<script>
import BaseCount from './components/BaseCount.vue'
export default {
components:{
BaseCount
},
data(){
return {
count:100
}
},
methods:{
handleChange(value){
this.count = value
}
}
}
script>
<style>
style>
子组件
<template>
<div class="base-count">
<button @click="handleSub">-button>
<span>{{ count }}span>
<button @click="handleAdd">+button>
div>
template>
<script>
export default {
// 1.自己的数据随便修改 (谁的数据 谁负责)
// data () {
// return {
// count: 100,
// }
// },
// 2.外部传过来的数据 不能随便修改
// 单向数据流: 父组件的prop更新, 会单向向下流动, 影响到子组件.
props: {
count: Number
},
methods: {
handleAdd(){
this.$emit('changeCount', this.count+1)
},
handleSub(){
this.$emit('changeCount', this.count-1)
}
}
}
script>
<style>
.base-count {
margin: 20px;
}
style>
import Vue from 'vue'
const Bus = new Vue()
export default Bus
created() {
Bus.$on('sendMsg', (msg) => {
// console.log(msg)
this.msg = msg
})
}
Bus.$emit('sendMsg', '今天天气不错,适合旅游')
provide() {
return {
// 简单类型 是非响应式的
color: this.color,
// 复杂类型 是响应式的
userInfo: this.userInfo,
}
}
<script>
export default {
inject: ['color', 'userInfo'],
}
</script>
核心步骤
<template>
<section id="app">
<TodoHeaderVue @add="handleAdd">TodoHeaderVue>
<TodoMainVue @del="handleDel" :list="list">TodoMainVue>
<TodoFooterVue @clear="handleClear" :list="list">TodoFooterVue>
section>
template>
<script>
import TodoHeaderVue from './components/TodoHeader.vue'
import TodoMainVue from './components/TodoMain.vue'
import TodoFooterVue from './components/TodoFooter.vue'
// 渲染功能:
// 1. 提供数据-> 提供在公共的父组件 App.vue
// 2. 通过父传子, 奖数据传递给 TodoMain
// 3. 利用v-for渲染
// 添加功能
// 1. 收集表单数据 -> v-model
// 2. 监听事件 (回车 + 点击都要进行添加)
// 3. 子传父, 将任务名称传递给父组件App.vue
// 4. 进行添加 unshift
// 删除功能
// 1. 监听事件 (监听删除的点击) 携带id
// 2. 子传父, 将删除的id传递给父组件App.vue
// 3. 进行删除 filter
// 底部合计: 父传子list -> 渲染
// 清空功能: 子传父 通知父组件 -> 父组件进行清空
// 持久化存储: watch深度监视list的变化 -> 往本地存储 -> 进入页面优先读取本地存储
export default {
data () {
return {
list: JSON.parse(localStorage.getItem('list')) || [
{id: 1, name: '打篮球'},
{id: 2, name: '看电影'},
{id: 3, name: '逛街'},
]
}
},
methods: {
handleAdd(todoName){
this.list.unshift({
id: +new Date(),
name: todoName
})
},
handleDel(id){
this.list = this.list.filter(item => item.id!==id)
},
handleClear(){
this.list = []
}
},
watch: {
list: {
deep: true,
handler(newValue){
localStorage.setItem('list', JSON.stringify(newValue))
}
}
},
components: {
TodoHeaderVue,
TodoMainVue,
TodoFooterVue
}
}
script>
<style>
style>
<template>
<div>
<header class="header">
<h1>小黑记事本h1>
<input
v-model.trim="todoName"
@keyup.enter="handleAdd" placeholder="请输入任务" class="new-todo"/>
<button @click="handleAdd" class="add">添加任务button>
header>
div>
template>
<script>
export default {
data(){
return {
todoName: ''
}
},
methods: {
handleAdd(){
if(this.todoName.trim() === ''){
alert('任务名称不能为空')
return
}
this.$emit('add', this.todoName)
this.todoName = ''
}
}
}
script>
<style>
style>
<template>
<div>
<section class="main">
<ul class="todo-list">
<li class="todo" v-for="(item, index) in list" :key="item.id">
<div class="view">
<span class="index"> {{index+1}}. span>
<label> {{item.name}} label>
<button @click="handleDel(item.id)" class="destroy">button>
div>
li>
ul>
section>
div>
template>
<script>
export default {
props: {
list: Array
},
methods: {
handleDel(id){
this.$emit('del', id)
}
}
}
script>
<style>
style>
<template>
<div>
<footer class="footer">
<span class="todo-count">合 计:<strong> {{list.length}} strong>span>
<button @click="clear" class="clear-completed">
清空任务
button>
footer>
div>
template>
<script>
export default {
props: {
list: Array
},
methods: {
clear(){
this.$emit('clear')
}
}
}
script>
<style>
style>
$event
: 用在模板中, 获取事件的形参<div class="app">
<input v-model="msg1" type="text" /> <br />
<input :value="msg2" @input="msg2 = $event.target.value" type="text" >
div>
实现子组件和父组件数据的双向绑定
父组件
<template>
<div class="app">
<BaseSelect :selectId="selectId" @change="selectId = $event">BaseSelect>
div>
template>
<script>
import BaseSelect from './components/BaseSelect.vue'
export default {
data() {
return {
selectId: '102',
}
},
components: {
BaseSelect,
},
methods: {
}
}
script>
<style>
style>
子组件
<template>
<div>
<select :value="selectId" @change="handleChange">
<option value="101">北京option>
<option value="102">上海option>
<option value="103">武汉option>
<option value="104">广州option>
<option value="105">深圳option>
select>
div>
template>
<script>
export default {
props: {
selectId: String
},
methods: {
handleChange(e){
this.$emit('change', e.target.value)
}
}
}
script>
<style>
style>
父组件v-model简化实现子组件和父组件数据双向绑定
父组件
<template>
<div class="app">
<BaseSelect v-model="selectId">BaseSelect>
div>
template>
<script>
import BaseSelect from './components/BaseSelect.vue'
export default {
data() {
return {
selectId: '102',
}
},
components: {
BaseSelect,
},
}
script>
<style>
style>
子组件
<template>
<div>
<select :value="value" @change="handleChange">
<option value="101">北京option>
<option value="102">上海option>
<option value="103">武汉option>
<option value="104">广州option>
<option value="105">深圳option>
select>
div>
template>
<script>
export default {
props: {
value: String
},
methods: {
handleChange(e){
this.$emit('input', e.target.value)
}
}
}
script>
<style>
style>
:属性名 + @update:属性名
父组件
<template>
<div class="app">
<button @click="isShow = true">退出按钮button>
<BaseDialog :visible.sync="isShow">BaseDialog>
div>
template>
<script>
import BaseDialog from "./components/BaseDialog.vue"
export default {
data() {
return {
isShow: false
}
},
methods: {
},
components: {
BaseDialog,
},
}
script>
<style>
style>
子组件
<template>
<div v-show="visible" class="base-dialog-wrap">
<div class="base-dialog">
<div class="title">
<h3>温馨提示:h3>
<button @click="close" class="close">xbutton>
div>
<div class="content">
<p>你确认要退出本系统么?p>
div>
<div class="footer">
<button>确认button>
<button>取消button>
div>
div>
div>
template>
<script>
export default {
props: {
visible: Boolean
},
methods: {
close(){
this.$emit('update:visible', false)
}
}
}
script>
<style scoped>
.base-dialog-wrap {
width: 300px;
height: 200px;
box-shadow: 2px 2px 2px 2px #ccc;
position: fixed;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
padding: 0 10px;
}
.base-dialog .title {
display: flex;
justify-content: space-between;
align-items: center;
border-bottom: 2px solid #000;
}
.base-dialog .content {
margin-top: 38px;
}
.base-dialog .title .close {
width: 20px;
height: 20px;
cursor: pointer;
line-height: 10px;
}
.footer {
display: flex;
justify-content: flex-end;
margin-top: 26px;
}
.footer button {
width: 80px;
height: 40px;
}
.footer button:nth-child(1) {
margin-right: 10px;
cursor: pointer;
}
style>
ref
和$refs
可以获取dom元素和组件实例<div ref="test">div>
ref属性值
获取目标组件this.$refs.test
<div ref="mychart" class="base-chart-box">子组件</div>
const myChart = echarts.init(this.$refs.mychart)
<template>
<div class="app">
<BaseForm ref="baseFrom">BaseForm>
<button @click="handleGet">获取数据button>
<button @click="handleReset">重置数据button>
div>
template>
<script>
import BaseForm from './components/BaseForm.vue'
export default {
components: {
BaseForm,
},
methods: {
handleGet(){
console.log(this.$refs.baseFrom.getValues())
},
handleReset(){
this.$refs.baseFrom.resetValues()
}
}
}
script>
<style>
style>
子组件<template>
<div class="app">
<div>
账号: <input v-model="username" type="text">
div>
<div>
密码: <input v-model="password" type="text">
div>
div>
template>
<script>
export default {
data() {
return {
username: 'admin',
password: '123456',
}
},
methods: {
getValues() {
return {
username: this.username,
password: this.password
}
},
resetValues() {
this.username = ''
this.password = ''
console.log('重置表单数据成功');
},
}
}
script>
<style scoped>
.app {
border: 2px solid #ccc;
padding: 10px;
}
.app div{
margin: 10px 0;
}
.app div button{
margin-right: 8px;
}
style>
$nextTick
: 在DOM更新完成之后做某件事<template>
<div class="app">
<div v-if="isShowEdit">
<input type="text" v-model="editValue" ref="inp" />
<button>确认button>
div>
<div v-else>
<span>{{ title }}span>
<button @click="handleEdit">编辑button>
div>
div>
template>
<script>
export default {
data() {
return {
title: '大标题',
isShowEdit: false,
editValue: '',
}
},
methods: {
handleEdit(){
// 1. 显示输入框 (异步dom更新)
this.isShowEdit = true
// 2. 让输入框显示焦点
// console.log(this.$refs.inp) // undefined
this.$nextTick(()=>{
this.$refs.inp.focus()
})
}
},
}
script>
<style>
style>
黑马程序员. Vue2+Vue3基础入门到实战项目