指令
组件之间传值
父组件向子组件传值
<Child2 :name="name" :arr="arr"></Child2>
props: {
name: {
type: String,
},
arr: {
type: Array,
},
}
子组件向父组件传值
created() {
this.$emit("addNum", [22, 234, 2]);
},
<Child2 :name="name" :arr="arr" @addNum="addNum"></Child2> /
methods: {
addNum(arr) {
this.arr = arr;
},
},
类名
<span class="description">
This is how you add static classes in Vue.
</span>
<span :class="'description'">
This is how you add static classes in Vue.
</span>
<span
class="description"
:class="theme"
>
This is how you add static classes in Vue.
</span>
<span
class="description"
:class="darkMode ? 'dark-theme' : 'light-theme'"
>
This is how you add dynamic classes in Vue.
</span>
<span
class="description"
:class="[
fontTheme,
darkMode ? 'dark-theme' : 'light-theme',
]"
>
This is how you add dynamic classes in Vue.
</span>
<span
class="description"
:class="{
'dark-theme': darkMode,
'light-theme': !darkMode,
]"
>
This is how you add dynamic classes in Vue.
</span>
插槽slot
<my-button btnStyle="btnS">苗二伟</my-button>
<button :class="['my-btn', btnStyle]">
<slot></slot>
</button>
<i-button slot="icon">按钮1</i-button>
<i-button >按钮2</i-button>
<i-button ></i-button>
<template>
<button>
<slot name="icon"></slot>
<slot>solot里面默认的内容</slot>
</button>
</template>
<template>
<div class="home">
我是Home父组件
<HelloWorld>
<template v-slot:default="slotProps">
<h1>{{slotProps.users}}</h1>
</template>
</HelloWorld>
</div>
</template>
<template>
<div class="hello">
Helloworld子组件
<div #slotLeft>
<slot v-bind:users="user"></slot>
</div>
</div>
</template>
<template>
<div class="home">
我是Home父组件
<HelloWorld>
<template v-slot="{ users }">
<h1>{{users.name}}</h1>
</template>
</HelloWorld>
</div>
</template>
<template>
<div class="hello">
Helloworld子组件
<div class='slotLeft'>
<slot v-bind:users="user"></slot>
</div>
</div>
</template>
响应式
<div>{{ person }}</div>
<button @click="onClick">加加</button>
person:{
name:'miao'
}
onClick(){
if (!this.person.age) {
this.person.age= 20
console.log(this.person)
} else {
this.person.age++
}
}
computed
<div id="app">
<button @click="add">添加商品</button>
<table border="1">
<tr>
<th>商品名称</th>
<th>单价</th>
<th>数量</th>
</tr>
<tr v-for="(item, index) in shopingCarts" :key="index">
<td><input v-model="item.goodsName" /></td>
<td><input v-model="item.price" /></td>
<td><input v-model="item.quantity" /></td>
</tr>
</table>
<h3>总价: {{ totalPrice }} 元</h3>
</div>
export default {
data() {
return {
shopingCarts: [{ goodsName: "", price: "", quantity: "" }],
};
},
computed: {
totalPrice: function () {
let total = 0;
this.shopingCarts.forEach((item) => {
let subtotal = item.price * item.quantity;
total += subtotal;
});
return total;
},
},
methods: {
add: function () {
this.shopingCarts.push({ goodsName: "", price: "", quantity: "" });
},
},
};
watch
watch: {
firstName(newName, oldName) {
this.fullName = newName + ' ' + this.lastName;
}
}
data: {
obj: {
a: 123
}
},
watch: {
obj: {
handler(newName, oldName) {
console.log('obj.a changed');
},
mmediate: true,
deep: true
}
}
watch: {
'obj.a': {
handler(newName, oldName) {
console.log('obj.a changed');
},
immediate: true,
}
}
nextTick
<p ref="love_p">{{ title }}</p>
<button @click="love()">爱</button>
data() {
return {
title: "我爱生活",
},
methods: {
love() {
this.title = "你爱放屁";
this.$nextTick(() => {
console.log(this.$refs.love_p.innerHTML);
});
}
},
SSR
|