目录
1、使用ESlint之后,文件的name的编写
添加ESlint之后,项目组件的name必须是写成‘XXXName’的格式,也就是驼峰式命名,不能为单个单词。或者关闭eslint的组件命名规则,在.eslintrc.js中修改
module.exports = {
rules: {
//在rules中添加自定义规则
//关闭组件命名规则
"vue/multi-word-component-names": "off",
},
};
2、使用import引入图片
在<script>标签内使用import 引入例如
import zhIcon from"@/static/zh.png";
import enIcon from"@/static/en.png";
使用setup的return抛出到template
import { reactive,toRefs } from "vue";
setup() {
const state = reactive({f);
return {
..toRefs(state),
zhIcon,
zhIcon,
}
}
在template内部使用
<img :src="zhIcon”/>
或者是直接在template中直接使用,不过注意的是好像不能使用@的方式
<img src="../../assets/imgs/daiban.png" style="width: 20px; height: 20px" />
3、动态添加属性
可以使用ts提供的interface接口进行定义
setup(props: any, context: any) {
interface theObj {
Name:string //固定属性
[x: string]: any //动态添加属性
}
const a: theObj = [
{
id: 1,
Name: 'He',
area: '11',
},
{
id: 2,
Name: 'Nan',
area: '65',
},
]
a.forEach((i:any) => {
i['show'] = true
})
console.log(a)
return {
a,
}
},
当然也可以有固定属性
interface theObj {
Name:string //固定属性
[x: string]: any //动态添加属性
}
可选属性
interface theObj {
color?:string //可选属性
}
4、使用ref获取dom元素
获取dom要在onMounted钩子函数中 获取,或者添加一个点击事件
<template>
<div ref="box"></div>
</template>
<script>
import { ref, onMounted } from "vue";
export default {
setup() {
const box = ref(null);
onMounted(() => {
console.log(box.value);
});
return { box };
},
};
</script>
5、ref、reactive、toRefs函数的使用
简单数据类型使用ref
ref也可以用来定义对象(或数组)类型数据,它内部会自动通过reactive转为代理对象
<script>
import {ref} from 'vue'
export default {
setup() {
let num=ref(1),
funtion addNum(){
num.value++
console.log(num);
}
return {num,addNum}
},
};
</script>
复杂数据类型使用reactive
<template>
<div>
{{ obj.num}}
<button @click="addNum">加1</button>
</div>
</template>
<script>
import {reactive} from 'vue'
export default {
setup() {
const obj = reactive({num:1})
const addNum = ()=>obj.num++
return {obj,addNum}
},
};
</script>
toRefs函数进行解构
定义完响应式数据后,我们每次都要在模板中 通过对象.的形式进行渲染数据,这样就太麻烦了,所以可以通过toRefs函数进行解构。 toRefs与reactive配套使用。
<template>
<div>
{{ num }}
<button @click="addNum">加1</button>
</div>
</template>
<script>
import {reactive, toRefs} from 'vue'
export default {
setup() {
const obj = reactive({num :1})
const addNum = ()=>obj.num ++
return {...toRefs(obj),addNum}
},
};
</script>
6、watch监听的使用
//immediate首次监听,deep深度监听
watch( 数据|数组|get函数,
(newValue,oldValue)=>{ 回调处理逻辑 },
{immediate:true|false, deep: true|false}
)
单个简单数据类型
<script>
import {ref,watch} from 'vue'
export default {
setup() {
let num=ref(1),
addNum=()=>{
num.value++
}
watch(()=>{
return num
},(newValue)=>{
console.log(newValue);
})
return {num,addNum}
},
};
</script>
多个简单数据类型
<script>
import {ref,watch} from 'vue'
export default {
setup() {
let num=ref(1)
let num2=ref(2)
const addNum=()=>num.value++
const addNum2=()=>num2.value++
watch([num,num2],(newValue)=>{
console.log(newValue);
})
return {num,num2,addNum,addNum2}
},
};
</script>
复杂数据类型整体
<template>
姓名:{{ stu.name }},公司:{{ stu.company }}
月薪 {{ stu.money.salary }}
<button @click="stu.company ='CSDN'">跳槽到CSDN</button>
</template>
<script>
import { reactive, watch } from 'vue'
export default {
setup() {
const stu = reactive({
name: '小王',
company:'JD',
money: {
salary: 100
}
})
watch(stu, () => {
// 数据变化之后的回调函数
console.log('stu发生了变化')
})
return { stu }
}
}
</script>
复杂数据类型中的某一个属性为简单数据类型
<script>
import { reactive, watch } from 'vue'
export default {
setup() {
const stu = reactive({
name: '小王',
company:'CSDN',
money: {
salary: 100
}
})
watch(()=>{
return stu.money.salary
}, () => {
// 数据变化之后的回调函数
console.log('薪资发生变化了')
})
return { stu }
}
}
</script>
复杂数据类型中的某多个属性为简单数据类型
<script>
import { reactive, watch } from 'vue'
export default {
setup() {
const stu = reactive({
name: '小王',
company:'CSDN',
money: {
salary: 100
}
})
watch([()=>stu.name,()=>stu.company], (newValue,oldValue) => {
// 数据变化之后的回调函数
console.log(newValue,oldValue)
})
return { stu }
}
}
</script>
复杂数据类型中的某一个属性为复杂数据类型,深层次的对象
<script>
import { reactive, watch } from 'vue'
export default {
setup() {
const stu = reactive({
name: '小王',
company:'JD',
money: {
salary: 100,
bonus:200
}
})
watch(() => {
return stu.money
}, () => {
console.log('stu.money发生了变化')
}, {
deep: true // 只有为true,才能监听money下所有属性的变化
})
return {
stu
}
}
}
</script>
7、父子组件通信
(1)父传子
父
<template>
<div>
<Son :dialog="dialog"></Son>
</div>
</template>
<script>
import { ref } from 'vue';
import Son from "./son.vue";
export default {
components: {
Son,
},
setup() {
const dialog= ref(false)
return { dialog }
},
};
</script>
子
<template>
<div>
子组件 {{dialog}}
</div>
</template>
<script>
export default {
props:{
dialog:{
type: Boolean,
default: false,
}
},
setup(props,context){
console.log(props.dialog);
}
};
</script>
(2)子传父
子
<script>
export default {
props:{
dialog:{
type: Boolean,
default: false,
}
},
setup(props,context){
context.emit('update:dialog',false)
}
};
</script>
父
<template>
<Son @dialog="dialog" ></Son>
</template>
<script>
export default {
setup(props,context){
const dialog= (dialog)=>console.log(dialog);
return {dialog}
}
};
</script>
(1)典型的示例引用——弹窗
子
<template>
<div id="history">
<el-dialog
title="弹窗"
v-model="dialogShow"
width="65%"
:before-close="Close"
append-to-body
>
<div class="box1"></div>
</el-dialog>
</div>
</template>
<script lang='ts'>
/* eslint-disable */
// eslint-disable-next-line vue/no-setup-props-destructure
import { defineComponent, ref, toRefs, computed, watch, reactive } from 'vue'
export default defineComponent({
props: {
dialog: {
type: Boolean,
default: false,
},
},
setup(props: any, context: any) {
const dialogShow = computed({
get() {
return props.dialog
},
set(value) {
context.emit('update:dialog',false)
},
})
// 关闭弹窗
function Close() {
context.emit('update:dialog', false)
}
return {
dialogShow,
Close,
}
},
})
</script>
父
<template>
<div>
<HistoricalDetails
v-model:dialog="dialog"
ref="DetailsDialog"
></HistoricalDetails>
</div>
</template>
<script lang='ts'>
import { defineComponent, reactive, computed, ref, toRefs } from 'vue'
import { useStore } from 'vuex'
import type { TabsPaneContext } from 'element-plus'
import HistoricalDetails from '@/components/Information/HistoricalDetails.vue'
export default defineComponent({
name: 'Information',
components: {
HistoricalDetails,
},
setup(props: any, context: any) {
const dialog = ref(false) //弹窗显示
return {
dialog
}
},
})
</script>
8、自定义hook函数
把setup函数中使用的Composition API进行了封装
新建src/hook/add.js
import {reactive,onMounted,onBeforeUnmount } from 'vue'
function savePoint(){
let point = reactive({
x: null,
y: null
})
const savePoint = (e)=>{
point.x = e.pageX
point.y = e.pageY
}
onMounted(()=>{
window.addEventListener('click',savePoint)
})
onBeforeUnmount(()=>{
window.removeEventListener('click',savePoint)
})
return point
}
export default savePoint
组件使用
<template>
<p>当前求和为: {{sum}} </p>
<button @click="sum++">加一</button>
<hr>
<h2>当前点击时候的坐标: x: {{point.x}} y:{{point.y}}</h2>
</template>
<script>
import { ref } from 'vue'
import add from '../hooks/add '
export default {
name: 'test8',
setup(props,context){
let sum = ref(0)
let point = add ()
return {
sum,
point
}
}
}
</script>